1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.login.action;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.servlet.SessionMessages;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletPreferences;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  /**
39   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Julio Camarero
43   *
44   */
45  public class ConfigurationActionImpl extends BaseConfigurationAction {
46  
47      public void processAction(
48              PortletConfig portletConfig, ActionRequest actionRequest,
49              ActionResponse actionResponse)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
53  
54          if (!cmd.equals(Constants.UPDATE)) {
55              return;
56          }
57  
58          String portletResource = ParamUtil.getString(
59              actionRequest, "portletResource");
60  
61          PortletPreferences preferences =
62              PortletPreferencesFactoryUtil.getPortletSetup(
63                  actionRequest, portletResource);
64  
65          String tabs1 = ParamUtil.getString(actionRequest, "tabs1");
66  
67          if (tabs1.equals("general")) {
68              updateGeneral(actionRequest, preferences);
69          }
70          else if (tabs1.equals("email-notifications")) {
71              updateEmailNotifications(actionRequest, preferences);
72          }
73  
74          SessionMessages.add(
75              actionRequest, portletConfig.getPortletName() + ".doConfigure");
76      }
77  
78      public String render(
79              PortletConfig portletConfig, RenderRequest renderRequest,
80              RenderResponse renderResponse)
81          throws Exception {
82  
83          return "/html/portlet/login/configuration.jsp";
84      }
85  
86      protected void updateEmailNotifications(
87              ActionRequest actionRequest, PortletPreferences preferences)
88          throws Exception {
89  
90          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
91  
92          if (tabs2.equals("password-changed-notification")) {
93              String languageId = LanguageUtil.getLanguageId(actionRequest);
94  
95              String emailPasswordSentEnabled = ParamUtil.getString(
96                  actionRequest, "emailPasswordSentEnabled");
97              String emailPasswordSentSubject = ParamUtil.getString(
98                  actionRequest, "emailPasswordSentSubject_" + languageId);
99              String emailPasswordSentBody = ParamUtil.getString(
100                 actionRequest, "emailPasswordSentBody_" + languageId);
101 
102             preferences.setValue(
103                 "emailPasswordSentEnabled", emailPasswordSentEnabled);
104             preferences.setValue(
105                 "emailPasswordSentSubject_" + languageId,
106                 emailPasswordSentSubject);
107             preferences.setValue(
108                 "emailPasswordSentBody_" + languageId, emailPasswordSentBody);
109 
110             preferences.store();
111         }
112         else {
113             String emailFromName = ParamUtil.getString(
114                 actionRequest, "emailFromName");
115             String emailFromAddress = ParamUtil.getString(
116                 actionRequest, "emailFromAddress");
117 
118             preferences.setValue("emailFromName", emailFromName);
119 
120             if (Validator.isNotNull(emailFromAddress) &&
121                 !Validator.isEmailAddress(emailFromAddress)) {
122 
123                 SessionErrors.add(actionRequest, "emailFromAddress");
124             }
125             else {
126                 preferences.setValue("emailFromName", emailFromName);
127                 preferences.setValue("emailFromAddress", emailFromAddress);
128 
129                 preferences.store();
130             }
131         }
132     }
133 
134     protected void updateGeneral(
135             ActionRequest actionRequest, PortletPreferences preferences)
136         throws Exception {
137 
138         String authType = ParamUtil.getString(actionRequest, "authType");
139 
140         preferences.setValue("authType", authType);
141 
142         preferences.store();
143     }
144 
145 }