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