1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.LocaleUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  import com.liferay.util.LocalizationUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.TreeMap;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  
52  /**
53   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   */
57  public class ConfigurationActionImpl extends BaseConfigurationAction {
58  
59      public void processAction(
60              PortletConfig portletConfig, ActionRequest actionRequest,
61              ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          if (!cmd.equals(Constants.UPDATE)) {
67              return;
68          }
69  
70          String portletResource = ParamUtil.getString(
71              actionRequest, "portletResource");
72  
73          PortletPreferences preferences =
74              PortletPreferencesFactoryUtil.getPortletSetup(
75                  actionRequest, portletResource);
76  
77          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
78  
79          if (tabs2.equals("email-from")) {
80              updateEmailFrom(actionRequest, preferences);
81          }
82          else if (tabs2.equals("general")) {
83              updateGeneral(actionRequest, preferences);
84          }
85          else if (tabs2.equals("message-added-email")) {
86              updateEmailMessageAdded(actionRequest, preferences);
87          }
88          else if (tabs2.equals("message-updated-email")) {
89              updateEmailMessageUpdated(actionRequest, preferences);
90          }
91          else if (tabs2.equals("rss")) {
92              updateRSS(actionRequest, preferences);
93          }
94          else if (tabs2.equals("thread-priorities")) {
95              updateThreadPriorities(actionRequest, preferences);
96          }
97          else if (tabs2.equals("user-ranks")) {
98              updateUserRanks(actionRequest, preferences);
99          }
100 
101         if (SessionErrors.isEmpty(actionRequest)) {
102             preferences.store();
103 
104             SessionMessages.add(
105                 actionRequest, portletConfig.getPortletName() + ".doConfigure");
106         }
107     }
108 
109     public String render(
110             PortletConfig portletConfig, RenderRequest renderRequest,
111             RenderResponse renderResponse)
112         throws Exception {
113 
114         return "/html/portlet/message_boards/configuration.jsp";
115     }
116 
117     protected void updateEmailFrom(
118             ActionRequest actionRequest, PortletPreferences preferences)
119         throws Exception {
120 
121         String emailFromName = ParamUtil.getString(
122             actionRequest, "emailFromName");
123         String emailFromAddress = ParamUtil.getString(
124             actionRequest, "emailFromAddress");
125         boolean emailHtmlFormat = ParamUtil.getBoolean(
126             actionRequest, "emailHtmlFormat");
127 
128         if (Validator.isNull(emailFromName)) {
129             SessionErrors.add(actionRequest, "emailFromName");
130         }
131         else if (!Validator.isEmailAddress(emailFromAddress) &&
132                  !Validator.isVariableTerm(emailFromAddress)) {
133 
134             SessionErrors.add(actionRequest, "emailFromAddress");
135         }
136         else {
137             preferences.setValue("email-from-name", emailFromName);
138             preferences.setValue("email-from-address", emailFromAddress);
139             preferences.setValue(
140                 "email-html-format", String.valueOf(emailHtmlFormat));
141         }
142     }
143 
144     protected void updateEmailMessageAdded(
145             ActionRequest actionRequest, PortletPreferences preferences)
146         throws Exception {
147 
148         boolean emailMessageAddedEnabled = ParamUtil.getBoolean(
149             actionRequest, "emailMessageAddedEnabled");
150         String emailMessageAddedSubjectPrefix = ParamUtil.getString(
151             actionRequest, "emailMessageAddedSubjectPrefix");
152         String emailMessageAddedBody = ParamUtil.getString(
153             actionRequest, "emailMessageAddedBody");
154         String emailMessageAddedSignature = ParamUtil.getString(
155             actionRequest, "emailMessageAddedSignature");
156 
157         if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
158             SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
159         }
160         else if (Validator.isNull(emailMessageAddedBody)) {
161             SessionErrors.add(actionRequest, "emailMessageAddedBody");
162         }
163         else {
164             preferences.setValue(
165                 "email-message-added-enabled",
166                 String.valueOf(emailMessageAddedEnabled));
167             preferences.setValue(
168                 "email-message-added-subject-prefix",
169                 emailMessageAddedSubjectPrefix);
170             preferences.setValue(
171                 "email-message-added-body", emailMessageAddedBody);
172             preferences.setValue(
173                 "email-message-added-signature", emailMessageAddedSignature);
174         }
175     }
176 
177     protected void updateEmailMessageUpdated(
178             ActionRequest actionRequest, PortletPreferences preferences)
179         throws Exception {
180 
181         boolean emailMessageUpdatedEnabled = ParamUtil.getBoolean(
182             actionRequest, "emailMessageUpdatedEnabled");
183         String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
184             actionRequest, "emailMessageUpdatedSubjectPrefix");
185         String emailMessageUpdatedBody = ParamUtil.getString(
186             actionRequest, "emailMessageUpdatedBody");
187         String emailMessageUpdatedSignature = ParamUtil.getString(
188             actionRequest, "emailMessageUpdatedSignature");
189 
190         if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
191             SessionErrors.add(
192                 actionRequest, "emailMessageUpdatedSubjectPrefix");
193         }
194         else if (Validator.isNull(emailMessageUpdatedBody)) {
195             SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
196         }
197         else {
198             preferences.setValue(
199                 "email-message-updated-enabled",
200                 String.valueOf(emailMessageUpdatedEnabled));
201             preferences.setValue(
202                 "email-message-updated-subject-prefix",
203                 emailMessageUpdatedSubjectPrefix);
204             preferences.setValue(
205                 "email-message-updated-body", emailMessageUpdatedBody);
206             preferences.setValue(
207                 "email-message-updated-signature",
208                 emailMessageUpdatedSignature);
209         }
210     }
211 
212     protected void updateGeneral(
213             ActionRequest actionRequest, PortletPreferences preferences)
214         throws Exception {
215 
216         String allowAnonymousPosting = ParamUtil.getString(
217             actionRequest, "allowAnonymousPosting");
218         String enableFlags = ParamUtil.getString(actionRequest, "enableFlags");
219         boolean enableRatings = ParamUtil.getBoolean(
220             actionRequest, "enableRatings");
221 
222         preferences.setValue("allow-anonymous-posting", allowAnonymousPosting);
223         preferences.setValue("enable-flags", enableFlags);
224         preferences.setValue(
225             "enable-message-ratings", String.valueOf(enableRatings));
226     }
227 
228     protected void updateRSS(
229             ActionRequest actionRequest, PortletPreferences preferences)
230         throws Exception {
231 
232         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
233         String rssDisplayStyle = ParamUtil.getString(
234             actionRequest, "rssDisplayStyle");
235         String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
236 
237         preferences.setValue("rss-delta", String.valueOf(rssDelta));
238         preferences.setValue("rss-display-style", rssDisplayStyle);
239         preferences.setValue("rss-format", rssFormat);
240     }
241 
242     protected void updateThreadPriorities(
243             ActionRequest actionRequest, PortletPreferences preferences)
244         throws Exception {
245 
246         Locale[] locales = LanguageUtil.getAvailableLocales();
247 
248         for (int i = 0; i < locales.length; i++) {
249             String languageId = LocaleUtil.toLanguageId(locales[i]);
250 
251             List<String> priorities = new ArrayList<String>();
252 
253             for (int j = 0; j < 10; j++) {
254                 String name = ParamUtil.getString(
255                     actionRequest, "priorityName" + j + "_" + languageId);
256                 String image = ParamUtil.getString(
257                     actionRequest, "priorityImage" + j + "_" + languageId);
258                 double value = ParamUtil.getDouble(
259                     actionRequest, "priorityValue" + j + "_" + languageId);
260 
261                 if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
262                     (value != 0.0)) {
263 
264                     priorities.add(
265                         name + StringPool.COMMA + image + StringPool.COMMA +
266                             value);
267                 }
268             }
269 
270             LocalizationUtil.setPreferencesValues(
271                 preferences, "priorities", languageId,
272                 priorities.toArray(new String[priorities.size()]));
273         }
274     }
275 
276     protected void updateUserRanks(
277             ActionRequest actionRequest, PortletPreferences preferences)
278         throws Exception {
279 
280         Locale[] locales = LanguageUtil.getAvailableLocales();
281 
282         for (int i = 0; i < locales.length; i++) {
283             String languageId = LocaleUtil.toLanguageId(locales[i]);
284 
285             String[] ranks = StringUtil.split(
286                 ParamUtil.getString(actionRequest, "ranks_" + languageId),
287                 StringPool.NEW_LINE);
288 
289             Map<String, String> map = new TreeMap<String, String>();
290 
291             for (int j = 0; j < ranks.length; j++) {
292                 String[] kvp = StringUtil.split(ranks[j], StringPool.EQUAL);
293 
294                 String kvpName = kvp[0];
295                 String kvpValue = kvp[1];
296 
297                 map.put(kvpValue, kvpName);
298             }
299 
300             ranks = new String[map.size()];
301 
302             int count = 0;
303 
304             Iterator<Map.Entry<String, String>> itr =
305                 map.entrySet().iterator();
306 
307             while (itr.hasNext()) {
308                 Map.Entry<String, String> entry = itr.next();
309 
310                 String kvpValue = entry.getKey();
311                 String kvpName = entry.getValue();
312 
313                 ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
314             }
315 
316             LocalizationUtil.setPreferencesValues(
317                 preferences, "ranks", languageId, ranks);
318         }
319     }
320 
321 }