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