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         // Social
127 
128         socialActivityLocalService.addActivity(
129             userId, groupId, TasksProposal.class.getName(), proposalId,
130             TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
131 
132         return proposal;
133     }
134 
135     public void addProposalResources(
136             long proposalId, boolean addCommunityPermissions,
137             boolean addGuestPermissions)
138         throws PortalException, SystemException {
139 
140         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
141             proposalId);
142 
143         addProposalResources(
144             proposal, addCommunityPermissions, addGuestPermissions);
145     }
146 
147     public void addProposalResources(
148             TasksProposal proposal, boolean addCommunityPermissions,
149             boolean addGuestPermissions)
150         throws PortalException, SystemException {
151 
152         resourceLocalService.addResources(
153             proposal.getCompanyId(), proposal.getGroupId(),
154             proposal.getUserId(), TasksProposal.class.getName(),
155             proposal.getProposalId(), false, addCommunityPermissions,
156             addGuestPermissions);
157     }
158 
159     public void addProposalResources(
160             long proposalId, String[] communityPermissions,
161             String[] guestPermissions)
162         throws PortalException, SystemException {
163 
164         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
165             proposalId);
166 
167         addProposalResources(proposal, communityPermissions, guestPermissions);
168     }
169 
170     public void addProposalResources(
171             TasksProposal proposal, String[] communityPermissions,
172             String[] guestPermissions)
173         throws PortalException, SystemException {
174 
175         resourceLocalService.addModelResources(
176             proposal.getCompanyId(), proposal.getGroupId(),
177             proposal.getUserId(), TasksProposal.class.getName(),
178             proposal.getProposalId(), communityPermissions, guestPermissions);
179     }
180 
181     public void deleteProposal(String className, String classPK)
182         throws PortalException, SystemException {
183 
184         long classNameId = PortalUtil.getClassNameId(className);
185 
186         deleteProposal(classNameId, classPK);
187     }
188 
189     public void deleteProposal(long classNameId, String classPK)
190         throws PortalException, SystemException {
191 
192         try {
193             TasksProposal proposal = getProposal(classNameId, classPK);
194 
195             deleteProposal(proposal);
196         }
197         catch (NoSuchProposalException nspe) {
198         }
199     }
200 
201     public void deleteProposal(long proposalId)
202         throws PortalException, SystemException {
203 
204         TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
205             proposalId);
206 
207         deleteProposal(proposal);
208     }
209 
210     public void deleteProposal(TasksProposal proposal)
211         throws PortalException, SystemException {
212 
213         // Reviews
214 
215         tasksReviewLocalService.deleteReviews(proposal.getProposalId());
216 
217         // Social
218 
219         socialActivityLocalService.deleteActivities(
220             TasksProposal.class.getName(), proposal.getProposalId());
221 
222         // Message boards
223 
224         mbMessageLocalService.deleteDiscussionMessages(
225             TasksProposal.class.getName(), proposal.getProposalId());
226 
227         // Resources
228 
229         resourceLocalService.deleteResource(
230             proposal.getCompanyId(), TasksProposal.class.getName(),
231             ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
232 
233         // Proposal
234 
235         tasksProposalPersistence.remove(proposal);
236     }
237 
238     public void deleteProposals(long groupId) throws SystemException {
239         List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
240             groupId);
241 
242         for (TasksProposal proposal : proposals) {
243             deleteTasksProposal(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(String className, String classPK)
254         throws PortalException, SystemException {
255 
256         long classNameId = PortalUtil.getClassNameId(className);
257 
258         return getProposal(classNameId, classPK);
259     }
260 
261     public TasksProposal getProposal(long classNameId, String classPK)
262         throws PortalException, SystemException {
263 
264         return tasksProposalPersistence.findByC_C(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 }