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   */
48  public class TasksProposalLocalServiceImpl
49      extends TasksProposalLocalServiceBaseImpl {
50  
51      public TasksProposal addProposal(
52              long userId, long groupId, String className, String classPK,
53              String name, String description, long reviewUserId,
54              boolean addCommunityPermissions, boolean addGuestPermissions)
55          throws PortalException, SystemException {
56  
57          return addProposal(
58              userId, groupId, className, classPK, name, description,
59              reviewUserId, Boolean.valueOf(addCommunityPermissions),
60              Boolean.valueOf(addGuestPermissions), null, null);
61      }
62  
63      public TasksProposal addProposal(
64              long userId, long groupId, String className, String classPK,
65              String name, String description, long reviewUserId,
66              String[] communityPermissions, String[] guestPermissions)
67          throws PortalException, SystemException {
68  
69          return addProposal(
70              userId, groupId, className, classPK, name, description,
71              reviewUserId, null, null, communityPermissions, guestPermissions);
72      }
73  
74      public TasksProposal addProposal(
75              long userId, long groupId, String className, String classPK,
76              String name, String description, long reviewUserId,
77              Boolean addCommunityPermissions, Boolean addGuestPermissions,
78              String[] communityPermissions, String[] guestPermissions)
79          throws PortalException, SystemException {
80  
81          // Proposal
82  
83          User user = userPersistence.findByPrimaryKey(userId);
84          long classNameId = PortalUtil.getClassNameId(className);
85          Date now = new Date();
86  
87          long proposalId = counterLocalService.increment();
88  
89          TasksProposal proposal = tasksProposalPersistence.create(proposalId);
90  
91          proposal.setGroupId(groupId);
92          proposal.setCompanyId(user.getCompanyId());
93          proposal.setUserId(user.getUserId());
94          proposal.setUserName(user.getFullName());
95          proposal.setCreateDate(now);
96          proposal.setModifiedDate(now);
97          proposal.setClassNameId(classNameId);
98          proposal.setClassPK(classPK);
99          proposal.setName(name);
100         proposal.setDescription(description);
101 
102         proposal = tasksProposalPersistence.update(proposal, false);
103 
104         // Resources
105 
106         if ((addCommunityPermissions != null) &&
107             (addGuestPermissions != null)) {
108 
109             addProposalResources(
110                 proposal, addCommunityPermissions.booleanValue(),
111                 addGuestPermissions.booleanValue());
112         }
113         else {
114             addProposalResources(
115                 proposal, communityPermissions, guestPermissions);
116         }
117 
118         // Review
119 
120         long assignedByUserId = userId;
121         int stage = 1;
122 
123         tasksReviewLocalService.addReview(
124             reviewUserId, proposal.getProposalId(), assignedByUserId, stage);
125 
126         mbMessageLocalService.addDiscussionMessage(
127             userId, proposal.getUserName(), TasksProposal.class.getName(),
128             proposalId);
129 
130         // Social
131 
132         socialActivityLocalService.addActivity(
133             userId, groupId, TasksProposal.class.getName(), proposalId,
134             TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
135 
136         return proposal;
137     }
138 
139     public void addProposalResources(
140             long proposalId, boolean addCommunityPermissions,
141             boolean addGuestPermissions)
142         throws PortalException, SystemException {
143 
144         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
145             proposalId);
146 
147         addProposalResources(
148             proposal, addCommunityPermissions, addGuestPermissions);
149     }
150 
151     public void addProposalResources(
152             TasksProposal proposal, boolean addCommunityPermissions,
153             boolean addGuestPermissions)
154         throws PortalException, SystemException {
155 
156         resourceLocalService.addResources(
157             proposal.getCompanyId(), proposal.getGroupId(),
158             proposal.getUserId(), TasksProposal.class.getName(),
159             proposal.getProposalId(), false, addCommunityPermissions,
160             addGuestPermissions);
161     }
162 
163     public void addProposalResources(
164             long proposalId, String[] communityPermissions,
165             String[] guestPermissions)
166         throws PortalException, SystemException {
167 
168         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
169             proposalId);
170 
171         addProposalResources(proposal, communityPermissions, guestPermissions);
172     }
173 
174     public void addProposalResources(
175             TasksProposal proposal, String[] communityPermissions,
176             String[] guestPermissions)
177         throws PortalException, SystemException {
178 
179         resourceLocalService.addModelResources(
180             proposal.getCompanyId(), proposal.getGroupId(),
181             proposal.getUserId(), TasksProposal.class.getName(),
182             proposal.getProposalId(), communityPermissions, guestPermissions);
183     }
184 
185     public void deleteProposal(String className, String classPK)
186         throws PortalException, SystemException {
187 
188         long classNameId = PortalUtil.getClassNameId(className);
189 
190         deleteProposal(classNameId, classPK);
191     }
192 
193     public void deleteProposal(long classNameId, String classPK)
194         throws PortalException, SystemException {
195 
196         try {
197             TasksProposal proposal = getProposal(classNameId, classPK);
198 
199             deleteProposal(proposal);
200         }
201         catch (NoSuchProposalException nspe) {
202         }
203     }
204 
205     public void deleteProposal(long proposalId)
206         throws PortalException, SystemException {
207 
208         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
209             proposalId);
210 
211         deleteProposal(proposal);
212     }
213 
214     public void deleteProposal(TasksProposal proposal)
215         throws PortalException, SystemException {
216 
217         // Reviews
218 
219         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
220 
221         // Social
222 
223         socialActivityLocalService.deleteActivities(
224             TasksProposal.class.getName(), proposal.getProposalId());
225 
226         // Message boards
227 
228         mbMessageLocalService.deleteDiscussionMessages(
229             TasksProposal.class.getName(), proposal.getProposalId());
230 
231         // Resources
232 
233         resourceLocalService.deleteResource(
234             proposal.getCompanyId(), TasksProposal.class.getName(),
235             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
236 
237         // Proposal
238 
239         tasksProposalPersistence.remove(proposal);
240     }
241 
242     public void deleteProposals(long groupId) throws SystemException {
243         List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
244             groupId);
245 
246         for (TasksProposal proposal : proposals) {
247             deleteTasksProposal(proposal);
248         }
249     }
250 
251     public TasksProposal getProposal(long proposalId)
252         throws PortalException, SystemException {
253 
254         return tasksProposalPersistence.findByPrimaryKey(proposalId);
255     }
256 
257     public TasksProposal getProposal(String className, String classPK)
258         throws PortalException, SystemException {
259 
260         long classNameId = PortalUtil.getClassNameId(className);
261 
262         return getProposal(classNameId, classPK);
263     }
264 
265     public TasksProposal getProposal(long classNameId, String classPK)
266         throws PortalException, SystemException {
267 
268         return tasksProposalPersistence.findByC_C(classNameId, classPK);
269     }
270 
271     public List<TasksProposal> getProposals(long groupId, int start, int end)
272         throws SystemException {
273 
274         return tasksProposalPersistence.findByGroupId(groupId, start, end);
275     }
276 
277     public int getProposalsCount(long groupId) throws SystemException {
278         return tasksProposalPersistence.countByGroupId(groupId);
279     }
280 
281     public List<TasksProposal> getReviewProposals(
282             long groupId, long userId, int start, int end)
283         throws SystemException {
284 
285         return tasksProposalFinder.findByG_U(groupId, userId, start, end);
286     }
287 
288     public int getReviewProposalsCount(long groupId, long userId)
289         throws SystemException {
290 
291         return tasksProposalFinder.countByG_U(groupId, userId);
292     }
293 
294     public List<TasksProposal> getUserProposals(
295             long groupId, long userId, int start, int end)
296         throws SystemException {
297 
298         return tasksProposalPersistence.findByG_U(groupId, userId, start, end);
299     }
300 
301     public int getUserProposalsCount(long groupId, long userId)
302         throws SystemException {
303 
304         return tasksProposalPersistence.countByG_U(groupId, userId);
305     }
306 
307     public TasksProposal updateProposal(
308             long userId, long proposalId, String description, int dueDateMonth,
309             int dueDateDay, int dueDateYear, int dueDateHour, int dueDateMinute)
310         throws PortalException, SystemException {
311 
312         User user = userPersistence.findByPrimaryKey(userId);
313 
314         Date dueDate = PortalUtil.getDate(
315             dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
316             user.getTimeZone(), new ProposalDueDateException());
317 
318         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
319             proposalId);
320 
321         proposal.setModifiedDate(new Date());
322         proposal.setDescription(description);
323         proposal.setDueDate(dueDate);
324 
325         tasksProposalPersistence.update(proposal, false);
326 
327         return proposal;
328     }
329 
330 }