1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.polls.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.polls.DuplicateVoteException;
28  import com.liferay.portlet.polls.NoSuchChoiceException;
29  import com.liferay.portlet.polls.NoSuchQuestionException;
30  import com.liferay.portlet.polls.QuestionChoiceException;
31  import com.liferay.portlet.polls.QuestionDescriptionException;
32  import com.liferay.portlet.polls.QuestionExpirationDateException;
33  import com.liferay.portlet.polls.QuestionExpiredException;
34  import com.liferay.portlet.polls.QuestionTitleException;
35  import com.liferay.portlet.polls.model.PollsChoice;
36  import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
37  import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
38  import com.liferay.util.LocalizationUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Calendar;
42  import java.util.Enumeration;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Locale;
46  import java.util.Map;
47  import java.util.Set;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.RenderRequest;
53  import javax.portlet.RenderResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="EditQuestionAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   */
64  public class EditQuestionAction extends PortletAction {
65  
66      public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
67  
68      public static final String CHOICE_NAME_PREFIX = "choiceName";
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
76  
77          try {
78              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
79                  updateQuestion(actionRequest);
80              }
81              else if (cmd.equals(Constants.DELETE)) {
82                  deleteQuestion(actionRequest);
83              }
84  
85              if (Validator.isNotNull(cmd)) {
86                  sendRedirect(actionRequest, actionResponse);
87              }
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchQuestionException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(actionRequest, e.getClass().getName());
94  
95                  setForward(actionRequest, "portlet.polls.error");
96              }
97              else if (e instanceof DuplicateVoteException ||
98                       e instanceof NoSuchChoiceException ||
99                       e instanceof QuestionChoiceException ||
100                      e instanceof QuestionDescriptionException ||
101                      e instanceof QuestionExpirationDateException ||
102 
103                      e instanceof QuestionTitleException) {
104 
105                 SessionErrors.add(actionRequest, e.getClass().getName());
106             }
107             else if (e instanceof QuestionExpiredException) {
108             }
109             else {
110                 throw e;
111             }
112         }
113     }
114 
115     public ActionForward render(
116             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
117             RenderRequest renderRequest, RenderResponse renderResponse)
118         throws Exception {
119 
120         try {
121             ActionUtil.getQuestion(renderRequest);
122         }
123         catch (Exception e) {
124             if (e instanceof NoSuchQuestionException ||
125                 e instanceof PrincipalException) {
126 
127                 SessionErrors.add(renderRequest, e.getClass().getName());
128 
129                 return mapping.findForward("portlet.polls.error");
130             }
131             else {
132                 throw e;
133             }
134         }
135 
136         return mapping.findForward(
137             getForward(renderRequest, "portlet.polls.edit_question"));
138     }
139 
140     protected void deleteQuestion(ActionRequest actionRequest)
141         throws Exception {
142 
143         long questionId = ParamUtil.getLong(actionRequest, "questionId");
144 
145         PollsQuestionServiceUtil.deleteQuestion(questionId);
146 
147     }
148 
149     protected void updateQuestion(ActionRequest actionRequest)
150         throws Exception {
151 
152         long questionId = ParamUtil.getLong(actionRequest, "questionId");
153 
154         Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
155             actionRequest, "title");
156         Map<Locale, String> descriptionMap =
157             LocalizationUtil.getLocalizationMap(
158                 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         Set<String> readParameters = new HashSet<String>();
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(),
192                         param.indexOf(StringPool.UNDERLINE));
193 
194                     if (readParameters.contains(id)) {
195                         continue;
196                     }
197 
198                     String choiceName = ParamUtil.getString(
199                         actionRequest, CHOICE_NAME_PREFIX + id);
200 
201                     Map<Locale, String> localeChoiceDescriptionMap =
202                         LocalizationUtil.getLocalizationMap(
203                             actionRequest, CHOICE_DESCRIPTION_PREFIX + id);
204 
205                     PollsChoice choice = PollsChoiceUtil.create(0);
206 
207                     choice.setName(choiceName);
208                     choice.setDescriptionMap(localeChoiceDescriptionMap);
209 
210                     choices.add(choice);
211 
212                     readParameters.add(id);
213                 }
214                 catch (Exception e) {
215                 }
216             }
217         }
218 
219         ServiceContext serviceContext = ServiceContextFactory.getInstance(
220             BookmarksEntry.class.getName(), actionRequest);
221 
222         if (questionId <= 0) {
223 
224             // Add question
225 
226             PollsQuestionServiceUtil.addQuestion(
227                 titleMap, descriptionMap, expirationDateMonth,
228                 expirationDateDay, expirationDateYear, expirationDateHour,
229                 expirationDateMinute, neverExpire, choices, serviceContext);
230         }
231         else {
232 
233             // Update question
234 
235             PollsQuestionServiceUtil.updateQuestion(
236                 questionId, titleMap, descriptionMap, expirationDateMonth,
237                 expirationDateDay, expirationDateYear, expirationDateHour,
238                 expirationDateMinute, neverExpire, choices, serviceContext);
239         }
240     }
241 
242 }