1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.Validator;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portal.service.ServiceContextFactory;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
26  import com.liferay.portlet.polls.DuplicateVoteException;
27  import com.liferay.portlet.polls.NoSuchChoiceException;
28  import com.liferay.portlet.polls.NoSuchQuestionException;
29  import com.liferay.portlet.polls.QuestionChoiceException;
30  import com.liferay.portlet.polls.QuestionDescriptionException;
31  import com.liferay.portlet.polls.QuestionExpirationDateException;
32  import com.liferay.portlet.polls.QuestionExpiredException;
33  import com.liferay.portlet.polls.QuestionTitleException;
34  import com.liferay.portlet.polls.model.PollsChoice;
35  import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
36  import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Calendar;
40  import java.util.Enumeration;
41  import java.util.List;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditQuestionAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   */
58  public class EditQuestionAction extends PortletAction {
59  
60      public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
61  
62      public static final String CHOICE_NAME_PREFIX = "choiceName";
63  
64      public void processAction(
65              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
66              ActionRequest actionRequest, ActionResponse actionResponse)
67          throws Exception {
68  
69          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
70  
71          try {
72              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
73                  updateQuestion(actionRequest);
74              }
75              else if (cmd.equals(Constants.DELETE)) {
76                  deleteQuestion(actionRequest);
77              }
78  
79              if (Validator.isNotNull(cmd)) {
80                  sendRedirect(actionRequest, actionResponse);
81              }
82          }
83          catch (Exception e) {
84              if (e instanceof NoSuchQuestionException ||
85                  e instanceof PrincipalException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88  
89                  setForward(actionRequest, "portlet.polls.error");
90              }
91              else if (e instanceof DuplicateVoteException ||
92                       e instanceof NoSuchChoiceException ||
93                       e instanceof QuestionChoiceException ||
94                       e instanceof QuestionDescriptionException ||
95                       e instanceof QuestionExpirationDateException ||
96  
97                       e instanceof QuestionTitleException) {
98  
99                  SessionErrors.add(actionRequest, e.getClass().getName());
100             }
101             else if (e instanceof QuestionExpiredException) {
102             }
103             else {
104                 throw e;
105             }
106         }
107     }
108 
109     public ActionForward render(
110             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111             RenderRequest renderRequest, RenderResponse renderResponse)
112         throws Exception {
113 
114         try {
115             ActionUtil.getQuestion(renderRequest);
116         }
117         catch (Exception e) {
118             if (e instanceof NoSuchQuestionException ||
119                 e instanceof PrincipalException) {
120 
121                 SessionErrors.add(renderRequest, e.getClass().getName());
122 
123                 return mapping.findForward("portlet.polls.error");
124             }
125             else {
126                 throw e;
127             }
128         }
129 
130         return mapping.findForward(
131             getForward(renderRequest, "portlet.polls.edit_question"));
132     }
133 
134     protected void deleteQuestion(ActionRequest actionRequest)
135         throws Exception {
136 
137         long questionId = ParamUtil.getLong(actionRequest, "questionId");
138 
139         PollsQuestionServiceUtil.deleteQuestion(questionId);
140 
141     }
142 
143     protected void updateQuestion(ActionRequest actionRequest)
144         throws Exception {
145 
146         long questionId = ParamUtil.getLong(actionRequest, "questionId");
147 
148         String title = ParamUtil.getString(actionRequest, "title");
149         String description = ParamUtil.getString(actionRequest, "description");
150 
151         int expirationDateMonth = ParamUtil.getInteger(
152             actionRequest, "expirationDateMonth");
153         int expirationDateDay = ParamUtil.getInteger(
154             actionRequest, "expirationDateDay");
155         int expirationDateYear = ParamUtil.getInteger(
156             actionRequest, "expirationDateYear");
157         int expirationDateHour = ParamUtil.getInteger(
158             actionRequest, "expirationDateHour");
159         int expirationDateMinute = ParamUtil.getInteger(
160             actionRequest, "expirationDateMinute");
161         int expirationDateAmPm = ParamUtil.getInteger(
162             actionRequest, "expirationDateAmPm");
163         boolean neverExpire = ParamUtil.getBoolean(
164             actionRequest, "neverExpire");
165 
166         if (expirationDateAmPm == Calendar.PM) {
167             expirationDateHour += 12;
168         }
169 
170         List<PollsChoice> choices = new ArrayList<PollsChoice>();
171 
172         Enumeration<String> enu = actionRequest.getParameterNames();
173 
174         while (enu.hasMoreElements()) {
175             String param = enu.nextElement();
176 
177             if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
178                 try {
179                     String id = param.substring(
180                         CHOICE_DESCRIPTION_PREFIX.length(), param.length());
181 
182                     String choiceName = ParamUtil.getString(
183                         actionRequest, CHOICE_NAME_PREFIX + id);
184                     String choiceDescription = ParamUtil.getString(
185                         actionRequest, param);
186 
187                     PollsChoice choice = PollsChoiceUtil.create(0);
188 
189                     choice.setName(choiceName);
190                     choice.setDescription(choiceDescription);
191 
192                     choices.add(choice);
193                 }
194                 catch (Exception e) {
195                 }
196             }
197         }
198 
199         ServiceContext serviceContext = ServiceContextFactory.getInstance(
200             BookmarksEntry.class.getName(), actionRequest);
201 
202         if (questionId <= 0) {
203 
204             // Add question
205 
206             PollsQuestionServiceUtil.addQuestion(
207                 title, description, expirationDateMonth, expirationDateDay,
208                 expirationDateYear, expirationDateHour, expirationDateMinute,
209                 neverExpire, choices, serviceContext);
210         }
211         else {
212 
213             // Update question
214 
215             PollsQuestionServiceUtil.updateQuestion(
216                 questionId, title, description, expirationDateMonth,
217                 expirationDateDay, expirationDateYear, expirationDateHour,
218                 expirationDateMinute, neverExpire, choices, serviceContext);
219         }
220     }
221 
222 }