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