1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tasks.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.ResourceConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.tasks.NoSuchProposalException;
32  import com.liferay.portlet.tasks.ProposalDueDateException;
33  import com.liferay.portlet.tasks.model.TasksProposal;
34  import com.liferay.portlet.tasks.service.base.TasksProposalLocalServiceBaseImpl;
35  import com.liferay.portlet.tasks.social.TasksActivityKeys;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="TasksProposalLocalServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Raymond Augé
45   * @author Brian Wing Shun Chan
46   */
47  public class TasksProposalLocalServiceImpl
48      extends TasksProposalLocalServiceBaseImpl {
49  
50      public TasksProposal addProposal(
51              long userId, long groupId, String className, String classPK,
52              String name, String description, long reviewUserId,
53              boolean addCommunityPermissions, boolean addGuestPermissions)
54          throws PortalException, SystemException {
55  
56          return addProposal(
57              userId, groupId, className, classPK, name, description,
58              reviewUserId, Boolean.valueOf(addCommunityPermissions),
59              Boolean.valueOf(addGuestPermissions), null, null);
60      }
61  
62      public TasksProposal addProposal(
63              long userId, long groupId, String className, String classPK,
64              String name, String description, long reviewUserId,
65              Boolean addCommunityPermissions, Boolean addGuestPermissions,
66              String[] communityPermissions, String[] guestPermissions)
67          throws PortalException, SystemException {
68  
69          // Proposal
70  
71          User user = userPersistence.findByPrimaryKey(userId);
72          long classNameId = PortalUtil.getClassNameId(className);
73          Date now = new Date();
74  
75          long proposalId = counterLocalService.increment();
76  
77          TasksProposal proposal = tasksProposalPersistence.create(proposalId);
78  
79          proposal.setGroupId(groupId);
80          proposal.setCompanyId(user.getCompanyId());
81          proposal.setUserId(user.getUserId());
82          proposal.setUserName(user.getFullName());
83          proposal.setCreateDate(now);
84          proposal.setModifiedDate(now);
85          proposal.setClassNameId(classNameId);
86          proposal.setClassPK(classPK);
87          proposal.setName(name);
88          proposal.setDescription(description);
89  
90          proposal = tasksProposalPersistence.update(proposal, false);
91  
92          // Resources
93  
94          if ((addCommunityPermissions != null) &&
95              (addGuestPermissions != null)) {
96  
97              addProposalResources(
98                  proposal, addCommunityPermissions.booleanValue(),
99                  addGuestPermissions.booleanValue());
100         }
101         else {
102             addProposalResources(
103                 proposal, communityPermissions, guestPermissions);
104         }
105 
106         // Review
107 
108         long assignedByUserId = userId;
109         int stage = 1;
110 
111         tasksReviewLocalService.addReview(
112             reviewUserId, proposal.getProposalId(), assignedByUserId, stage);
113 
114         // Message boards
115 
116         mbMessageLocalService.addDiscussionMessage(
117             userId, proposal.getUserName(), TasksProposal.class.getName(),
118             proposalId);
119 
120         // Social
121 
122         socialActivityLocalService.addActivity(
123             userId, groupId, TasksProposal.class.getName(), proposalId,
124             TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
125 
126         return proposal;
127     }
128 
129     public TasksProposal addProposal(
130             long userId, long groupId, String className, String classPK,
131             String name, String description, long reviewUserId,
132             String[] communityPermissions, String[] guestPermissions)
133         throws PortalException, SystemException {
134 
135         return addProposal(
136             userId, groupId, className, classPK, name, description,
137             reviewUserId, null, null, communityPermissions, guestPermissions);
138     }
139 
140     public void addProposalResources(
141             long proposalId, boolean addCommunityPermissions,
142             boolean addGuestPermissions)
143         throws PortalException, SystemException {
144 
145         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
146             proposalId);
147 
148         addProposalResources(
149             proposal, addCommunityPermissions, addGuestPermissions);
150     }
151 
152     public void addProposalResources(
153             long proposalId, String[] communityPermissions,
154             String[] guestPermissions)
155         throws PortalException, SystemException {
156 
157         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
158             proposalId);
159 
160         addProposalResources(proposal, communityPermissions, guestPermissions);
161     }
162 
163     public void addProposalResources(
164             TasksProposal proposal, boolean addCommunityPermissions,
165             boolean addGuestPermissions)
166         throws PortalException, SystemException {
167 
168         resourceLocalService.addResources(
169             proposal.getCompanyId(), proposal.getGroupId(),
170             proposal.getUserId(), TasksProposal.class.getName(),
171             proposal.getProposalId(), false, addCommunityPermissions,
172             addGuestPermissions);
173     }
174 
175     public void addProposalResources(
176             TasksProposal proposal, String[] communityPermissions,
177             String[] guestPermissions)
178         throws PortalException, SystemException {
179 
180         resourceLocalService.addModelResources(
181             proposal.getCompanyId(), proposal.getGroupId(),
182             proposal.getUserId(), TasksProposal.class.getName(),
183             proposal.getProposalId(), communityPermissions, guestPermissions);
184     }
185 
186     public void deleteProposal(long proposalId)
187         throws PortalException, SystemException {
188 
189         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
190             proposalId);
191 
192         deleteProposal(proposal);
193     }
194 
195     public void deleteProposal(long classNameId, String classPK)
196         throws PortalException, SystemException {
197 
198         try {
199             TasksProposal proposal = getProposal(classNameId, classPK);
200 
201             deleteProposal(proposal);
202         }
203         catch (NoSuchProposalException nspe) {
204         }
205     }
206 
207     public void deleteProposal(String className, String classPK)
208         throws PortalException, SystemException {
209 
210         long classNameId = PortalUtil.getClassNameId(className);
211 
212         deleteProposal(classNameId, classPK);
213     }
214 
215     public void deleteProposal(TasksProposal proposal)
216         throws PortalException, SystemException {
217 
218         // Proposal
219 
220         tasksProposalPersistence.remove(proposal);
221 
222         // Resources
223 
224         resourceLocalService.deleteResource(
225             proposal.getCompanyId(), TasksProposal.class.getName(),
226             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
227 
228         // Reviews
229 
230         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
231 
232         // Message boards
233 
234         mbMessageLocalService.deleteDiscussionMessages(
235             TasksProposal.class.getName(), proposal.getProposalId());
236 
237         // Social
238 
239         socialActivityLocalService.deleteActivities(
240             TasksProposal.class.getName(), proposal.getProposalId());
241     }
242 
243     public void deleteProposals(long groupId)
244         throws PortalException, SystemException {
245 
246         List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
247             groupId);
248 
249         for (TasksProposal proposal : proposals) {
250             deleteProposal(proposal);
251         }
252     }
253 
254     public TasksProposal getProposal(long proposalId)
255         throws PortalException, SystemException {
256 
257         return tasksProposalPersistence.findByPrimaryKey(proposalId);
258     }
259 
260     public TasksProposal getProposal(long classNameId, String classPK)
261         throws PortalException, SystemException {
262 
263         return tasksProposalPersistence.findByC_C(classNameId, classPK);
264     }
265 
266     public TasksProposal getProposal(String className, String classPK)
267         throws PortalException, SystemException {
268 
269         long classNameId = PortalUtil.getClassNameId(className);
270 
271         return getProposal(classNameId, classPK);
272     }
273 
274     public List<TasksProposal> getProposals(long groupId, int start, int end)
275         throws SystemException {
276 
277         return tasksProposalPersistence.findByGroupId(groupId, start, end);
278     }
279 
280     public int getProposalsCount(long groupId) throws SystemException {
281         return tasksProposalPersistence.countByGroupId(groupId);
282     }
283 
284     public List<TasksProposal> getReviewProposals(
285             long groupId, long userId, int start, int end)
286         throws SystemException {
287 
288         return tasksProposalFinder.findByG_U(groupId, userId, start, end);
289     }
290 
291     public int getReviewProposalsCount(long groupId, long userId)
292         throws SystemException {
293 
294         return tasksProposalFinder.countByG_U(groupId, userId);
295     }
296 
297     public List<TasksProposal> getUserProposals(
298             long groupId, long userId, int start, int end)
299         throws SystemException {
300 
301         return tasksProposalPersistence.findByG_U(groupId, userId, start, end);
302     }
303 
304     public int getUserProposalsCount(long groupId, long userId)
305         throws SystemException {
306 
307         return tasksProposalPersistence.countByG_U(groupId, userId);
308     }
309 
310     public TasksProposal updateProposal(
311             long userId, long proposalId, String description, int dueDateMonth,
312             int dueDateDay, int dueDateYear, int dueDateHour, int dueDateMinute)
313         throws PortalException, SystemException {
314 
315         User user = userPersistence.findByPrimaryKey(userId);
316 
317         Date dueDate = PortalUtil.getDate(
318             dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
319             user.getTimeZone(), new ProposalDueDateException());
320 
321         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
322             proposalId);
323 
324         proposal.setModifiedDate(new Date());
325         proposal.setDescription(description);
326         proposal.setDueDate(dueDate);
327 
328         tasksProposalPersistence.update(proposal, false);
329 
330         return proposal;
331     }
332 
333 }