001    /**
002     * Copyright (c) 2000-2013 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.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.portlet.LiferayWindowState;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.PortletPreferencesFactoryUtil;
037    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
038    import com.liferay.portlet.polls.DuplicateVoteException;
039    import com.liferay.portlet.polls.NoSuchChoiceException;
040    import com.liferay.portlet.polls.NoSuchQuestionException;
041    import com.liferay.portlet.polls.QuestionChoiceException;
042    import com.liferay.portlet.polls.QuestionDescriptionException;
043    import com.liferay.portlet.polls.QuestionExpirationDateException;
044    import com.liferay.portlet.polls.QuestionExpiredException;
045    import com.liferay.portlet.polls.QuestionTitleException;
046    import com.liferay.portlet.polls.model.PollsChoice;
047    import com.liferay.portlet.polls.model.PollsQuestion;
048    import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
049    import com.liferay.portlet.polls.service.persistence.PollsChoiceUtil;
050    
051    import java.util.ArrayList;
052    import java.util.Calendar;
053    import java.util.Enumeration;
054    import java.util.HashSet;
055    import java.util.List;
056    import java.util.Locale;
057    import java.util.Map;
058    import java.util.Set;
059    
060    import javax.portlet.ActionRequest;
061    import javax.portlet.ActionResponse;
062    import javax.portlet.PortletConfig;
063    import javax.portlet.PortletPreferences;
064    import javax.portlet.PortletRequest;
065    import javax.portlet.RenderRequest;
066    import javax.portlet.RenderResponse;
067    import javax.portlet.WindowState;
068    
069    import org.apache.struts.action.ActionForm;
070    import org.apache.struts.action.ActionForward;
071    import org.apache.struts.action.ActionMapping;
072    
073    /**
074     * @author Brian Wing Shun Chan
075     */
076    public class EditQuestionAction extends PortletAction {
077    
078            public static final String CHOICE_DESCRIPTION_PREFIX = "choiceDescription";
079    
080            public static final String CHOICE_NAME_PREFIX = "choiceName";
081    
082            @Override
083            public void processAction(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, ActionRequest actionRequest,
086                            ActionResponse actionResponse)
087                    throws Exception {
088    
089                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
090    
091                    try {
092                            if (Validator.isNull(cmd)) {
093                                    return;
094                            }
095                            else if (cmd.equals(Constants.ADD) ||
096                                             cmd.equals(Constants.UPDATE) ||
097                                             cmd.equals(Constants.VOTE)) {
098    
099                                    updateQuestion(portletConfig, actionRequest, actionResponse);
100                            }
101                            else if (cmd.equals(Constants.DELETE)) {
102                                    deleteQuestion(actionRequest);
103                            }
104    
105                            WindowState windowState = actionRequest.getWindowState();
106    
107                            if (windowState.equals(LiferayWindowState.POP_UP)) {
108                                    String redirect = PortalUtil.escapeRedirect(
109                                            ParamUtil.getString(actionRequest, "redirect"));
110    
111                                    if (Validator.isNotNull(redirect)) {
112                                            actionResponse.sendRedirect(redirect);
113                                    }
114                            }
115                            else {
116                                    sendRedirect(actionRequest, actionResponse);
117                            }
118                    }
119                    catch (Exception e) {
120                            if (e instanceof NoSuchQuestionException ||
121                                    e instanceof PrincipalException) {
122    
123                                    SessionErrors.add(actionRequest, e.getClass());
124    
125                                    setForward(actionRequest, "portlet.polls.error");
126                            }
127                            else if (e instanceof DuplicateVoteException ||
128                                             e instanceof NoSuchChoiceException ||
129                                             e instanceof QuestionChoiceException ||
130                                             e instanceof QuestionDescriptionException ||
131                                             e instanceof QuestionExpirationDateException ||
132                                             e instanceof QuestionTitleException) {
133    
134                                    SessionErrors.add(actionRequest, e.getClass());
135    
136                                    LiferayPortletConfig liferayPortletConfig =
137                                            (LiferayPortletConfig)portletConfig;
138    
139                                    SessionMessages.add(
140                                            actionRequest,
141                                            liferayPortletConfig.getPortletId() +
142                                                    SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
143                            }
144                            else if (e instanceof QuestionExpiredException) {
145                            }
146                            else {
147                                    throw e;
148                            }
149                    }
150            }
151    
152            @Override
153            public ActionForward render(
154                            ActionMapping actionMapping, ActionForm actionForm,
155                            PortletConfig portletConfig, RenderRequest renderRequest,
156                            RenderResponse renderResponse)
157                    throws Exception {
158    
159                    try {
160                            ActionUtil.getQuestion(renderRequest);
161                    }
162                    catch (Exception e) {
163                            if (e instanceof NoSuchQuestionException ||
164                                    e instanceof PrincipalException) {
165    
166                                    SessionErrors.add(renderRequest, e.getClass());
167    
168                                    return actionMapping.findForward("portlet.polls.error");
169                            }
170                            else {
171                                    throw e;
172                            }
173                    }
174    
175                    return actionMapping.findForward(
176                            getForward(renderRequest, "portlet.polls.edit_question"));
177            }
178    
179            protected void addAndStoreSelection(
180                            PortletConfig portletConfig, PortletRequest portletRequest,
181                            PollsQuestion question)
182                    throws Exception {
183    
184                    String referringPortletResource = ParamUtil.getString(
185                            portletRequest, "referringPortletResource");
186    
187                    if (Validator.isNull(referringPortletResource)) {
188                            return;
189                    }
190    
191                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
192                            WebKeys.THEME_DISPLAY);
193    
194                    Layout layout = LayoutLocalServiceUtil.getLayout(
195                            themeDisplay.getRefererPlid());
196    
197                    PortletPreferences preferences =
198                            PortletPreferencesFactoryUtil.getPortletSetup(
199                                    layout, referringPortletResource, StringPool.BLANK);
200    
201                    preferences.setValue(
202                            "questionId", String.valueOf(question.getQuestionId()));
203    
204                    preferences.store();
205    
206                    LiferayPortletConfig liferayPortletConfig =
207                            (LiferayPortletConfig)portletConfig;
208    
209                    SessionMessages.add(
210                            portletRequest,
211                            liferayPortletConfig.getPortletId() +
212                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
213                            referringPortletResource);
214            }
215    
216            protected void deleteQuestion(ActionRequest actionRequest)
217                    throws Exception {
218    
219                    long questionId = ParamUtil.getLong(actionRequest, "questionId");
220    
221                    PollsQuestionServiceUtil.deleteQuestion(questionId);
222            }
223    
224            protected void updateQuestion(
225                            PortletConfig portletConfig, ActionRequest actionRequest,
226                            ActionResponse actionResponse)
227                    throws Exception {
228    
229                    long questionId = ParamUtil.getLong(actionRequest, "questionId");
230    
231                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
232                            actionRequest, "title");
233                    Map<Locale, String> descriptionMap =
234                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
235    
236                    int expirationDateMonth = ParamUtil.getInteger(
237                            actionRequest, "expirationDateMonth");
238                    int expirationDateDay = ParamUtil.getInteger(
239                            actionRequest, "expirationDateDay");
240                    int expirationDateYear = ParamUtil.getInteger(
241                            actionRequest, "expirationDateYear");
242                    int expirationDateHour = ParamUtil.getInteger(
243                            actionRequest, "expirationDateHour");
244                    int expirationDateMinute = ParamUtil.getInteger(
245                            actionRequest, "expirationDateMinute");
246                    int expirationDateAmPm = ParamUtil.getInteger(
247                            actionRequest, "expirationDateAmPm");
248                    boolean neverExpire = ParamUtil.getBoolean(
249                            actionRequest, "neverExpire");
250    
251                    if (expirationDateAmPm == Calendar.PM) {
252                            expirationDateHour += 12;
253                    }
254    
255                    List<PollsChoice> choices = new ArrayList<PollsChoice>();
256    
257                    Set<String> readParameters = new HashSet<String>();
258    
259                    Enumeration<String> enu = actionRequest.getParameterNames();
260    
261                    while (enu.hasMoreElements()) {
262                            String param = enu.nextElement();
263    
264                            if (param.startsWith(CHOICE_DESCRIPTION_PREFIX)) {
265                                    try {
266                                            String id = param.substring(
267                                                    CHOICE_DESCRIPTION_PREFIX.length(),
268                                                    param.indexOf(CharPool.UNDERLINE));
269    
270                                            if (readParameters.contains(id)) {
271                                                    continue;
272                                            }
273    
274                                            String choiceName = ParamUtil.getString(
275                                                    actionRequest, CHOICE_NAME_PREFIX + id);
276    
277                                            Map<Locale, String> localeChoiceDescriptionMap =
278                                                    LocalizationUtil.getLocalizationMap(
279                                                            actionRequest, CHOICE_DESCRIPTION_PREFIX + id);
280    
281                                            PollsChoice choice = PollsChoiceUtil.create(0);
282    
283                                            choice.setName(choiceName);
284                                            choice.setDescriptionMap(localeChoiceDescriptionMap);
285    
286                                            choices.add(choice);
287    
288                                            readParameters.add(id);
289                                    }
290                                    catch (Exception e) {
291                                    }
292                            }
293                    }
294    
295                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
296                            BookmarksEntry.class.getName(), actionRequest);
297    
298                    if (questionId <= 0) {
299    
300                            // Add question
301    
302                            PollsQuestion question = PollsQuestionServiceUtil.addQuestion(
303                                    titleMap, descriptionMap, expirationDateMonth,
304                                    expirationDateDay, expirationDateYear, expirationDateHour,
305                                    expirationDateMinute, neverExpire, choices, serviceContext);
306    
307                            // Poll display
308    
309                            addAndStoreSelection(portletConfig, actionRequest, question);
310                    }
311                    else {
312    
313                            // Update question
314    
315                            PollsQuestionServiceUtil.updateQuestion(
316                                    questionId, titleMap, descriptionMap, expirationDateMonth,
317                                    expirationDateDay, expirationDateYear, expirationDateHour,
318                                    expirationDateMinute, neverExpire, choices, serviceContext);
319                    }
320            }
321    
322    }