1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Group;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.model.PortletConstants;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.service.PortletLocalServiceUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.communities.util.StagingUtil;
36  import com.liferay.portlet.tasks.DuplicateReviewUserIdException;
37  import com.liferay.portlet.tasks.NoSuchProposalException;
38  import com.liferay.portlet.tasks.ProposalDueDateException;
39  import com.liferay.portlet.tasks.ProposalPublishDateException;
40  import com.liferay.portlet.tasks.model.TasksProposal;
41  import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
42  import com.liferay.portlet.tasks.service.TasksProposalServiceUtil;
43  import com.liferay.portlet.tasks.service.TasksReviewServiceUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditProposalAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Raymond Augé
59   */
60  public class EditProposalAction extends EditPagesAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              checkPermissions(actionRequest);
69          }
70          catch (PrincipalException pe) {
71              return;
72          }
73  
74          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
75  
76          try {
77              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
78                  updateProposal(actionRequest, actionResponse);
79              }
80              else if (cmd.equals(Constants.APPROVE)) {
81                  approveReview(actionRequest);
82              }
83              else if (cmd.equals(Constants.DELETE)) {
84                  deleteProposal(actionRequest);
85              }
86              else if (cmd.equals(Constants.PUBLISH)) {
87                  publishProposal(actionRequest);
88              }
89              else if (cmd.equals(Constants.REJECT)) {
90                  rejectReview(actionRequest);
91              }
92  
93              String redirect = ParamUtil.getString(
94                  actionRequest, "pagesRedirect");
95  
96              if (Validator.isNull(redirect)) {
97                  redirect = ParamUtil.getString(actionRequest, "redirect");
98              }
99  
100             sendRedirect(actionRequest, actionResponse, redirect);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchProposalException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(actionRequest, e.getClass().getName());
107 
108                 setForward(actionRequest, "portlet.communities.error");
109             }
110             else if (e instanceof DuplicateReviewUserIdException ||
111                      e instanceof ProposalDueDateException ||
112                      e instanceof ProposalPublishDateException) {
113 
114                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
115             }
116             else {
117                 throw e;
118             }
119         }
120     }
121 
122     public ActionForward render(
123             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
124             RenderRequest renderRequest, RenderResponse renderResponse)
125         throws Exception {
126 
127         try {
128             checkPermissions(renderRequest);
129         }
130         catch (PrincipalException pe) {
131             SessionErrors.add(
132                 renderRequest, PrincipalException.class.getName());
133 
134             return mapping.findForward("portlet.communities.error");
135         }
136 
137         try {
138             ActionUtil.getGroup(renderRequest);
139 
140             long proposalId = ParamUtil.getLong(renderRequest, "proposalId");
141 
142             TasksProposal proposal = null;
143 
144             if (proposalId > 0) {
145                 proposal = TasksProposalLocalServiceUtil.getProposal(
146                     proposalId);
147             }
148 
149             renderRequest.setAttribute(WebKeys.TASKS_PROPOSAL, proposal);
150         }
151         catch (Exception e) {
152             if (e instanceof NoSuchProposalException ||
153                 e instanceof PrincipalException) {
154 
155                 SessionErrors.add(renderRequest, e.getClass().getName());
156 
157                 return mapping.findForward("portlet.communities.error");
158             }
159             else {
160                 throw e;
161             }
162         }
163 
164         return mapping.findForward(
165             getForward(renderRequest, "portlet.communities.edit_proposal"));
166     }
167 
168     protected void approveReview(ActionRequest actionRequest) throws Exception {
169         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
170 
171         int stage = ParamUtil.getInteger(actionRequest, "stage");
172 
173         TasksReviewServiceUtil.approveReview(proposalId, stage);
174     }
175 
176     protected void deleteProposal(ActionRequest actionRequest)
177         throws Exception {
178 
179         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
180 
181         TasksProposalServiceUtil.deleteProposal(proposalId);
182     }
183 
184     protected void publishProposal(ActionRequest actionRequest)
185         throws Exception {
186 
187         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
188 
189         TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
190             proposalId);
191 
192         String className = PortalUtil.getClassName(proposal.getClassNameId());
193 
194         if (className.equals(Layout.class.getName())) {
195             StagingUtil.publishToLive(actionRequest);
196         }
197         else if (className.equals(Portlet.class.getName())) {
198             String classPK = proposal.getClassPK();
199 
200             String portletId = classPK.substring(
201                 classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
202                     PortletConstants.LAYOUT_SEPARATOR.length());
203 
204             Portlet portlet = PortletLocalServiceUtil.getPortletById(
205                 proposal.getCompanyId(), portletId);
206 
207             StagingUtil.publishToLive(actionRequest, portlet);
208         }
209 
210         TasksProposalServiceUtil.deleteProposal(proposal.getProposalId());
211     }
212 
213     protected void rejectReview(ActionRequest actionRequest) throws Exception {
214         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
215 
216         int stage = ParamUtil.getInteger(actionRequest, "stage");
217 
218         TasksReviewServiceUtil.rejectReview(proposalId, stage);
219     }
220 
221     protected void updateProposal(
222             ActionRequest actionRequest, ActionResponse actionResponse)
223         throws Exception {
224 
225         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
226             WebKeys.THEME_DISPLAY);
227 
228         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
229 
230         String description = ParamUtil.getString(actionRequest, "description");
231 
232         if (proposalId <= 0) {
233             long groupId = ParamUtil.getLong(actionRequest, "groupId");
234 
235             long reviewUserId = ParamUtil.getLong(
236                 actionRequest, "reviewUserId");
237 
238             String className = ParamUtil.getString(actionRequest, "className");
239             String classPK = ParamUtil.getString(actionRequest, "classPK");
240 
241             String name = StringPool.BLANK;
242 
243             if (className.equals(Layout.class.getName())) {
244                 long plid = GetterUtil.getLong(classPK);
245 
246                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
247 
248                 name = layout.getName(themeDisplay.getLocale());
249             }
250             else if (className.equals(Portlet.class.getName())) {
251                 String portletId = classPK.substring(
252                     classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
253                         PortletConstants.LAYOUT_SEPARATOR.length());
254 
255                 name = PortalUtil.getPortletTitle(
256                     portletId, themeDisplay.getLocale());
257             }
258 
259             boolean addCommunityPermissions = true;
260             boolean addGuestPermissions = true;
261 
262             TasksProposalServiceUtil.addProposal(
263                 groupId, className, classPK, name, description, reviewUserId,
264                 addCommunityPermissions, addGuestPermissions);
265         }
266         else {
267             int dueDateMonth = ParamUtil.getInteger(
268                 actionRequest, "dueDateMonth");
269             int dueDateDay = ParamUtil.getInteger(
270                 actionRequest, "dueDateDay");
271             int dueDateYear = ParamUtil.getInteger(
272                 actionRequest, "dueDateYear");
273             int dueDateHour = ParamUtil.getInteger(
274                 actionRequest, "dueDateHour");
275             int dueDateMinute = ParamUtil.getInteger(
276                 actionRequest, "dueDateMinute");
277 
278             TasksProposalServiceUtil.updateProposal(
279                 proposalId, description, dueDateMonth, dueDateDay, dueDateYear,
280                 dueDateHour, dueDateMinute);
281 
282             long groupId = ParamUtil.getLong(actionRequest, "groupId");
283 
284             Group group = GroupLocalServiceUtil.getGroup(groupId);
285 
286             int workflowStages = group.getWorkflowStages();
287 
288             long[][] userIdsPerStage = new long[workflowStages][0];
289 
290             for (int i = 2; i <= workflowStages; i++) {
291                 long[] userIds = StringUtil.split(ParamUtil.getString(
292                     actionRequest, "reviewUserIds_" + i), 0L);
293 
294                 userIdsPerStage[i - 2] = userIds;
295             }
296 
297             TasksReviewServiceUtil.updateReviews(proposalId, userIdsPerStage);
298         }
299     }
300 
301 }