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