1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditProposalAction extends EditPagesAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          try {
76              checkPermissions(actionRequest);
77          }
78          catch (PrincipalException pe) {
79              return;
80          }
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  updateProposal(actionRequest, actionResponse);
87              }
88              else if (cmd.equals(Constants.APPROVE)) {
89                  approveReview(actionRequest);
90              }
91              else if (cmd.equals(Constants.DELETE)) {
92                  deleteProposal(actionRequest);
93              }
94              else if (cmd.equals(Constants.PUBLISH)) {
95                  publishProposal(actionRequest);
96              }
97              else if (cmd.equals(Constants.REJECT)) {
98                  rejectReview(actionRequest);
99              }
100 
101             String redirect = ParamUtil.getString(
102                 actionRequest, "pagesRedirect");
103 
104             if (Validator.isNull(redirect)) {
105                 redirect = ParamUtil.getString(actionRequest, "redirect");
106             }
107 
108             sendRedirect(actionRequest, actionResponse, redirect);
109         }
110         catch (Exception e) {
111             if (e instanceof NoSuchProposalException ||
112                 e instanceof PrincipalException) {
113 
114                 SessionErrors.add(actionRequest, e.getClass().getName());
115 
116                 setForward(actionRequest, "portlet.communities.error");
117             }
118             else if (e instanceof DuplicateReviewUserIdException ||
119                      e instanceof ProposalDueDateException ||
120                      e instanceof ProposalPublishDateException) {
121 
122                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
123             }
124             else {
125                 throw e;
126             }
127         }
128     }
129 
130     public ActionForward render(
131             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
132             RenderRequest renderRequest, RenderResponse renderResponse)
133         throws Exception {
134 
135         try {
136             checkPermissions(renderRequest);
137         }
138         catch (PrincipalException pe) {
139             SessionErrors.add(
140                 renderRequest, PrincipalException.class.getName());
141 
142             return mapping.findForward("portlet.communities.error");
143         }
144 
145         try {
146             ActionUtil.getGroup(renderRequest);
147 
148             long proposalId = ParamUtil.getLong(renderRequest, "proposalId");
149 
150             TasksProposal proposal = null;
151 
152             if (proposalId > 0) {
153                 proposal = TasksProposalLocalServiceUtil.getProposal(
154                     proposalId);
155             }
156 
157             renderRequest.setAttribute(WebKeys.TASKS_PROPOSAL, proposal);
158         }
159         catch (Exception e) {
160             if (e instanceof NoSuchProposalException ||
161                 e instanceof PrincipalException) {
162 
163                 SessionErrors.add(renderRequest, e.getClass().getName());
164 
165                 return mapping.findForward("portlet.communities.error");
166             }
167             else {
168                 throw e;
169             }
170         }
171 
172         return mapping.findForward(
173             getForward(renderRequest, "portlet.communities.edit_proposal"));
174     }
175 
176     protected void approveReview(ActionRequest actionRequest) throws Exception {
177         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
178 
179         int stage = ParamUtil.getInteger(actionRequest, "stage");
180 
181         TasksReviewServiceUtil.approveReview(proposalId, stage);
182     }
183 
184     protected void deleteProposal(ActionRequest actionRequest)
185         throws Exception {
186 
187         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
188 
189         TasksProposalServiceUtil.deleteProposal(proposalId);
190     }
191 
192     protected void publishProposal(ActionRequest actionRequest)
193         throws Exception {
194 
195         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
196 
197         TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
198             proposalId);
199 
200         String className = PortalUtil.getClassName(proposal.getClassNameId());
201 
202         if (className.equals(Layout.class.getName())) {
203             StagingUtil.publishToLive(actionRequest);
204         }
205         else if (className.equals(Portlet.class.getName())) {
206             String classPK = proposal.getClassPK();
207 
208             String portletId = classPK.substring(
209                 classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
210                     PortletConstants.LAYOUT_SEPARATOR.length());
211 
212             Portlet portlet = PortletLocalServiceUtil.getPortletById(
213                 proposal.getCompanyId(), portletId);
214 
215             StagingUtil.publishToLive(actionRequest, portlet);
216         }
217 
218         TasksProposalServiceUtil.deleteProposal(proposal.getProposalId());
219     }
220 
221     protected void rejectReview(ActionRequest actionRequest) throws Exception {
222         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
223 
224         int stage = ParamUtil.getInteger(actionRequest, "stage");
225 
226         TasksReviewServiceUtil.rejectReview(proposalId, stage);
227     }
228 
229     protected void updateProposal(
230             ActionRequest actionRequest, ActionResponse actionResponse)
231         throws Exception {
232 
233         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
234             WebKeys.THEME_DISPLAY);
235 
236         long proposalId = ParamUtil.getLong(actionRequest, "proposalId");
237 
238         String description = ParamUtil.getString(actionRequest, "description");
239 
240         if (proposalId <= 0) {
241             long groupId = ParamUtil.getLong(actionRequest, "groupId");
242 
243             long reviewUserId = ParamUtil.getLong(
244                 actionRequest, "reviewUserId");
245 
246             String className = ParamUtil.getString(actionRequest, "className");
247             String classPK = ParamUtil.getString(actionRequest, "classPK");
248 
249             String name = StringPool.BLANK;
250 
251             if (className.equals(Layout.class.getName())) {
252                 long plid = GetterUtil.getLong(classPK);
253 
254                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
255 
256                 name = layout.getName(themeDisplay.getLocale());
257             }
258             else if (className.equals(Portlet.class.getName())) {
259                 String portletId = classPK.substring(
260                     classPK.indexOf(PortletConstants.LAYOUT_SEPARATOR) +
261                         PortletConstants.LAYOUT_SEPARATOR.length());
262 
263                 name = PortalUtil.getPortletTitle(
264                     portletId, themeDisplay.getCompanyId(),
265                     themeDisplay.getLocale());
266             }
267 
268             boolean addCommunityPermissions = true;
269             boolean addGuestPermissions = true;
270 
271             TasksProposalServiceUtil.addProposal(
272                 groupId, className, classPK, name, description, reviewUserId,
273                 addCommunityPermissions, addGuestPermissions);
274         }
275         else {
276             int dueDateMonth = ParamUtil.getInteger(
277                 actionRequest, "dueDateMonth");
278             int dueDateDay = ParamUtil.getInteger(
279                 actionRequest, "dueDateDay");
280             int dueDateYear = ParamUtil.getInteger(
281                 actionRequest, "dueDateYear");
282             int dueDateHour = ParamUtil.getInteger(
283                 actionRequest, "dueDateHour");
284             int dueDateMinute = ParamUtil.getInteger(
285                 actionRequest, "dueDateMinute");
286 
287             TasksProposalServiceUtil.updateProposal(
288                 proposalId, description, dueDateMonth, dueDateDay, dueDateYear,
289                 dueDateHour, dueDateMinute);
290 
291             long groupId = ParamUtil.getLong(actionRequest, "groupId");
292 
293             Group group = GroupLocalServiceUtil.getGroup(groupId);
294 
295             int workflowStages = group.getWorkflowStages();
296 
297             long[][] userIdsPerStage = new long[workflowStages][0];
298 
299             for (int i = 2; i <= workflowStages; i++) {
300                 long[] userIds = StringUtil.split(ParamUtil.getString(
301                     actionRequest, "reviewUserIds_" + i), 0L);
302 
303                 userIdsPerStage[i - 2] = userIds;
304             }
305 
306             TasksReviewServiceUtil.updateReviews(proposalId, userIdsPerStage);
307         }
308     }
309 
310 }