1   /**
2    * Copyright (c) 2000-2008 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         // Social
127 
128         socialActivityLocalService.addActivity(
129             user.getUserId(), groupId, TasksProposal.class.getName(),
130             proposal.getProposalId(), TasksActivityKeys.PROPOSE,
131             StringPool.BLANK, 0);
132 
133         return proposal;
134     }
135 
136     public void addProposalResources(
137             long proposalId, boolean addCommunityPermissions,
138             boolean addGuestPermissions)
139         throws PortalException, SystemException {
140 
141         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
142             proposalId);
143 
144         addProposalResources(
145             proposal, addCommunityPermissions, addGuestPermissions);
146     }
147 
148     public void addProposalResources(
149             TasksProposal proposal, boolean addCommunityPermissions,
150             boolean addGuestPermissions)
151         throws PortalException, SystemException {
152 
153         resourceLocalService.addResources(
154             proposal.getCompanyId(), proposal.getGroupId(),
155             proposal.getUserId(), TasksProposal.class.getName(),
156             proposal.getProposalId(), false, addCommunityPermissions,
157             addGuestPermissions);
158     }
159 
160     public void addProposalResources(
161             long proposalId, String[] communityPermissions,
162             String[] guestPermissions)
163         throws PortalException, SystemException {
164 
165         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
166             proposalId);
167 
168         addProposalResources(proposal, communityPermissions, guestPermissions);
169     }
170 
171     public void addProposalResources(
172             TasksProposal proposal, String[] communityPermissions,
173             String[] guestPermissions)
174         throws PortalException, SystemException {
175 
176         resourceLocalService.addModelResources(
177             proposal.getCompanyId(), proposal.getGroupId(),
178             proposal.getUserId(), TasksProposal.class.getName(),
179             proposal.getProposalId(), communityPermissions, guestPermissions);
180     }
181 
182     public void deleteProposal(String className, String classPK)
183         throws PortalException, SystemException {
184 
185         long classNameId = PortalUtil.getClassNameId(className);
186 
187         deleteProposal(classNameId, classPK);
188     }
189 
190     public void deleteProposal(long classNameId, String classPK)
191         throws PortalException, SystemException {
192 
193         try {
194             TasksProposal proposal = getProposal(classNameId, classPK);
195 
196             deleteProposal(proposal);
197         }
198         catch (NoSuchProposalException nspe) {
199         }
200     }
201 
202     public void deleteProposal(long proposalId)
203         throws PortalException, SystemException {
204 
205         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
206             proposalId);
207 
208         deleteProposal(proposal);
209     }
210 
211     public void deleteProposal(TasksProposal proposal)
212         throws PortalException, SystemException {
213 
214         // Reviews
215 
216         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
217 
218         // Social
219 
220         socialActivityLocalService.deleteActivities(
221             TasksProposal.class.getName(), proposal.getProposalId());
222 
223         // Message boards
224 
225         mbMessageLocalService.deleteDiscussionMessages(
226             TasksProposal.class.getName(), proposal.getProposalId());
227 
228         // Resources
229 
230         resourceLocalService.deleteResource(
231             proposal.getCompanyId(), TasksProposal.class.getName(),
232             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
233 
234         // Proposal
235 
236         tasksProposalPersistence.remove(proposal);
237     }
238 
239     public void deleteProposals(long groupId)
240         throws PortalException, SystemException {
241 
242         List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
243             groupId);
244 
245         for (TasksProposal proposal : proposals) {
246             deleteTasksProposal(proposal);
247         }
248     }
249 
250     public TasksProposal getProposal(long proposalId)
251         throws PortalException, SystemException {
252 
253         return tasksProposalPersistence.findByPrimaryKey(proposalId);
254     }
255 
256     public TasksProposal getProposal(String className, String classPK)
257         throws PortalException, SystemException {
258 
259         long classNameId = PortalUtil.getClassNameId(className);
260 
261         return getProposal(classNameId, classPK);
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 List<TasksProposal> getProposals(long groupId, int begin, int end)
271         throws SystemException {
272 
273         return tasksProposalPersistence.findByGroupId(groupId, begin, end);
274     }
275 
276     public int getProposalsCount(long groupId) throws SystemException {
277         return tasksProposalPersistence.countByGroupId(groupId);
278     }
279 
280     public List<TasksProposal> getReviewProposals(
281             long groupId, long userId, int begin, int end)
282         throws SystemException {
283 
284         return tasksProposalFinder.findByG_U(groupId, userId, begin, end);
285     }
286 
287     public int getReviewProposalsCount(long groupId, long userId)
288         throws SystemException {
289 
290         return tasksProposalFinder.countByG_U(groupId, userId);
291     }
292 
293     public List<TasksProposal> getUserProposals(
294             long groupId, long userId, int begin, int end)
295         throws SystemException {
296 
297         return tasksProposalPersistence.findByG_U(groupId, userId, begin, end);
298     }
299 
300     public int getUserProposalsCount(long groupId, long userId)
301         throws SystemException {
302 
303         return tasksProposalPersistence.countByG_U(groupId, userId);
304     }
305 
306     public TasksProposal updateProposal(
307             long userId, long proposalId, String description, int dueDateMonth,
308             int dueDateDay, int dueDateYear, int dueDateHour, int dueDateMinute)
309         throws PortalException, SystemException {
310 
311         User user = userPersistence.findByPrimaryKey(userId);
312 
313         Date dueDate = PortalUtil.getDate(
314             dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
315             user.getTimeZone(), new ProposalDueDateException());
316 
317         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
318             proposalId);
319 
320         proposal.setModifiedDate(new Date());
321         proposal.setDescription(description);
322         proposal.setDueDate(dueDate);
323 
324         tasksProposalPersistence.update(proposal, false);
325 
326         return proposal;
327     }
328 
329 }