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