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