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.portlet.login.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.PortletPreferences;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Julio Camarero
036     */
037    public class ConfigurationActionImpl extends BaseConfigurationAction {
038    
039            public void processAction(
040                            PortletConfig portletConfig, ActionRequest actionRequest,
041                            ActionResponse actionResponse)
042                    throws Exception {
043    
044                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
045    
046                    if (!cmd.equals(Constants.UPDATE)) {
047                            return;
048                    }
049    
050                    String portletResource = ParamUtil.getString(
051                            actionRequest, "portletResource");
052    
053                    PortletPreferences preferences =
054                            PortletPreferencesFactoryUtil.getPortletSetup(
055                                    actionRequest, portletResource);
056    
057                    String tabs1 = ParamUtil.getString(actionRequest, "tabs1");
058    
059                    if (tabs1.equals("general")) {
060                            updateGeneral(actionRequest, preferences);
061                    }
062                    else if (tabs1.equals("email-notifications")) {
063                            updateEmailNotifications(actionRequest, preferences);
064                    }
065    
066                    SessionMessages.add(
067                            actionRequest, portletConfig.getPortletName() + ".doConfigure");
068            }
069    
070            public String render(
071                            PortletConfig portletConfig, RenderRequest renderRequest,
072                            RenderResponse renderResponse)
073                    throws Exception {
074    
075                    return "/html/portlet/login/configuration.jsp";
076            }
077    
078            protected void updateEmailNotifications(
079                            ActionRequest actionRequest, PortletPreferences preferences)
080                    throws Exception {
081    
082                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
083    
084                    if (tabs2.equals("password-changed-notification") ||
085                            tabs2.equals("password-reset-notification")) {
086    
087                            String languageId = LanguageUtil.getLanguageId(actionRequest);
088    
089                            String emailParam = "emailPasswordSent";
090    
091                            if (tabs2.equals("password-reset-notification")) {
092                                    emailParam = "emailPasswordReset";
093                            }
094    
095                            String emailSubject = ParamUtil.getString(
096                                    actionRequest, emailParam + "Subject_" + languageId);
097                            String emailBody = ParamUtil.getString(
098                                    actionRequest, emailParam + "Body_" + languageId);
099    
100                            preferences.setValue(
101                                    emailParam + "Subject_" + languageId, emailSubject);
102                            preferences.setValue(emailParam + "Body_" + languageId, emailBody);
103    
104                            preferences.store();
105                    }
106                    else {
107                            String emailFromName = ParamUtil.getString(
108                                    actionRequest, "emailFromName");
109                            String emailFromAddress = ParamUtil.getString(
110                                    actionRequest, "emailFromAddress");
111    
112                            preferences.setValue("emailFromName", emailFromName);
113    
114                            if (Validator.isNotNull(emailFromAddress) &&
115                                    !Validator.isEmailAddress(emailFromAddress)) {
116    
117                                    SessionErrors.add(actionRequest, "emailFromAddress");
118                            }
119                            else {
120                                    preferences.setValue("emailFromName", emailFromName);
121                                    preferences.setValue("emailFromAddress", emailFromAddress);
122    
123                                    preferences.store();
124                            }
125                    }
126            }
127    
128            protected void updateGeneral(
129                            ActionRequest actionRequest, PortletPreferences preferences)
130                    throws Exception {
131    
132                    String authType = ParamUtil.getString(actionRequest, "authType");
133    
134                    preferences.setValue("authType", authType);
135    
136                    preferences.store();
137            }
138    
139    }