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