1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.tasks.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.workflow.WorkflowConstants;
21  import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
22  import com.liferay.portal.model.ResourceConstants;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portlet.tasks.NoSuchProposalException;
26  import com.liferay.portlet.tasks.ProposalDueDateException;
27  import com.liferay.portlet.tasks.model.TasksProposal;
28  import com.liferay.portlet.tasks.service.base.TasksProposalLocalServiceBaseImpl;
29  import com.liferay.portlet.tasks.social.TasksActivityKeys;
30  
31  import java.util.Date;
32  import java.util.List;
33  
34  /**
35   * <a href="TasksProposalLocalServiceImpl.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Raymond Augé
39   * @author Brian Wing Shun Chan
40   */
41  public class TasksProposalLocalServiceImpl
42      extends TasksProposalLocalServiceBaseImpl {
43  
44      public TasksProposal addProposal(
45              long userId, long groupId, String className, String classPK,
46              String name, String description, long reviewUserId,
47              boolean addCommunityPermissions, boolean addGuestPermissions)
48          throws PortalException, SystemException {
49  
50          return addProposal(
51              userId, groupId, className, classPK, name, description,
52              reviewUserId, Boolean.valueOf(addCommunityPermissions),
53              Boolean.valueOf(addGuestPermissions), null, null);
54      }
55  
56      public TasksProposal addProposal(
57              long userId, long groupId, String className, String classPK,
58              String name, String description, long reviewUserId,
59              Boolean addCommunityPermissions, Boolean addGuestPermissions,
60              String[] communityPermissions, String[] guestPermissions)
61          throws PortalException, SystemException {
62  
63          // Proposal
64  
65          User user = userPersistence.findByPrimaryKey(userId);
66          long classNameId = PortalUtil.getClassNameId(className);
67          Date now = new Date();
68  
69          long proposalId = counterLocalService.increment();
70  
71          TasksProposal proposal = tasksProposalPersistence.create(proposalId);
72  
73          proposal.setGroupId(groupId);
74          proposal.setCompanyId(user.getCompanyId());
75          proposal.setUserId(user.getUserId());
76          proposal.setUserName(user.getFullName());
77          proposal.setCreateDate(now);
78          proposal.setModifiedDate(now);
79          proposal.setClassNameId(classNameId);
80          proposal.setClassPK(classPK);
81          proposal.setName(name);
82          proposal.setDescription(description);
83  
84          proposal = tasksProposalPersistence.update(proposal, false);
85  
86          // Resources
87  
88          if ((addCommunityPermissions != null) &&
89              (addGuestPermissions != null)) {
90  
91              addProposalResources(
92                  proposal, addCommunityPermissions.booleanValue(),
93                  addGuestPermissions.booleanValue());
94          }
95          else {
96              addProposalResources(
97                  proposal, communityPermissions, guestPermissions);
98          }
99  
100         // Review
101 
102         long assignedByUserId = userId;
103         int stage = 1;
104 
105         tasksReviewLocalService.addReview(
106             reviewUserId, proposal.getProposalId(), assignedByUserId, stage);
107 
108         // Message boards
109 
110         boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
111 
112         WorkflowThreadLocal.setEnabled(false);
113 
114         try {
115             mbMessageLocalService.addDiscussionMessage(
116                 userId, proposal.getUserName(), groupId,
117                 TasksProposal.class.getName(), proposalId,
118                 WorkflowConstants.ACTION_PUBLISH);
119         }
120         finally {
121             WorkflowThreadLocal.setEnabled(workflowEnabled);
122         }
123 
124         // Social
125 
126         socialActivityLocalService.addActivity(
127             userId, groupId, TasksProposal.class.getName(), proposalId,
128             TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
129 
130         return proposal;
131     }
132 
133     public TasksProposal addProposal(
134             long userId, long groupId, String className, String classPK,
135             String name, String description, long reviewUserId,
136             String[] communityPermissions, String[] guestPermissions)
137         throws PortalException, SystemException {
138 
139         return addProposal(
140             userId, groupId, className, classPK, name, description,
141             reviewUserId, null, null, communityPermissions, guestPermissions);
142     }
143 
144     public void addProposalResources(
145             long proposalId, boolean addCommunityPermissions,
146             boolean addGuestPermissions)
147         throws PortalException, SystemException {
148 
149         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
150             proposalId);
151 
152         addProposalResources(
153             proposal, addCommunityPermissions, addGuestPermissions);
154     }
155 
156     public void addProposalResources(
157             long proposalId, String[] communityPermissions,
158             String[] guestPermissions)
159         throws PortalException, SystemException {
160 
161         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
162             proposalId);
163 
164         addProposalResources(proposal, communityPermissions, guestPermissions);
165     }
166 
167     public void addProposalResources(
168             TasksProposal proposal, boolean addCommunityPermissions,
169             boolean addGuestPermissions)
170         throws PortalException, SystemException {
171 
172         resourceLocalService.addResources(
173             proposal.getCompanyId(), proposal.getGroupId(),
174             proposal.getUserId(), TasksProposal.class.getName(),
175             proposal.getProposalId(), false, addCommunityPermissions,
176             addGuestPermissions);
177     }
178 
179     public void addProposalResources(
180             TasksProposal proposal, String[] communityPermissions,
181             String[] guestPermissions)
182         throws PortalException, SystemException {
183 
184         resourceLocalService.addModelResources(
185             proposal.getCompanyId(), proposal.getGroupId(),
186             proposal.getUserId(), TasksProposal.class.getName(),
187             proposal.getProposalId(), communityPermissions, guestPermissions);
188     }
189 
190     public void deleteProposal(long proposalId)
191         throws PortalException, SystemException {
192 
193         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
194             proposalId);
195 
196         deleteProposal(proposal);
197     }
198 
199     public void deleteProposal(long classNameId, String classPK)
200         throws PortalException, SystemException {
201 
202         try {
203             TasksProposal proposal = getProposal(classNameId, classPK);
204 
205             deleteProposal(proposal);
206         }
207         catch (NoSuchProposalException nspe) {
208         }
209     }
210 
211     public void deleteProposal(String className, String classPK)
212         throws PortalException, SystemException {
213 
214         long classNameId = PortalUtil.getClassNameId(className);
215 
216         deleteProposal(classNameId, classPK);
217     }
218 
219     public void deleteProposal(TasksProposal proposal)
220         throws PortalException, SystemException {
221 
222         // Proposal
223 
224         tasksProposalPersistence.remove(proposal);
225 
226         // Resources
227 
228         resourceLocalService.deleteResource(
229             proposal.getCompanyId(), TasksProposal.class.getName(),
230             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
231 
232         // Reviews
233 
234         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
235 
236         // Message boards
237 
238         mbMessageLocalService.deleteDiscussionMessages(
239             TasksProposal.class.getName(), proposal.getProposalId());
240 
241         // Social
242 
243         socialActivityLocalService.deleteActivities(
244             TasksProposal.class.getName(), proposal.getProposalId());
245     }
246 
247     public void deleteProposals(long groupId)
248         throws PortalException, SystemException {
249 
250         List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
251             groupId);
252 
253         for (TasksProposal proposal : proposals) {
254             deleteProposal(proposal);
255         }
256     }
257 
258     public TasksProposal getProposal(long proposalId)
259         throws PortalException, SystemException {
260 
261         return tasksProposalPersistence.findByPrimaryKey(proposalId);
262     }
263 
264     public TasksProposal getProposal(long classNameId, String classPK)
265         throws PortalException, SystemException {
266 
267         return tasksProposalPersistence.findByC_C(classNameId, classPK);
268     }
269 
270     public TasksProposal getProposal(String className, String classPK)
271         throws PortalException, SystemException {
272 
273         long classNameId = PortalUtil.getClassNameId(className);
274 
275         return getProposal(classNameId, classPK);
276     }
277 
278     public List<TasksProposal> getProposals(long groupId, int start, int end)
279         throws SystemException {
280 
281         return tasksProposalPersistence.findByGroupId(groupId, start, end);
282     }
283 
284     public int getProposalsCount(long groupId) throws SystemException {
285         return tasksProposalPersistence.countByGroupId(groupId);
286     }
287 
288     public List<TasksProposal> getReviewProposals(
289             long groupId, long userId, int start, int end)
290         throws SystemException {
291 
292         return tasksProposalFinder.findByG_U(groupId, userId, start, end);
293     }
294 
295     public int getReviewProposalsCount(long groupId, long userId)
296         throws SystemException {
297 
298         return tasksProposalFinder.countByG_U(groupId, userId);
299     }
300 
301     public List<TasksProposal> getUserProposals(
302             long groupId, long userId, int start, int end)
303         throws SystemException {
304 
305         return tasksProposalPersistence.findByG_U(groupId, userId, start, end);
306     }
307 
308     public int getUserProposalsCount(long groupId, long userId)
309         throws SystemException {
310 
311         return tasksProposalPersistence.countByG_U(groupId, userId);
312     }
313 
314     public TasksProposal updateProposal(
315             long userId, long proposalId, String description, int dueDateMonth,
316             int dueDateDay, int dueDateYear, int dueDateHour, int dueDateMinute)
317         throws PortalException, SystemException {
318 
319         User user = userPersistence.findByPrimaryKey(userId);
320 
321         Date dueDate = PortalUtil.getDate(
322             dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
323             user.getTimeZone(), new ProposalDueDateException());
324 
325         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
326             proposalId);
327 
328         proposal.setModifiedDate(new Date());
329         proposal.setDescription(description);
330         proposal.setDueDate(dueDate);
331 
332         tasksProposalPersistence.update(proposal, false);
333 
334         return proposal;
335     }
336 
337 }