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 prefs =
74              PortletPreferencesFactoryUtil.getPortletSetup(
75                  actionRequest, portletResource);
76  
77          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
78  
79          if (tabs2.equals("anonymous-posting")) {
80              updateAnonymousPosting(actionRequest, prefs);
81          }
82          else if (tabs2.equals("email-from")) {
83              updateEmailFrom(actionRequest, prefs);
84          }
85          else if (tabs2.equals("message-added-email")) {
86              updateEmailMessageAdded(actionRequest, prefs);
87          }
88          else if (tabs2.equals("message-updated-email")) {
89              updateEmailMessageUpdated(actionRequest, prefs);
90          }
91          else if (tabs2.equals("ratings")) {
92              updateRatings(actionRequest, prefs);
93          }
94          else if (tabs2.equals("rss")) {
95              updateRSS(actionRequest, prefs);
96          }
97          else if (tabs2.equals("thread-priorities")) {
98              updateThreadPriorities(actionRequest, prefs);
99          }
100         else if (tabs2.equals("user-ranks")) {
101             updateUserRanks(actionRequest, prefs);
102         }
103 
104         if (SessionErrors.isEmpty(actionRequest)) {
105             prefs.store();
106 
107             SessionMessages.add(
108                 actionRequest, portletConfig.getPortletName() + ".doConfigure");
109         }
110     }
111 
112     public String render(
113             PortletConfig portletConfig, RenderRequest renderRequest,
114             RenderResponse renderResponse)
115         throws Exception {
116 
117         return "/html/portlet/message_boards/configuration.jsp";
118     }
119 
120     protected void updateAnonymousPosting(
121             ActionRequest actionRequest, PortletPreferences prefs)
122         throws Exception {
123 
124         String allowAnonymousPosting = ParamUtil.getString(
125             actionRequest, "allowAnonymousPosting");
126 
127         prefs.setValue("allow-anonymous-posting", allowAnonymousPosting);
128     }
129 
130     protected void updateEmailFrom(
131             ActionRequest actionRequest, PortletPreferences prefs)
132         throws Exception {
133 
134         String emailFromName = ParamUtil.getString(
135             actionRequest, "emailFromName");
136         String emailFromAddress = ParamUtil.getString(
137             actionRequest, "emailFromAddress");
138         boolean emailHtmlFormat = ParamUtil.getBoolean(
139             actionRequest, "emailHtmlFormat");
140 
141         if (Validator.isNull(emailFromName)) {
142             SessionErrors.add(actionRequest, "emailFromName");
143         }
144         else if (!Validator.isEmailAddress(emailFromAddress) &&
145                  !Validator.isVariableTerm(emailFromAddress)) {
146 
147             SessionErrors.add(actionRequest, "emailFromAddress");
148         }
149         else {
150             prefs.setValue("email-from-name", emailFromName);
151             prefs.setValue("email-from-address", emailFromAddress);
152             prefs.setValue(
153                 "email-html-format", String.valueOf(emailHtmlFormat));
154         }
155     }
156 
157     protected void updateEmailMessageAdded(
158             ActionRequest actionRequest, PortletPreferences prefs)
159         throws Exception {
160 
161         boolean emailMessageAddedEnabled = ParamUtil.getBoolean(
162             actionRequest, "emailMessageAddedEnabled");
163         String emailMessageAddedSubjectPrefix = ParamUtil.getString(
164             actionRequest, "emailMessageAddedSubjectPrefix");
165         String emailMessageAddedBody = ParamUtil.getString(
166             actionRequest, "emailMessageAddedBody");
167         String emailMessageAddedSignature = ParamUtil.getString(
168             actionRequest, "emailMessageAddedSignature");
169 
170         if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
171             SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
172         }
173         else if (Validator.isNull(emailMessageAddedBody)) {
174             SessionErrors.add(actionRequest, "emailMessageAddedBody");
175         }
176         else {
177             prefs.setValue(
178                 "email-message-added-enabled",
179                 String.valueOf(emailMessageAddedEnabled));
180             prefs.setValue(
181                 "email-message-added-subject-prefix",
182                 emailMessageAddedSubjectPrefix);
183             prefs.setValue("email-message-added-body", emailMessageAddedBody);
184             prefs.setValue(
185                 "email-message-added-signature", emailMessageAddedSignature);
186         }
187     }
188 
189     protected void updateEmailMessageUpdated(
190             ActionRequest actionRequest, PortletPreferences prefs)
191         throws Exception {
192 
193         boolean emailMessageUpdatedEnabled = ParamUtil.getBoolean(
194             actionRequest, "emailMessageUpdatedEnabled");
195         String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
196             actionRequest, "emailMessageUpdatedSubjectPrefix");
197         String emailMessageUpdatedBody = ParamUtil.getString(
198             actionRequest, "emailMessageUpdatedBody");
199         String emailMessageUpdatedSignature = ParamUtil.getString(
200             actionRequest, "emailMessageUpdatedSignature");
201 
202         if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
203             SessionErrors.add(
204                 actionRequest, "emailMessageUpdatedSubjectPrefix");
205         }
206         else if (Validator.isNull(emailMessageUpdatedBody)) {
207             SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
208         }
209         else {
210             prefs.setValue(
211                 "email-message-updated-enabled",
212                 String.valueOf(emailMessageUpdatedEnabled));
213             prefs.setValue(
214                 "email-message-updated-subject-prefix",
215                 emailMessageUpdatedSubjectPrefix);
216             prefs.setValue(
217                 "email-message-updated-body", emailMessageUpdatedBody);
218             prefs.setValue(
219                 "email-message-updated-signature",
220                 emailMessageUpdatedSignature);
221         }
222     }
223 
224     protected void updateRatings(
225             ActionRequest actionRequest, PortletPreferences prefs)
226         throws Exception {
227 
228         boolean enableMessageRatings = ParamUtil.getBoolean(
229             actionRequest, "enableMessageRatings");
230 
231         prefs.setValue(
232             "enable-message-ratings", String.valueOf(enableMessageRatings));
233     }
234 
235     protected void updateRSS(
236             ActionRequest actionRequest, PortletPreferences prefs)
237         throws Exception {
238 
239         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
240         String rssDisplayStyle = ParamUtil.getString(
241             actionRequest, "rssDisplayStyle");
242         String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
243 
244         prefs.setValue("rss-delta", String.valueOf(rssDelta));
245         prefs.setValue("rss-display-style", rssDisplayStyle);
246         prefs.setValue("rss-format", rssFormat);
247     }
248 
249     protected void updateThreadPriorities(
250             ActionRequest actionRequest, PortletPreferences prefs)
251         throws Exception {
252 
253         Locale[] locales = LanguageUtil.getAvailableLocales();
254 
255         for (int i = 0; i < locales.length; i++) {
256             String languageId = LocaleUtil.toLanguageId(locales[i]);
257 
258             List<String> priorities = new ArrayList<String>();
259 
260             for (int j = 0; j < 10; j++) {
261                 String name = ParamUtil.getString(
262                     actionRequest, "priorityName" + j + "_" + languageId);
263                 String image = ParamUtil.getString(
264                     actionRequest, "priorityImage" + j + "_" + languageId);
265                 double value = ParamUtil.getDouble(
266                     actionRequest, "priorityValue" + j + "_" + languageId);
267 
268                 if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
269                     (value != 0.0)) {
270 
271                     priorities.add(
272                         name + StringPool.COMMA + image + StringPool.COMMA +
273                             value);
274                 }
275             }
276 
277             LocalizationUtil.setPreferencesValues(
278                 prefs, "priorities", languageId,
279                 priorities.toArray(new String[priorities.size()]));
280         }
281     }
282 
283     protected void updateUserRanks(
284             ActionRequest actionRequest, PortletPreferences prefs)
285         throws Exception {
286 
287         Locale[] locales = LanguageUtil.getAvailableLocales();
288 
289         for (int i = 0; i < locales.length; i++) {
290             String languageId = LocaleUtil.toLanguageId(locales[i]);
291 
292             String[] ranks = StringUtil.split(
293                 ParamUtil.getString(actionRequest, "ranks_" + languageId),
294                 StringPool.NEW_LINE);
295 
296             Map<String, String> map = new TreeMap<String, String>();
297 
298             for (int j = 0; j < ranks.length; j++) {
299                 String[] kvp = StringUtil.split(ranks[j], StringPool.EQUAL);
300 
301                 String kvpName = kvp[0];
302                 String kvpValue = kvp[1];
303 
304                 map.put(kvpValue, kvpName);
305             }
306 
307             ranks = new String[map.size()];
308 
309             int count = 0;
310 
311             Iterator<Map.Entry<String, String>> itr =
312                 map.entrySet().iterator();
313 
314             while (itr.hasNext()) {
315                 Map.Entry<String, String> entry = itr.next();
316 
317                 String kvpValue = entry.getKey();
318                 String kvpName = entry.getValue();
319 
320                 ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
321             }
322 
323             LocalizationUtil.setPreferencesValues(
324                 prefs, "ranks", languageId, ranks);
325         }
326     }
327 
328 }