001    /**
002     * Copyright (c) 2000-present 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.exception.NoSuchUserException;
018    import com.liferay.portal.exception.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.users.admin.kernel.util.UsersAdmin;
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            @Override
044            public ActionForward execute(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            HttpServletRequest request, HttpServletResponse response)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(request, Constants.CMD);
050    
051                    if (Validator.isNull(cmd)) {
052                            return actionMapping.findForward("portal.update_reminder_query");
053                    }
054    
055                    try {
056                            updateReminderQuery(request, response);
057    
058                            return actionMapping.findForward(
059                                    ActionConstants.COMMON_REFERER_JSP);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof UserReminderQueryException) {
063                                    SessionErrors.add(request, e.getClass());
064    
065                                    return actionMapping.findForward(
066                                            "portal.update_reminder_query");
067                            }
068                            else if (e instanceof NoSuchUserException ||
069                                             e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(request, e.getClass());
072    
073                                    return actionMapping.findForward("portal.error");
074                            }
075    
076                            PortalUtil.sendError(e, request, response);
077    
078                            return null;
079                    }
080            }
081    
082            protected void updateReminderQuery(
083                            HttpServletRequest request, HttpServletResponse response)
084                    throws Exception {
085    
086                    AuthTokenUtil.checkCSRFToken(
087                            request, UpdateReminderQueryAction.class.getName());
088    
089                    long userId = PortalUtil.getUserId(request);
090                    String question = ParamUtil.getString(request, "reminderQueryQuestion");
091                    String answer = ParamUtil.getString(request, "reminderQueryAnswer");
092    
093                    if (question.equals(UsersAdmin.CUSTOM_QUESTION)) {
094                            question = ParamUtil.getString(
095                                    request, "reminderQueryCustomQuestion");
096                    }
097    
098                    UserServiceUtil.updateReminderQuery(userId, question, answer);
099            }
100    
101    }