1   /**
2    * Copyright (c) 2000-2007 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.polls.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.polls.DuplicateVoteException;
33  import com.liferay.portlet.polls.NoSuchChoiceException;
34  import com.liferay.portlet.polls.NoSuchQuestionException;
35  import com.liferay.portlet.polls.QuestionChoiceException;
36  import com.liferay.portlet.polls.QuestionDescriptionException;
37  import com.liferay.portlet.polls.QuestionExpirationDateException;
38  import com.liferay.portlet.polls.QuestionExpiredException;
39  import com.liferay.portlet.polls.QuestionTitleException;
40  import com.liferay.portlet.polls.model.PollsChoice;
41  import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
42  import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
43  import com.liferay.util.servlet.SessionErrors;
44  
45  import java.util.ArrayList;
46  import java.util.Calendar;
47  import java.util.Enumeration;
48  import java.util.List;
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="EditQuestionAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class EditQuestionAction extends PortletAction {
67  
68      public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
69  
70      public static final String CHOICE_NAME_PREFIX = "choiceName";
71  
72      public void processAction(
73              ActionMapping mapping, ActionForm form, PortletConfig config,
74              ActionRequest req, ActionResponse res)
75          throws Exception {
76  
77          String cmd = ParamUtil.getString(req, Constants.CMD);
78  
79          try {
80              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
81                  updateQuestion(req);
82              }
83              else if (cmd.equals(Constants.DELETE)) {
84                  deleteQuestion(req);
85              }
86  
87              if (Validator.isNotNull(cmd)) {
88                  sendRedirect(req, res);
89              }
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchQuestionException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(req, e.getClass().getName());
96  
97                  setForward(req, "portlet.polls.error");
98              }
99              else if (e instanceof DuplicateVoteException ||
100                      e instanceof NoSuchChoiceException ||
101                      e instanceof QuestionChoiceException ||
102                      e instanceof QuestionDescriptionException ||
103                      e instanceof QuestionExpirationDateException ||
104 
105                      e instanceof QuestionTitleException) {
106 
107                 SessionErrors.add(req, e.getClass().getName());
108             }
109             else if (e instanceof QuestionExpiredException) {
110             }
111             else {
112                 throw e;
113             }
114         }
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig config,
119             RenderRequest req, RenderResponse res)
120         throws Exception {
121 
122         try {
123             ActionUtil.getQuestion(req);
124         }
125         catch (Exception e) {
126             if (e instanceof NoSuchQuestionException ||
127                 e instanceof PrincipalException) {
128 
129                 SessionErrors.add(req, e.getClass().getName());
130 
131                 return mapping.findForward("portlet.polls.error");
132             }
133             else {
134                 throw e;
135             }
136         }
137 
138         return mapping.findForward(
139             getForward(req, "portlet.polls.edit_question"));
140     }
141 
142     protected void deleteQuestion(ActionRequest req) throws Exception {
143         long questionId = ParamUtil.getLong(req, "questionId");
144 
145         PollsQuestionServiceUtil.deleteQuestion(questionId);
146 
147     }
148 
149     protected void updateQuestion(ActionRequest req) throws Exception {
150         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
151 
152         long questionId = ParamUtil.getLong(req, "questionId");
153 
154         String title = ParamUtil.getString(req, "title");
155         String description = ParamUtil.getString(req, "description");
156 
157         int expirationDateMonth = ParamUtil.getInteger(
158             req, "expirationDateMonth");
159         int expirationDateDay = ParamUtil.getInteger(req, "expirationDateDay");
160         int expirationDateYear = ParamUtil.getInteger(
161             req, "expirationDateYear");
162         int expirationDateHour = ParamUtil.getInteger(
163             req, "expirationDateHour");
164         int expirationDateMinute = ParamUtil.getInteger(
165             req, "expirationDateMinute");
166         int expirationDateAmPm = ParamUtil.getInteger(
167             req, "expirationDateAmPm");
168         boolean neverExpire = ParamUtil.getBoolean(req, "neverExpire");
169 
170         if (expirationDateAmPm == Calendar.PM) {
171             expirationDateHour += 12;
172         }
173 
174         List choices = new ArrayList();
175 
176         Enumeration enu = req.getParameterNames();
177 
178         while (enu.hasMoreElements()) {
179             String param = (String)enu.nextElement();
180 
181             if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
182                 try {
183                     String id = param.substring(
184                         CHOICE_DESCRIPTION_PREFIX.length(), param.length());
185 
186                     String choiceName = ParamUtil.getString(
187                         req, CHOICE_NAME_PREFIX + id);
188                     String choiceDescription = ParamUtil.getString(req, param);
189 
190                     PollsChoice choice = PollsChoiceUtil.create(0);
191 
192                     choice.setName(choiceName);
193                     choice.setDescription(choiceDescription);
194 
195                     choices.add(choice);
196                 }
197                 catch (Exception e) {
198                 }
199             }
200         }
201 
202         String[] communityPermissions = req.getParameterValues(
203             "communityPermissions");
204         String[] guestPermissions = req.getParameterValues(
205             "guestPermissions");
206 
207         if (questionId <= 0) {
208 
209             // Add question
210 
211             PollsQuestionServiceUtil.addQuestion(
212                 layout.getPlid(), title, description, expirationDateMonth,
213                 expirationDateDay, expirationDateYear, expirationDateHour,
214                 expirationDateMinute, neverExpire, choices,
215                 communityPermissions, guestPermissions);
216         }
217         else {
218 
219             // Update question
220 
221             PollsQuestionServiceUtil.updateQuestion(
222                 questionId, title, description, expirationDateMonth,
223                 expirationDateDay, expirationDateYear, expirationDateHour,
224                 expirationDateMinute, neverExpire, choices);
225         }
226     }
227 
228 }