001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.tasks.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.tasks.NoSuchProposalException;
026    import com.liferay.portlet.tasks.ProposalDueDateException;
027    import com.liferay.portlet.tasks.model.TasksProposal;
028    import com.liferay.portlet.tasks.service.base.TasksProposalLocalServiceBaseImpl;
029    import com.liferay.portlet.tasks.social.TasksActivityKeys;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Raymond Augé
036     * @author Brian Wing Shun Chan
037     */
038    public class TasksProposalLocalServiceImpl
039            extends TasksProposalLocalServiceBaseImpl {
040    
041            public TasksProposal addProposal(
042                            long userId, long groupId, String className, String classPK,
043                            String name, String description, long reviewUserId,
044                            boolean addCommunityPermissions, boolean addGuestPermissions)
045                    throws PortalException, SystemException {
046    
047                    return addProposal(
048                            userId, groupId, className, classPK, name, description,
049                            reviewUserId, Boolean.valueOf(addCommunityPermissions),
050                            Boolean.valueOf(addGuestPermissions), null, null);
051            }
052    
053            public TasksProposal addProposal(
054                            long userId, long groupId, String className, String classPK,
055                            String name, String description, long reviewUserId,
056                            Boolean addCommunityPermissions, Boolean addGuestPermissions,
057                            String[] communityPermissions, String[] guestPermissions)
058                    throws PortalException, SystemException {
059    
060                    // Proposal
061    
062                    User user = userPersistence.findByPrimaryKey(userId);
063                    long classNameId = PortalUtil.getClassNameId(className);
064                    Date now = new Date();
065    
066                    long proposalId = counterLocalService.increment();
067    
068                    TasksProposal proposal = tasksProposalPersistence.create(proposalId);
069    
070                    proposal.setGroupId(groupId);
071                    proposal.setCompanyId(user.getCompanyId());
072                    proposal.setUserId(user.getUserId());
073                    proposal.setUserName(user.getFullName());
074                    proposal.setCreateDate(now);
075                    proposal.setModifiedDate(now);
076                    proposal.setClassNameId(classNameId);
077                    proposal.setClassPK(classPK);
078                    proposal.setName(name);
079                    proposal.setDescription(description);
080    
081                    proposal = tasksProposalPersistence.update(proposal, false);
082    
083                    // Resources
084    
085                    if ((addCommunityPermissions != null) &&
086                            (addGuestPermissions != null)) {
087    
088                            addProposalResources(
089                                    proposal, addCommunityPermissions.booleanValue(),
090                                    addGuestPermissions.booleanValue());
091                    }
092                    else {
093                            addProposalResources(
094                                    proposal, communityPermissions, guestPermissions);
095                    }
096    
097                    // Review
098    
099                    long assignedByUserId = userId;
100                    int stage = 1;
101    
102                    tasksReviewLocalService.addReview(
103                            reviewUserId, proposal.getProposalId(), assignedByUserId, stage);
104    
105                    // Message boards
106    
107                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
108    
109                    WorkflowThreadLocal.setEnabled(false);
110    
111                    try {
112                            mbMessageLocalService.addDiscussionMessage(
113                                    userId, proposal.getUserName(), groupId,
114                                    TasksProposal.class.getName(), proposalId,
115                                    WorkflowConstants.ACTION_PUBLISH);
116                    }
117                    finally {
118                            WorkflowThreadLocal.setEnabled(workflowEnabled);
119                    }
120    
121                    // Social
122    
123                    socialActivityLocalService.addActivity(
124                            userId, groupId, TasksProposal.class.getName(), proposalId,
125                            TasksActivityKeys.ADD_PROPOSAL, StringPool.BLANK, 0);
126    
127                    return proposal;
128            }
129    
130            public TasksProposal addProposal(
131                            long userId, long groupId, String className, String classPK,
132                            String name, String description, long reviewUserId,
133                            String[] communityPermissions, String[] guestPermissions)
134                    throws PortalException, SystemException {
135    
136                    return addProposal(
137                            userId, groupId, className, classPK, name, description,
138                            reviewUserId, null, null, communityPermissions, guestPermissions);
139            }
140    
141            public void addProposalResources(
142                            long proposalId, boolean addCommunityPermissions,
143                            boolean addGuestPermissions)
144                    throws PortalException, SystemException {
145    
146                    TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
147                            proposalId);
148    
149                    addProposalResources(
150                            proposal, addCommunityPermissions, addGuestPermissions);
151            }
152    
153            public void addProposalResources(
154                            long proposalId, String[] communityPermissions,
155                            String[] guestPermissions)
156                    throws PortalException, SystemException {
157    
158                    TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
159                            proposalId);
160    
161                    addProposalResources(proposal, communityPermissions, guestPermissions);
162            }
163    
164            public void addProposalResources(
165                            TasksProposal proposal, boolean addCommunityPermissions,
166                            boolean addGuestPermissions)
167                    throws PortalException, SystemException {
168    
169                    resourceLocalService.addResources(
170                            proposal.getCompanyId(), proposal.getGroupId(),
171                            proposal.getUserId(), TasksProposal.class.getName(),
172                            proposal.getProposalId(), false, addCommunityPermissions,
173                            addGuestPermissions);
174            }
175    
176            public void addProposalResources(
177                            TasksProposal proposal, String[] communityPermissions,
178                            String[] guestPermissions)
179                    throws PortalException, SystemException {
180    
181                    resourceLocalService.addModelResources(
182                            proposal.getCompanyId(), proposal.getGroupId(),
183                            proposal.getUserId(), TasksProposal.class.getName(),
184                            proposal.getProposalId(), communityPermissions, guestPermissions);
185            }
186    
187            public void deleteProposal(long proposalId)
188                    throws PortalException, SystemException {
189    
190                    TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
191                            proposalId);
192    
193                    deleteProposal(proposal);
194            }
195    
196            public void deleteProposal(long classNameId, String classPK)
197                    throws PortalException, SystemException {
198    
199                    try {
200                            TasksProposal proposal = getProposal(classNameId, classPK);
201    
202                            deleteProposal(proposal);
203                    }
204                    catch (NoSuchProposalException nspe) {
205                    }
206            }
207    
208            public void deleteProposal(String className, String classPK)
209                    throws PortalException, SystemException {
210    
211                    long classNameId = PortalUtil.getClassNameId(className);
212    
213                    deleteProposal(classNameId, classPK);
214            }
215    
216            public void deleteProposal(TasksProposal proposal)
217                    throws PortalException, SystemException {
218    
219                    // Proposal
220    
221                    tasksProposalPersistence.remove(proposal);
222    
223                    // Resources
224    
225                    resourceLocalService.deleteResource(
226                            proposal.getCompanyId(), TasksProposal.class.getName(),
227                            ResourceConstants.SCOPE_INDIVIDUAL, proposal.getProposalId());
228    
229                    // Reviews
230    
231                    tasksReviewLocalService.deleteReviews(proposal.getProposalId());
232    
233                    // Message boards
234    
235                    mbMessageLocalService.deleteDiscussionMessages(
236                            TasksProposal.class.getName(), proposal.getProposalId());
237    
238                    // Social
239    
240                    socialActivityLocalService.deleteActivities(
241                            TasksProposal.class.getName(), proposal.getProposalId());
242            }
243    
244            public void deleteProposals(long groupId)
245                    throws PortalException, SystemException {
246    
247                    List<TasksProposal> proposals = tasksProposalPersistence.findByGroupId(
248                            groupId);
249    
250                    for (TasksProposal proposal : proposals) {
251                            deleteProposal(proposal);
252                    }
253            }
254    
255            public TasksProposal getProposal(long proposalId)
256                    throws PortalException, SystemException {
257    
258                    return tasksProposalPersistence.findByPrimaryKey(proposalId);
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 TasksProposal getProposal(String className, String classPK)
268                    throws PortalException, SystemException {
269    
270                    long classNameId = PortalUtil.getClassNameId(className);
271    
272                    return getProposal(classNameId, classPK);
273            }
274    
275            public List<TasksProposal> getProposals(long groupId, int start, int end)
276                    throws SystemException {
277    
278                    return tasksProposalPersistence.findByGroupId(groupId, start, end);
279            }
280    
281            public int getProposalsCount(long groupId) throws SystemException {
282                    return tasksProposalPersistence.countByGroupId(groupId);
283            }
284    
285            public List<TasksProposal> getReviewProposals(
286                            long groupId, long userId, int start, int end)
287                    throws SystemException {
288    
289                    return tasksProposalFinder.findByG_U(groupId, userId, start, end);
290            }
291    
292            public int getReviewProposalsCount(long groupId, long userId)
293                    throws SystemException {
294    
295                    return tasksProposalFinder.countByG_U(groupId, userId);
296            }
297    
298            public List<TasksProposal> getUserProposals(
299                            long groupId, long userId, int start, int end)
300                    throws SystemException {
301    
302                    return tasksProposalPersistence.findByG_U(groupId, userId, start, end);
303            }
304    
305            public int getUserProposalsCount(long groupId, long userId)
306                    throws SystemException {
307    
308                    return tasksProposalPersistence.countByG_U(groupId, userId);
309            }
310    
311            public TasksProposal updateProposal(
312                            long userId, long proposalId, String description, int dueDateMonth,
313                            int dueDateDay, int dueDateYear, int dueDateHour, int dueDateMinute)
314                    throws PortalException, SystemException {
315    
316                    User user = userPersistence.findByPrimaryKey(userId);
317    
318                    Date dueDate = PortalUtil.getDate(
319                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
320                            user.getTimeZone(), new ProposalDueDateException());
321    
322                    TasksProposal proposal = tasksProposalPersistence.findByPrimaryKey(
323                            proposalId);
324    
325                    proposal.setModifiedDate(new Date());
326                    proposal.setDescription(description);
327                    proposal.setDueDate(dueDate);
328    
329                    tasksProposalPersistence.update(proposal, false);
330    
331                    return proposal;
332            }
333    
334    }