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.communities.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.staging.StagingUtil;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.model.PortletConstants;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.PortletLocalServiceUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.tasks.DuplicateReviewUserIdException;
037    import com.liferay.portlet.tasks.NoSuchProposalException;
038    import com.liferay.portlet.tasks.ProposalDueDateException;
039    import com.liferay.portlet.tasks.ProposalPublishDateException;
040    import com.liferay.portlet.tasks.model.TasksProposal;
041    import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
042    import com.liferay.portlet.tasks.service.TasksProposalServiceUtil;
043    import com.liferay.portlet.tasks.service.TasksReviewServiceUtil;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.RenderRequest;
049    import javax.portlet.RenderResponse;
050    
051    import org.apache.struts.action.ActionForm;
052    import org.apache.struts.action.ActionForward;
053    import org.apache.struts.action.ActionMapping;
054    
055    /**
056     * @author Raymond Augé
057     */
058    public class EditProposalAction extends EditPagesAction {
059    
060            public void processAction(
061                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    try {
066                            checkPermissions(actionRequest);
067                    }
068                    catch (PrincipalException pe) {
069                            return;
070                    }
071    
072                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073    
074                    try {
075                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
076                                    updateProposal(actionRequest, actionResponse);
077                            }
078                            else if (cmd.equals(Constants.APPROVE)) {
079                                    approveReview(actionRequest);
080                            }
081                            else if (cmd.equals(Constants.DELETE)) {
082                                    deleteProposal(actionRequest);
083                            }
084                            else if (cmd.equals(Constants.PUBLISH)) {
085                                    publishProposal(actionRequest);
086                            }
087                            else if (cmd.equals(Constants.REJECT)) {
088                                    rejectReview(actionRequest);
089                            }
090    
091                            String redirect = ParamUtil.getString(
092                                    actionRequest, "pagesRedirect");
093    
094                            if (Validator.isNull(redirect)) {
095                                    redirect = ParamUtil.getString(actionRequest, "redirect");
096                            }
097    
098                            sendRedirect(actionRequest, actionResponse, redirect);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchProposalException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(actionRequest, e.getClass().getName());
105    
106                                    setForward(actionRequest, "portlet.communities.error");
107                            }
108                            else if (e instanceof DuplicateReviewUserIdException ||
109                                             e instanceof ProposalDueDateException ||
110                                             e instanceof ProposalPublishDateException) {
111    
112                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
113                            }
114                            else {
115                                    throw e;
116                            }
117                    }
118            }
119    
120            public ActionForward render(
121                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
122                            RenderRequest renderRequest, RenderResponse renderResponse)
123                    throws Exception {
124    
125                    try {
126                            checkPermissions(renderRequest);
127                    }
128                    catch (PrincipalException pe) {
129                            SessionErrors.add(
130                                    renderRequest, PrincipalException.class.getName());
131    
132                            return mapping.findForward("portlet.communities.error");
133                    }
134    
135                    try {
136                            ActionUtil.getGroup(renderRequest);
137    
138                            long proposalId = ParamUtil.getLong(renderRequest, "proposalId");
139    
140                            TasksProposal proposal = null;
141    
142                            if (proposalId > 0) {
143                                    proposal = TasksProposalLocalServiceUtil.getProposal(
144                                            proposalId);
145                            }
146    
147                            renderRequest.setAttribute(WebKeys.TASKS_PROPOSAL, proposal);
148                    }
149                    catch (Exception e) {
150                            if (e instanceof NoSuchProposalException ||
151                                    e instanceof PrincipalException) {
152    
153                                    SessionErrors.add(renderRequest, e.getClass().getName());
154    
155                                    return mapping.findForward("portlet.communities.error");
156                            }
157                            else {
158                                    throw e;
159                            }
160                    }
161    
162                    return mapping.findForward(
163                            getForward(renderRequest, "portlet.communities.edit_proposal"));
164            }
165    
166            protected void approveReview(ActionRequest actionRequest) throws Exception {
167                    long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
168    
169                    int stage = ParamUtil.getInteger(actionRequest, "stage");
170    
171                    TasksReviewServiceUtil.approveReview(proposalId, stage);
172            }
173    
174            protected void deleteProposal(ActionRequest actionRequest)
175                    throws Exception {
176    
177                    long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
178    
179                    TasksProposalServiceUtil.deleteProposal(proposalId);
180            }
181    
182            protected void publishProposal(ActionRequest actionRequest)
183                    throws Exception {
184    
185                    long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
186    
187                    TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
188                            proposalId);
189    
190                    String className = PortalUtil.getClassName(proposal.getClassNameId());
191    
192                    if (className.equals(Layout.class.getName())) {
193                            StagingUtil.publishToLive(actionRequest);
194                    }
195                    else if (className.equals(Portlet.class.getName())) {
196                            String classPK = proposal.getClassPK();
197    
198                            String portletId = classPK.substring(
199                                    classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
200                                            PortletConstants.LAYOUT_SEPARATOR.length());
201    
202                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
203                                    proposal.getCompanyId(), portletId);
204    
205                            StagingUtil.publishToLive(actionRequest, portlet);
206                    }
207    
208                    TasksProposalServiceUtil.deleteProposal(proposal.getProposalId());
209            }
210    
211            protected void rejectReview(ActionRequest actionRequest) throws Exception {
212                    long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
213    
214                    int stage = ParamUtil.getInteger(actionRequest, "stage");
215    
216                    TasksReviewServiceUtil.rejectReview(proposalId, stage);
217            }
218    
219            protected void updateProposal(
220                            ActionRequest actionRequest, ActionResponse actionResponse)
221                    throws Exception {
222    
223                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
224                            WebKeys.THEME_DISPLAY);
225    
226                    long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
227    
228                    String description = ParamUtil.getString(actionRequest, "description");
229    
230                    if (proposalId <= 0) {
231                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
232    
233                            long reviewUserId = ParamUtil.getLong(
234                                    actionRequest, "reviewUserId");
235    
236                            String className = ParamUtil.getString(actionRequest, "className");
237                            String classPK = ParamUtil.getString(actionRequest, "classPK");
238    
239                            String name = StringPool.BLANK;
240    
241                            if (className.equals(Layout.class.getName())) {
242                                    long plid = GetterUtil.getLong(classPK);
243    
244                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
245    
246                                    name = layout.getName(themeDisplay.getLocale());
247                            }
248                            else if (className.equals(Portlet.class.getName())) {
249                                    String portletId = classPK.substring(
250                                            classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
251                                                    PortletConstants.LAYOUT_SEPARATOR.length());
252    
253                                    name = PortalUtil.getPortletTitle(
254                                            portletId, themeDisplay.getLocale());
255                            }
256    
257                            boolean addCommunityPermissions = true;
258                            boolean addGuestPermissions = true;
259    
260                            TasksProposalServiceUtil.addProposal(
261                                    groupId, className, classPK, name, description, reviewUserId,
262                                    addCommunityPermissions, addGuestPermissions);
263                    }
264                    else {
265                            int dueDateMonth = ParamUtil.getInteger(
266                                    actionRequest, "dueDateMonth");
267                            int dueDateDay = ParamUtil.getInteger(
268                                    actionRequest, "dueDateDay");
269                            int dueDateYear = ParamUtil.getInteger(
270                                    actionRequest, "dueDateYear");
271                            int dueDateHour = ParamUtil.getInteger(
272                                    actionRequest, "dueDateHour");
273                            int dueDateMinute = ParamUtil.getInteger(
274                                    actionRequest, "dueDateMinute");
275    
276                            TasksProposalServiceUtil.updateProposal(
277                                    proposalId, description, dueDateMonth, dueDateDay, dueDateYear,
278                                    dueDateHour, dueDateMinute);
279    
280                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
281    
282                            Group group = GroupLocalServiceUtil.getGroup(groupId);
283    
284                            int workflowStages = group.getWorkflowStages();
285    
286                            long[][] userIdsPerStage = new long[workflowStages][0];
287    
288                            for (int i = 2; i <= workflowStages; i++) {
289                                    long[] userIds = StringUtil.split(ParamUtil.getString(
290                                            actionRequest, "reviewUserIds_" + i), 0L);
291    
292                                    userIdsPerStage[i - 2] = userIds;
293                            }
294    
295                            TasksReviewServiceUtil.updateReviews(proposalId, userIdsPerStage);
296                    }
297            }
298    
299    }