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