001    /**
002     * Copyright (c) 2000-2012 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.usersadmin.util.UsersAdminUtil;
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 mapping, ActionForm form, HttpServletRequest request,
046                            HttpServletResponse response)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(request, Constants.CMD);
050    
051                    if (Validator.isNull(cmd)) {
052                            return mapping.findForward("portal.update_reminder_query");
053                    }
054    
055                    try {
056                            updateReminderQuery(request, response);
057    
058                            return mapping.findForward(ActionConstants.COMMON_REFERER);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof UserReminderQueryException) {
062                                    SessionErrors.add(request, e.getClass().getName());
063    
064                                    return mapping.findForward("portal.update_reminder_query");
065                            }
066                            else if (e instanceof NoSuchUserException ||
067                                             e instanceof PrincipalException) {
068    
069                                    SessionErrors.add(request, e.getClass().getName());
070    
071                                    return mapping.findForward("portal.error");
072                            }
073                            else {
074                                    PortalUtil.sendError(e, request, response);
075    
076                                    return null;
077                            }
078                    }
079            }
080    
081            protected void updateReminderQuery(
082                            HttpServletRequest request, HttpServletResponse response)
083                    throws Exception {
084    
085                    AuthTokenUtil.check(request);
086    
087                    long userId = PortalUtil.getUserId(request);
088                    String question = ParamUtil.getString(request, "reminderQueryQuestion");
089                    String answer = ParamUtil.getString(request, "reminderQueryAnswer");
090    
091                    if (question.equals(UsersAdminUtil.CUSTOM_QUESTION)) {
092                            question = ParamUtil.getString(
093                                    request, "reminderQueryCustomQuestion");
094                    }
095    
096                    UserServiceUtil.updateReminderQuery(userId, question, answer);
097            }
098    
099    }