1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.polls.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.service.ServiceContextFactory;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
31  import com.liferay.portlet.polls.DuplicateVoteException;
32  import com.liferay.portlet.polls.NoSuchChoiceException;
33  import com.liferay.portlet.polls.NoSuchQuestionException;
34  import com.liferay.portlet.polls.QuestionChoiceException;
35  import com.liferay.portlet.polls.QuestionDescriptionException;
36  import com.liferay.portlet.polls.QuestionExpirationDateException;
37  import com.liferay.portlet.polls.QuestionExpiredException;
38  import com.liferay.portlet.polls.QuestionTitleException;
39  import com.liferay.portlet.polls.model.PollsChoice;
40  import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
41  import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
42  
43  import java.util.ArrayList;
44  import java.util.Calendar;
45  import java.util.Enumeration;
46  import java.util.List;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditQuestionAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
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         String title = ParamUtil.getString(actionRequest, "title");
155         String description = ParamUtil.getString(actionRequest, "description");
156 
157         int expirationDateMonth = ParamUtil.getInteger(
158             actionRequest, "expirationDateMonth");
159         int expirationDateDay = ParamUtil.getInteger(
160             actionRequest, "expirationDateDay");
161         int expirationDateYear = ParamUtil.getInteger(
162             actionRequest, "expirationDateYear");
163         int expirationDateHour = ParamUtil.getInteger(
164             actionRequest, "expirationDateHour");
165         int expirationDateMinute = ParamUtil.getInteger(
166             actionRequest, "expirationDateMinute");
167         int expirationDateAmPm = ParamUtil.getInteger(
168             actionRequest, "expirationDateAmPm");
169         boolean neverExpire = ParamUtil.getBoolean(
170             actionRequest, "neverExpire");
171 
172         if (expirationDateAmPm == Calendar.PM) {
173             expirationDateHour += 12;
174         }
175 
176         List<PollsChoice> choices = new ArrayList<PollsChoice>();
177 
178         Enumeration<String> enu = actionRequest.getParameterNames();
179 
180         while (enu.hasMoreElements()) {
181             String param = enu.nextElement();
182 
183             if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
184                 try {
185                     String id = param.substring(
186                         CHOICE_DESCRIPTION_PREFIX.length(), param.length());
187 
188                     String choiceName = ParamUtil.getString(
189                         actionRequest, CHOICE_NAME_PREFIX + id);
190                     String choiceDescription = ParamUtil.getString(
191                         actionRequest, param);
192 
193                     PollsChoice choice = PollsChoiceUtil.create(0);
194 
195                     choice.setName(choiceName);
196                     choice.setDescription(choiceDescription);
197 
198                     choices.add(choice);
199                 }
200                 catch (Exception e) {
201                 }
202             }
203         }
204 
205         ServiceContext serviceContext = ServiceContextFactory.getInstance(
206             BookmarksEntry.class.getName(), actionRequest);
207 
208         if (questionId <= 0) {
209 
210             // Add question
211 
212             PollsQuestionServiceUtil.addQuestion(
213                 title, description, expirationDateMonth, expirationDateDay,
214                 expirationDateYear, expirationDateHour, expirationDateMinute,
215                 neverExpire, choices, serviceContext);
216         }
217         else {
218 
219             // Update question
220 
221             PollsQuestionServiceUtil.updateQuestion(
222                 questionId, title, description, expirationDateMonth,
223                 expirationDateDay, expirationDateYear, expirationDateHour,
224                 expirationDateMinute, neverExpire, choices, serviceContext);
225         }
226     }
227 
228 }