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              String[] communityPermissions, String[] guestPermissions)
66          throws PortalException, SystemException {
67  
68          return addProposal(
69              userId, groupId, className, classPK, name, description,
70              reviewUserId, null, null, communityPermissions, guestPermissions);
71      }
72  
73      public TasksProposal addProposal(
74              long userId, long groupId, String className, String classPK,
75              String name, String description, long reviewUserId,
76              Boolean addCommunityPermissions, Boolean addGuestPermissions,
77              String[] communityPermissions, String[] guestPermissions)
78          throws PortalException, SystemException {
79  
80          // Proposal
81  
82          User user = userPersistence.findByPrimaryKey(userId);
83          long classNameId = PortalUtil.getClassNameId(className);
84          Date now = new Date();
85  
86          long proposalId = counterLocalService.increment();
87  
88          TasksProposal proposal = tasksProposalPersistence.create(proposalId);
89  
90          proposal.setGroupId(groupId);
91          proposal.setCompanyId(user.getCompanyId());
92          proposal.setUserId(user.getUserId());
93          proposal.setUserName(user.getFullName());
94          proposal.setCreateDate(now);
95          proposal.setModifiedDate(now);
96          proposal.setClassNameId(classNameId);
97          proposal.setClassPK(classPK);
98          proposal.setName(name);
99          proposal.setDescription(description);
100 
101         proposal = tasksProposalPersistence.update(proposal, false);
102 
103         // Resources
104 
105         if ((addCommunityPermissions != null) &&
106             (addGuestPermissions != null)) {
107 
108             addProposalResources(
109                 proposal, addCommunityPermissions.booleanValue(),
110                 addGuestPermissions.booleanValue());
111         }
112         else {
113             addProposalResources(
114                 proposal, communityPermissions, guestPermissions);
115         }
116 
117         // Review
118 
119         long assignedByUserId = userId;
120         int stage = 1;
121 
122         tasksReviewLocalService.addReview(
123             reviewUserId, proposal.getProposalId(), assignedByUserId, stage);
124 
125         mbMessageLocalService.addDiscussionMessage(
126             userId, proposal.getUserName(), TasksProposal.class.getName(),
127             proposalId);
128 
129         // Social
130 
131         socialActivityLocalService.addActivity(
132             userId, groupId, TasksProposal.class.getName(), proposalId,
133             TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
134 
135         return proposal;
136     }
137 
138     public void addProposalResources(
139             long proposalId, boolean addCommunityPermissions,
140             boolean addGuestPermissions)
141         throws PortalException, SystemException {
142 
143         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
144             proposalId);
145 
146         addProposalResources(
147             proposal, addCommunityPermissions, addGuestPermissions);
148     }
149 
150     public void addProposalResources(
151             TasksProposal proposal, boolean addCommunityPermissions,
152             boolean addGuestPermissions)
153         throws PortalException, SystemException {
154 
155         resourceLocalService.addResources(
156             proposal.getCompanyId(), proposal.getGroupId(),
157             proposal.getUserId(), TasksProposal.class.getName(),
158             proposal.getProposalId(), false, addCommunityPermissions,
159             addGuestPermissions);
160     }
161 
162     public void addProposalResources(
163             long proposalId, String[] communityPermissions,
164             String[] guestPermissions)
165         throws PortalException, SystemException {
166 
167         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
168             proposalId);
169 
170         addProposalResources(proposal, communityPermissions, guestPermissions);
171     }
172 
173     public void addProposalResources(
174             TasksProposal proposal, String[] communityPermissions,
175             String[] guestPermissions)
176         throws PortalException, SystemException {
177 
178         resourceLocalService.addModelResources(
179             proposal.getCompanyId(), proposal.getGroupId(),
180             proposal.getUserId(), TasksProposal.class.getName(),
181             proposal.getProposalId(), communityPermissions, guestPermissions);
182     }
183 
184     public void deleteProposal(String className, String classPK)
185         throws PortalException, SystemException {
186 
187         long classNameId = PortalUtil.getClassNameId(className);
188 
189         deleteProposal(classNameId, classPK);
190     }
191 
192     public void deleteProposal(long classNameId, String classPK)
193         throws PortalException, SystemException {
194 
195         try {
196             TasksProposal proposal = getProposal(classNameId, classPK);
197 
198             deleteProposal(proposal);
199         }
200         catch (NoSuchProposalException nspe) {
201         }
202     }
203 
204     public void deleteProposal(long proposalId)
205         throws PortalException, SystemException {
206 
207         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
208             proposalId);
209 
210         deleteProposal(proposal);
211     }
212 
213     public void deleteProposal(TasksProposal proposal)
214         throws PortalException, SystemException {
215 
216         // Reviews
217 
218         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
219 
220         // Social
221 
222         socialActivityLocalService.deleteActivities(
223             TasksProposal.class.getName(), proposal.getProposalId());
224 
225         // Message boards
226 
227         mbMessageLocalService.deleteDiscussionMessages(
228             TasksProposal.class.getName(), proposal.getProposalId());
229 
230         // Resources
231 
232         resourceLocalService.deleteResource(
233             proposal.getCompanyId(), TasksProposal.class.getName(),
234             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
235 
236         // Proposal
237 
238         tasksProposalPersistence.remove(proposal);
239     }
240 
241     public void deleteProposals(long groupId) throws SystemException {
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 start, int end)
271         throws SystemException {
272 
273         return tasksProposalPersistence.findByGroupId(groupId, start, 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 start, int end)
282         throws SystemException {
283 
284         return tasksProposalFinder.findByG_U(groupId, userId, start, 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 start, int end)
295         throws SystemException {
296 
297         return tasksProposalPersistence.findByG_U(groupId, userId, start, 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 }