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