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