001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.polls.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.Layout;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.struts.PortletAction;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.portlet.polls.DuplicateVoteException;
031: import com.liferay.portlet.polls.NoSuchChoiceException;
032: import com.liferay.portlet.polls.NoSuchQuestionException;
033: import com.liferay.portlet.polls.QuestionChoiceException;
034: import com.liferay.portlet.polls.QuestionDescriptionException;
035: import com.liferay.portlet.polls.QuestionExpirationDateException;
036: import com.liferay.portlet.polls.QuestionExpiredException;
037: import com.liferay.portlet.polls.QuestionTitleException;
038: import com.liferay.portlet.polls.model.PollsChoice;
039: import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
040: import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
041: import com.liferay.util.servlet.SessionErrors;
042:
043: import java.util.ArrayList;
044: import java.util.Calendar;
045: import java.util.Enumeration;
046: import java.util.List;
047:
048: import javax.portlet.ActionRequest;
049: import javax.portlet.ActionResponse;
050: import javax.portlet.PortletConfig;
051: import javax.portlet.RenderRequest;
052: import javax.portlet.RenderResponse;
053:
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * <a href="EditQuestionAction.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Brian Wing Shun Chan
062: *
063: */
064: public class EditQuestionAction extends PortletAction {
065:
066: public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
067:
068: public static final String CHOICE_NAME_PREFIX = "choiceName";
069:
070: public void processAction(ActionMapping mapping, ActionForm form,
071: PortletConfig config, ActionRequest req, ActionResponse res)
072: throws Exception {
073:
074: String cmd = ParamUtil.getString(req, Constants.CMD);
075:
076: try {
077: if (cmd.equals(Constants.ADD)
078: || cmd.equals(Constants.UPDATE)) {
079: updateQuestion(req);
080: } else if (cmd.equals(Constants.DELETE)) {
081: deleteQuestion(req);
082: }
083:
084: if (Validator.isNotNull(cmd)) {
085: sendRedirect(req, res);
086: }
087: } catch (Exception e) {
088: if (e instanceof NoSuchQuestionException
089: || e instanceof PrincipalException) {
090:
091: SessionErrors.add(req, e.getClass().getName());
092:
093: setForward(req, "portlet.polls.error");
094: } else if (e instanceof DuplicateVoteException
095: || e instanceof NoSuchChoiceException
096: || e instanceof QuestionChoiceException
097: || e instanceof QuestionDescriptionException
098: || e instanceof QuestionExpirationDateException ||
099:
100: e instanceof QuestionTitleException) {
101:
102: SessionErrors.add(req, e.getClass().getName());
103: } else if (e instanceof QuestionExpiredException) {
104: } else {
105: throw e;
106: }
107: }
108: }
109:
110: public ActionForward render(ActionMapping mapping, ActionForm form,
111: PortletConfig config, RenderRequest req, RenderResponse res)
112: throws Exception {
113:
114: try {
115: ActionUtil.getQuestion(req);
116: } catch (Exception e) {
117: if (e instanceof NoSuchQuestionException
118: || e instanceof PrincipalException) {
119:
120: SessionErrors.add(req, e.getClass().getName());
121:
122: return mapping.findForward("portlet.polls.error");
123: } else {
124: throw e;
125: }
126: }
127:
128: return mapping.findForward(getForward(req,
129: "portlet.polls.edit_question"));
130: }
131:
132: protected void deleteQuestion(ActionRequest req) throws Exception {
133: long questionId = ParamUtil.getLong(req, "questionId");
134:
135: PollsQuestionServiceUtil.deleteQuestion(questionId);
136:
137: }
138:
139: protected void updateQuestion(ActionRequest req) throws Exception {
140: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
141:
142: long questionId = ParamUtil.getLong(req, "questionId");
143:
144: String title = ParamUtil.getString(req, "title");
145: String description = ParamUtil.getString(req, "description");
146:
147: int expirationDateMonth = ParamUtil.getInteger(req,
148: "expirationDateMonth");
149: int expirationDateDay = ParamUtil.getInteger(req,
150: "expirationDateDay");
151: int expirationDateYear = ParamUtil.getInteger(req,
152: "expirationDateYear");
153: int expirationDateHour = ParamUtil.getInteger(req,
154: "expirationDateHour");
155: int expirationDateMinute = ParamUtil.getInteger(req,
156: "expirationDateMinute");
157: int expirationDateAmPm = ParamUtil.getInteger(req,
158: "expirationDateAmPm");
159: boolean neverExpire = ParamUtil.getBoolean(req, "neverExpire");
160:
161: if (expirationDateAmPm == Calendar.PM) {
162: expirationDateHour += 12;
163: }
164:
165: List choices = new ArrayList();
166:
167: Enumeration enu = req.getParameterNames();
168:
169: while (enu.hasMoreElements()) {
170: String param = (String) enu.nextElement();
171:
172: if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
173: try {
174: String id = param.substring(
175: CHOICE_DESCRIPTION_PREFIX.length(), param
176: .length());
177:
178: String choiceName = ParamUtil.getString(req,
179: CHOICE_NAME_PREFIX + id);
180: String choiceDescription = ParamUtil.getString(req,
181: param);
182:
183: PollsChoice choice = PollsChoiceUtil.create(0);
184:
185: choice.setName(choiceName);
186: choice.setDescription(choiceDescription);
187:
188: choices.add(choice);
189: } catch (Exception e) {
190: }
191: }
192: }
193:
194: String[] communityPermissions = req
195: .getParameterValues("communityPermissions");
196: String[] guestPermissions = req
197: .getParameterValues("guestPermissions");
198:
199: if (questionId <= 0) {
200:
201: // Add question
202:
203: PollsQuestionServiceUtil.addQuestion(layout.getPlid(),
204: title, description, expirationDateMonth,
205: expirationDateDay, expirationDateYear,
206: expirationDateHour, expirationDateMinute,
207: neverExpire, choices, communityPermissions,
208: guestPermissions);
209: } else {
210:
211: // Update question
212:
213: PollsQuestionServiceUtil.updateQuestion(questionId, title,
214: description, expirationDateMonth,
215: expirationDateDay, expirationDateYear,
216: expirationDateHour, expirationDateMinute,
217: neverExpire, choices);
218: }
219: }
220:
221: }
|