1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.login.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.servlet.SessionMessages;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletPreferences;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  /**
34   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Julio Camarero
38   */
39  public class ConfigurationActionImpl extends BaseConfigurationAction {
40  
41      public void processAction(
42              PortletConfig portletConfig, ActionRequest actionRequest,
43              ActionResponse actionResponse)
44          throws Exception {
45  
46          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
47  
48          if (!cmd.equals(Constants.UPDATE)) {
49              return;
50          }
51  
52          String portletResource = ParamUtil.getString(
53              actionRequest, "portletResource");
54  
55          PortletPreferences preferences =
56              PortletPreferencesFactoryUtil.getPortletSetup(
57                  actionRequest, portletResource);
58  
59          String tabs1 = ParamUtil.getString(actionRequest, "tabs1");
60  
61          if (tabs1.equals("general")) {
62              updateGeneral(actionRequest, preferences);
63          }
64          else if (tabs1.equals("email-notifications")) {
65              updateEmailNotifications(actionRequest, preferences);
66          }
67  
68          SessionMessages.add(
69              actionRequest, portletConfig.getPortletName() + ".doConfigure");
70      }
71  
72      public String render(
73              PortletConfig portletConfig, RenderRequest renderRequest,
74              RenderResponse renderResponse)
75          throws Exception {
76  
77          return "/html/portlet/login/configuration.jsp";
78      }
79  
80      protected void updateEmailNotifications(
81              ActionRequest actionRequest, PortletPreferences preferences)
82          throws Exception {
83  
84          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
85  
86          if (tabs2.equals("password-changed-notification") ||
87              tabs2.equals("password-reset-notification")) {
88  
89              String languageId = LanguageUtil.getLanguageId(actionRequest);
90  
91              String emailParam = "emailPasswordSent";
92  
93              if (tabs2.equals("password-reset-notification")) {
94                  emailParam = "emailPasswordReset";
95              }
96  
97              String emailSubject = ParamUtil.getString(
98                  actionRequest, emailParam + "Subject_" + languageId);
99              String emailBody = ParamUtil.getString(
100                 actionRequest, emailParam + "Body_" + languageId);
101 
102             preferences.setValue(
103                 emailParam + "Subject_" + languageId, emailSubject);
104             preferences.setValue(emailParam + "Body_" + languageId, emailBody);
105 
106             preferences.store();
107         }
108         else {
109             String emailFromName = ParamUtil.getString(
110                 actionRequest, "emailFromName");
111             String emailFromAddress = ParamUtil.getString(
112                 actionRequest, "emailFromAddress");
113 
114             preferences.setValue("emailFromName", emailFromName);
115 
116             if (Validator.isNotNull(emailFromAddress) &&
117                 !Validator.isEmailAddress(emailFromAddress)) {
118 
119                 SessionErrors.add(actionRequest, "emailFromAddress");
120             }
121             else {
122                 preferences.setValue("emailFromName", emailFromName);
123                 preferences.setValue("emailFromAddress", emailFromAddress);
124 
125                 preferences.store();
126             }
127         }
128     }
129 
130     protected void updateGeneral(
131             ActionRequest actionRequest, PortletPreferences preferences)
132         throws Exception {
133 
134         String authType = ParamUtil.getString(actionRequest, "authType");
135 
136         preferences.setValue("authType", authType);
137 
138         preferences.store();
139     }
140 
141 }