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