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