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.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
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         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
155 
156         long questionId = ParamUtil.getLong(actionRequest, "questionId");
157 
158         String title = ParamUtil.getString(actionRequest, "title");
159         String description = ParamUtil.getString(actionRequest, "description");
160 
161         int expirationDateMonth = ParamUtil.getInteger(
162             actionRequest, "expirationDateMonth");
163         int expirationDateDay = ParamUtil.getInteger(
164             actionRequest, "expirationDateDay");
165         int expirationDateYear = ParamUtil.getInteger(
166             actionRequest, "expirationDateYear");
167         int expirationDateHour = ParamUtil.getInteger(
168             actionRequest, "expirationDateHour");
169         int expirationDateMinute = ParamUtil.getInteger(
170             actionRequest, "expirationDateMinute");
171         int expirationDateAmPm = ParamUtil.getInteger(
172             actionRequest, "expirationDateAmPm");
173         boolean neverExpire = ParamUtil.getBoolean(
174             actionRequest, "neverExpire");
175 
176         if (expirationDateAmPm == Calendar.PM) {
177             expirationDateHour += 12;
178         }
179 
180         List<PollsChoice> choices = new ArrayList<PollsChoice>();
181 
182         Enumeration<String> enu = actionRequest.getParameterNames();
183 
184         while (enu.hasMoreElements()) {
185             String param = enu.nextElement();
186 
187             if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
188                 try {
189                     String id = param.substring(
190                         CHOICE_DESCRIPTION_PREFIX.length(), param.length());
191 
192                     String choiceName = ParamUtil.getString(
193                         actionRequest, CHOICE_NAME_PREFIX + id);
194                     String choiceDescription = ParamUtil.getString(
195                         actionRequest, param);
196 
197                     PollsChoice choice = PollsChoiceUtil.create(0);
198 
199                     choice.setName(choiceName);
200                     choice.setDescription(choiceDescription);
201 
202                     choices.add(choice);
203                 }
204                 catch (Exception e) {
205                 }
206             }
207         }
208 
209         String[] communityPermissions = PortalUtil.getCommunityPermissions(
210             actionRequest);
211         String[] guestPermissions = PortalUtil.getGuestPermissions(
212             actionRequest);
213 
214         if (questionId <= 0) {
215 
216             // Add question
217 
218             PollsQuestionServiceUtil.addQuestion(
219                 layout.getPlid(), title, description, expirationDateMonth,
220                 expirationDateDay, expirationDateYear, expirationDateHour,
221                 expirationDateMinute, neverExpire, choices,
222                 communityPermissions, guestPermissions);
223         }
224         else {
225 
226             // Update question
227 
228             PollsQuestionServiceUtil.updateQuestion(
229                 questionId, title, description, expirationDateMonth,
230                 expirationDateDay, expirationDateYear, expirationDateHour,
231                 expirationDateMinute, neverExpire, choices);
232         }
233     }
234 
235 }