001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.LocalizationUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
028    import com.liferay.portlet.polls.DuplicateVoteException;
029    import com.liferay.portlet.polls.NoSuchChoiceException;
030    import com.liferay.portlet.polls.NoSuchQuestionException;
031    import com.liferay.portlet.polls.QuestionChoiceException;
032    import com.liferay.portlet.polls.QuestionDescriptionException;
033    import com.liferay.portlet.polls.QuestionExpirationDateException;
034    import com.liferay.portlet.polls.QuestionExpiredException;
035    import com.liferay.portlet.polls.QuestionTitleException;
036    import com.liferay.portlet.polls.model.PollsChoice;
037    import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
038    import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
039    
040    import java.util.ArrayList;
041    import java.util.Calendar;
042    import java.util.Enumeration;
043    import java.util.HashSet;
044    import java.util.List;
045    import java.util.Locale;
046    import java.util.Map;
047    import java.util.Set;
048    
049    import javax.portlet.ActionRequest;
050    import javax.portlet.ActionResponse;
051    import javax.portlet.PortletConfig;
052    import javax.portlet.RenderRequest;
053    import javax.portlet.RenderResponse;
054    
055    import org.apache.struts.action.ActionForm;
056    import org.apache.struts.action.ActionForward;
057    import org.apache.struts.action.ActionMapping;
058    
059    /**
060     * @author Brian Wing Shun Chan
061     */
062    public class EditQuestionAction extends PortletAction {
063    
064            public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
065    
066            public static final String CHOICE_NAME_PREFIX = "choiceName";
067    
068            public void processAction(
069                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070                            ActionRequest actionRequest, ActionResponse actionResponse)
071                    throws Exception {
072    
073                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
074    
075                    try {
076                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
077                                    updateQuestion(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.DELETE)) {
080                                    deleteQuestion(actionRequest);
081                            }
082    
083                            if (Validator.isNotNull(cmd)) {
084                                    sendRedirect(actionRequest, actionResponse);
085                            }
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchQuestionException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass().getName());
092    
093                                    setForward(actionRequest, "portlet.polls.error");
094                            }
095                            else if (e instanceof DuplicateVoteException ||
096                                             e instanceof NoSuchChoiceException ||
097                                             e instanceof QuestionChoiceException ||
098                                             e instanceof QuestionDescriptionException ||
099                                             e instanceof QuestionExpirationDateException ||
100    
101                                             e instanceof QuestionTitleException) {
102    
103                                    SessionErrors.add(actionRequest, e.getClass().getName());
104                            }
105                            else if (e instanceof QuestionExpiredException) {
106                            }
107                            else {
108                                    throw e;
109                            }
110                    }
111            }
112    
113            public ActionForward render(
114                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
115                            RenderRequest renderRequest, RenderResponse renderResponse)
116                    throws Exception {
117    
118                    try {
119                            ActionUtil.getQuestion(renderRequest);
120                    }
121                    catch (Exception e) {
122                            if (e instanceof NoSuchQuestionException ||
123                                    e instanceof PrincipalException) {
124    
125                                    SessionErrors.add(renderRequest, e.getClass().getName());
126    
127                                    return mapping.findForward("portlet.polls.error");
128                            }
129                            else {
130                                    throw e;
131                            }
132                    }
133    
134                    return mapping.findForward(
135                            getForward(renderRequest, "portlet.polls.edit_question"));
136            }
137    
138            protected void deleteQuestion(ActionRequest actionRequest)
139                    throws Exception {
140    
141                    long questionId = ParamUtil.getLong(actionRequest, "questionId");
142    
143                    PollsQuestionServiceUtil.deleteQuestion(questionId);
144    
145            }
146    
147            protected void updateQuestion(ActionRequest actionRequest)
148                    throws Exception {
149    
150                    long questionId = ParamUtil.getLong(actionRequest, "questionId");
151    
152                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
153                            actionRequest, "title");
154                    Map<Locale, String> descriptionMap =
155                            LocalizationUtil.getLocalizationMap(
156                                    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                    Set<String> readParameters = new HashSet<String>();
180    
181                    Enumeration<String> enu = actionRequest.getParameterNames();
182    
183                    while (enu.hasMoreElements()) {
184                            String param = enu.nextElement();
185    
186                            if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
187                                    try {
188                                            String id = param.substring(
189                                                    CHOICE_DESCRIPTION_PREFIX.length(),
190                                                    param.indexOf(StringPool.UNDERLINE));
191    
192                                            if (readParameters.contains(id)) {
193                                                    continue;
194                                            }
195    
196                                            String choiceName = ParamUtil.getString(
197                                                    actionRequest, CHOICE_NAME_PREFIX + id);
198    
199                                            Map<Locale, String> localeChoiceDescriptionMap =
200                                                    LocalizationUtil.getLocalizationMap(
201                                                            actionRequest, CHOICE_DESCRIPTION_PREFIX + id);
202    
203                                            PollsChoice choice = PollsChoiceUtil.create(0);
204    
205                                            choice.setName(choiceName);
206                                            choice.setDescriptionMap(localeChoiceDescriptionMap);
207    
208                                            choices.add(choice);
209    
210                                            readParameters.add(id);
211                                    }
212                                    catch (Exception e) {
213                                    }
214                            }
215                    }
216    
217                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
218                            BookmarksEntry.class.getName(), actionRequest);
219    
220                    if (questionId <= 0) {
221    
222                            // Add question
223    
224                            PollsQuestionServiceUtil.addQuestion(
225                                    titleMap, descriptionMap, expirationDateMonth,
226                                    expirationDateDay, expirationDateYear, expirationDateHour,
227                                    expirationDateMinute, neverExpire, choices, serviceContext);
228                    }
229                    else {
230    
231                            // Update question
232    
233                            PollsQuestionServiceUtil.updateQuestion(
234                                    questionId, titleMap, descriptionMap, expirationDateMonth,
235                                    expirationDateDay, expirationDateYear, expirationDateHour,
236                                    expirationDateMinute, neverExpire, choices, serviceContext);
237                    }
238            }
239    
240    }