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.wiki.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
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.portlet.PortletPreferencesFactoryUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletConfig;
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    /**
033     * @author Bruno Farache
034     */
035    public class ConfigurationActionImpl extends BaseConfigurationAction {
036    
037            public void processAction(
038                            PortletConfig portletConfig, ActionRequest actionRequest,
039                            ActionResponse actionResponse)
040                    throws Exception {
041    
042                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
043    
044                    if (!cmd.equals(Constants.UPDATE)) {
045                            return;
046                    }
047    
048                    String portletResource = ParamUtil.getString(
049                            actionRequest, "portletResource");
050    
051                    PortletPreferences preferences =
052                            PortletPreferencesFactoryUtil.getPortletSetup(
053                                    actionRequest, portletResource);
054    
055                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
056    
057                    if (tabs2.equals("display-settings")) {
058                            updateDisplaySettings(actionRequest, preferences);
059                    }
060                    else if (tabs2.equals("email-from")) {
061                            updateEmailFrom(actionRequest, preferences);
062                    }
063                    else if (tabs2.equals("page-added-email")) {
064                            updateEmailPageAdded(actionRequest, preferences);
065                    }
066                    else if (tabs2.equals("page-updated-email")) {
067                            updateEmailPageUpdated(actionRequest, preferences);
068                    }
069                    else if (tabs2.equals("rss")) {
070                            updateRSS(actionRequest, preferences);
071                    }
072    
073                    if (SessionErrors.isEmpty(actionRequest)) {
074                            preferences.store();
075    
076                            SessionMessages.add(
077                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
078                    }
079            }
080    
081            public String render(
082                            PortletConfig portletConfig, RenderRequest renderRequest,
083                            RenderResponse renderResponse)
084                    throws Exception {
085    
086                    return "/html/portlet/wiki/configuration.jsp";
087            }
088    
089            protected void updateDisplaySettings(
090                            ActionRequest actionRequest, PortletPreferences preferences)
091                    throws Exception {
092    
093                    boolean enablePageRatings = ParamUtil.getBoolean(
094                            actionRequest, "enablePageRatings");
095                    boolean enableComments = ParamUtil.getBoolean(
096                            actionRequest, "enableComments");
097                    boolean enableCommentRatings = ParamUtil.getBoolean(
098                            actionRequest, "enableCommentRatings");
099                    String visibleNodes = ParamUtil.getString(
100                            actionRequest, "visibleNodes");
101                    String hiddenNodes = ParamUtil.getString(actionRequest, "hiddenNodes");
102    
103                    if (Validator.isNull(visibleNodes)) {
104                            SessionErrors.add(actionRequest, "visibleNodesCount");
105                    }
106                    else {
107                            preferences.setValue(
108                                    "enable-page-ratings", String.valueOf(enablePageRatings));
109                            preferences.setValue(
110                                    "enable-comments", String.valueOf(enableComments));
111                            preferences.setValue(
112                                    "enable-comment-ratings", String.valueOf(enableCommentRatings));
113                            preferences.setValue("visible-nodes", visibleNodes);
114                            preferences.setValue("hidden-nodes", hiddenNodes);
115                    }
116            }
117    
118            protected void updateEmailFrom(
119                            ActionRequest actionRequest, PortletPreferences preferences)
120                    throws Exception {
121    
122                    String emailFromName = ParamUtil.getString(
123                            actionRequest, "emailFromName");
124                    String emailFromAddress = ParamUtil.getString(
125                            actionRequest, "emailFromAddress");
126    
127                    if (Validator.isNull(emailFromName)) {
128                            SessionErrors.add(actionRequest, "emailFromName");
129                    }
130                    else if (!Validator.isEmailAddress(emailFromAddress) &&
131                                     !Validator.isVariableTerm(emailFromAddress)) {
132    
133                            SessionErrors.add(actionRequest, "emailFromAddress");
134                    }
135                    else {
136                            preferences.setValue("email-from-name", emailFromName);
137                            preferences.setValue("email-from-address", emailFromAddress);
138                    }
139            }
140    
141            protected void updateEmailPageAdded(
142                            ActionRequest actionRequest, PortletPreferences preferences)
143                    throws Exception {
144    
145                    boolean emailPageAddedEnabled = ParamUtil.getBoolean(
146                            actionRequest, "emailPageAddedEnabled");
147                    String emailPageAddedSubjectPrefix = ParamUtil.getString(
148                            actionRequest, "emailPageAddedSubjectPrefix");
149                    String emailPageAddedBody = ParamUtil.getString(
150                            actionRequest, "emailPageAddedBody");
151                    String emailPageAddedSignature = ParamUtil.getString(
152                            actionRequest, "emailPageAddedSignature");
153    
154                    if (Validator.isNull(emailPageAddedSubjectPrefix)) {
155                            SessionErrors.add(actionRequest, "emailPageAddedSubjectPrefix");
156                    }
157                    else if (Validator.isNull(emailPageAddedBody)) {
158                            SessionErrors.add(actionRequest, "emailPageAddedBody");
159                    }
160                    else {
161                            preferences.setValue(
162                                    "email-page-added-enabled",
163                                    String.valueOf(emailPageAddedEnabled));
164                            preferences.setValue(
165                                    "email-page-added-subject-prefix", emailPageAddedSubjectPrefix);
166                            preferences.setValue("email-page-added-body", emailPageAddedBody);
167                            preferences.setValue(
168                                    "email-page-added-signature", emailPageAddedSignature);
169                    }
170            }
171    
172            protected void updateEmailPageUpdated(
173                            ActionRequest actionRequest, PortletPreferences preferences)
174                    throws Exception {
175    
176                    boolean emailPageUpdatedEnabled = ParamUtil.getBoolean(
177                            actionRequest, "emailPageUpdatedEnabled");
178                    String emailPageUpdatedSubjectPrefix = ParamUtil.getString(
179                            actionRequest, "emailPageUpdatedSubjectPrefix");
180                    String emailPageUpdatedBody = ParamUtil.getString(
181                            actionRequest, "emailPageUpdatedBody");
182                    String emailPageUpdatedSignature = ParamUtil.getString(
183                            actionRequest, "emailPageUpdatedSignature");
184    
185                    if (Validator.isNull(emailPageUpdatedSubjectPrefix)) {
186                            SessionErrors.add(actionRequest, "emailPageUpdatedSubjectPrefix");
187                    }
188                    else if (Validator.isNull(emailPageUpdatedBody)) {
189                            SessionErrors.add(actionRequest, "emailPageUpdatedBody");
190                    }
191                    else {
192                            preferences.setValue(
193                                    "email-page-updated-enabled",
194                                    String.valueOf(emailPageUpdatedEnabled));
195                            preferences.setValue(
196                                    "email-page-updated-subject-prefix",
197                                    emailPageUpdatedSubjectPrefix);
198                            preferences.setValue(
199                                    "email-page-updated-body", emailPageUpdatedBody);
200                            preferences.setValue(
201                                    "email-page-updated-signature", emailPageUpdatedSignature);
202                    }
203            }
204    
205            protected void updateRSS(
206                            ActionRequest actionRequest, PortletPreferences preferences)
207                    throws Exception {
208    
209                    int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
210                    String rssDisplayStyle = ParamUtil.getString(
211                            actionRequest, "rssDisplayStyle");
212    
213                    preferences.setValue("rss-delta", String.valueOf(rssDelta));
214                    preferences.setValue("rss-display-style", rssDisplayStyle);
215            }
216    
217    }