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.security.auth.PrincipalException;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portal.service.ServiceContextFactory;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
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         long questionId = ParamUtil.getLong(actionRequest, "questionId");
156 
157         String title = ParamUtil.getString(actionRequest, "title");
158         String description = ParamUtil.getString(actionRequest, "description");
159 
160         int expirationDateMonth = ParamUtil.getInteger(
161             actionRequest, "expirationDateMonth");
162         int expirationDateDay = ParamUtil.getInteger(
163             actionRequest, "expirationDateDay");
164         int expirationDateYear = ParamUtil.getInteger(
165             actionRequest, "expirationDateYear");
166         int expirationDateHour = ParamUtil.getInteger(
167             actionRequest, "expirationDateHour");
168         int expirationDateMinute = ParamUtil.getInteger(
169             actionRequest, "expirationDateMinute");
170         int expirationDateAmPm = ParamUtil.getInteger(
171             actionRequest, "expirationDateAmPm");
172         boolean neverExpire = ParamUtil.getBoolean(
173             actionRequest, "neverExpire");
174 
175         if (expirationDateAmPm == Calendar.PM) {
176             expirationDateHour += 12;
177         }
178 
179         List<PollsChoice> choices = new ArrayList<PollsChoice>();
180 
181         Enumeration<String> enu = actionRequest.getParameterNames();
182 
183         while (enu.hasMoreElements()) {
184             String param = enu.nextElement();
185 
186             if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
187                 try {
188                     String id = param.substring(
189                         CHOICE_DESCRIPTION_PREFIX.length(), param.length());
190 
191                     String choiceName = ParamUtil.getString(
192                         actionRequest, CHOICE_NAME_PREFIX + id);
193                     String choiceDescription = ParamUtil.getString(
194                         actionRequest, param);
195 
196                     PollsChoice choice = PollsChoiceUtil.create(0);
197 
198                     choice.setName(choiceName);
199                     choice.setDescription(choiceDescription);
200 
201                     choices.add(choice);
202                 }
203                 catch (Exception e) {
204                 }
205             }
206         }
207 
208         ServiceContext serviceContext = ServiceContextFactory.getInstance(
209             BookmarksEntry.class.getName(), actionRequest);
210 
211         if (questionId <= 0) {
212 
213             // Add question
214 
215             PollsQuestionServiceUtil.addQuestion(
216                 title, description, expirationDateMonth, expirationDateDay,
217                 expirationDateYear, expirationDateHour, expirationDateMinute,
218                 neverExpire, choices, serviceContext);
219         }
220         else {
221 
222             // Update question
223 
224             PollsQuestionServiceUtil.updateQuestion(
225                 questionId, title, description, expirationDateMonth,
226                 expirationDateDay, expirationDateYear, expirationDateHour,
227                 expirationDateMinute, neverExpire, choices, serviceContext);
228         }
229     }
230 
231 }