001    /**
002     * Copyright (c) 2000-2011 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.portal.action;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.UserReminderQueryException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.AuthTokenUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.UserServiceUtil;
026    import com.liferay.portal.struts.ActionConstants;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.Action;
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class UpdateReminderQueryAction extends Action {
042    
043            public ActionForward execute(
044                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
045                            HttpServletResponse response)
046                    throws Exception {
047    
048                    String cmd = ParamUtil.getString(request, Constants.CMD);
049    
050                    if (Validator.isNull(cmd)) {
051                            return mapping.findForward("portal.update_reminder_query");
052                    }
053    
054                    try {
055                            updateReminderQuery(request, response);
056    
057                            return mapping.findForward(ActionConstants.COMMON_REFERER);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof UserReminderQueryException) {
061                                    SessionErrors.add(request, e.getClass().getName());
062    
063                                    return mapping.findForward("portal.update_reminder_query");
064                            }
065                            else if (e instanceof NoSuchUserException ||
066                                             e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(request, e.getClass().getName());
069    
070                                    return mapping.findForward("portal.error");
071                            }
072                            else {
073                                    PortalUtil.sendError(e, request, response);
074    
075                                    return null;
076                            }
077                    }
078            }
079    
080            protected void updateReminderQuery(
081                            HttpServletRequest request, HttpServletResponse response)
082                    throws Exception {
083    
084                    AuthTokenUtil.check(request);
085    
086                    long userId = PortalUtil.getUserId(request);
087                    String question = ParamUtil.getString(request, "reminderQueryQuestion");
088                    String answer = ParamUtil.getString(request, "reminderQueryAnswer");
089    
090                    if (question.equals(EnterpriseAdminUtil.CUSTOM_QUESTION)) {
091                            question = ParamUtil.getString(
092                                    request, "reminderQueryCustomQuestion");
093                    }
094    
095                    UserServiceUtil.updateReminderQuery(userId, question, answer);
096            }
097    
098    }