001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.action;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.portlet.ConfigurationAction;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.LocaleUtil;
027: import com.liferay.portal.kernel.util.ParamUtil;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portlet.PortletPreferencesFactoryUtil;
032: import com.liferay.util.LocalizationUtil;
033: import com.liferay.util.servlet.SessionErrors;
034: import com.liferay.util.servlet.SessionMessages;
035:
036: import java.util.ArrayList;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.Locale;
040: import java.util.Map;
041: import java.util.TreeMap;
042:
043: import javax.portlet.ActionRequest;
044: import javax.portlet.ActionResponse;
045: import javax.portlet.PortletConfig;
046: import javax.portlet.PortletPreferences;
047: import javax.portlet.RenderRequest;
048: import javax.portlet.RenderResponse;
049:
050: /**
051: * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class ConfigurationActionImpl implements ConfigurationAction {
057:
058: public void processAction(PortletConfig config, ActionRequest req,
059: ActionResponse res) throws Exception {
060:
061: String cmd = ParamUtil.getString(req, Constants.CMD);
062:
063: if (!cmd.equals(Constants.UPDATE)) {
064: return;
065: }
066:
067: String portletResource = ParamUtil.getString(req,
068: "portletResource");
069:
070: PortletPreferences prefs = PortletPreferencesFactoryUtil
071: .getPortletSetup(req, portletResource, false, true);
072:
073: String tabs2 = ParamUtil.getString(req, "tabs2");
074:
075: if (tabs2.equals("anonymous-posting")) {
076: updateAnonymousPosting(req, prefs);
077: } else if (tabs2.equals("email-from")) {
078: updateEmailFrom(req, prefs);
079: } else if (tabs2.equals("message-added-email")) {
080: updateEmailMessageAdded(req, prefs);
081: } else if (tabs2.equals("message-updated-email")) {
082: updateEmailMessageUpdated(req, prefs);
083: } else if (tabs2.equals("rss")) {
084: updateRSS(req, prefs);
085: } else if (tabs2.equals("thread-priorities")) {
086: updateThreadPriorities(req, prefs);
087: } else if (tabs2.equals("user-ranks")) {
088: updateUserRanks(req, prefs);
089: }
090:
091: if (SessionErrors.isEmpty(req)) {
092: prefs.store();
093:
094: SessionMessages.add(req, config.getPortletName()
095: + ".doConfigure");
096: }
097: }
098:
099: public String render(PortletConfig config, RenderRequest req,
100: RenderResponse res) throws Exception {
101:
102: return "/html/portlet/message_boards/configuration.jsp";
103: }
104:
105: protected void updateAnonymousPosting(ActionRequest req,
106: PortletPreferences prefs) throws Exception {
107:
108: String allowAnonymousPosting = ParamUtil.getString(req,
109: "allowAnonymousPosting");
110:
111: prefs
112: .setValue("allow-anonymous-posting",
113: allowAnonymousPosting);
114: }
115:
116: protected void updateEmailFrom(ActionRequest req,
117: PortletPreferences prefs) throws Exception {
118:
119: String emailFromName = ParamUtil
120: .getString(req, "emailFromName");
121: String emailFromAddress = ParamUtil.getString(req,
122: "emailFromAddress");
123:
124: if (Validator.isNull(emailFromName)) {
125: SessionErrors.add(req, "emailFromName");
126: } else if (!Validator.isEmailAddress(emailFromAddress)
127: && !Validator.isVariableTerm(emailFromAddress)) {
128:
129: SessionErrors.add(req, "emailFromAddress");
130: } else {
131: prefs.setValue("email-from-name", emailFromName);
132: prefs.setValue("email-from-address", emailFromAddress);
133: }
134: }
135:
136: protected void updateEmailMessageAdded(ActionRequest req,
137: PortletPreferences prefs) throws Exception {
138:
139: String emailMessageAddedEnabled = ParamUtil.getString(req,
140: "emailMessageAddedEnabled");
141: String emailMessageAddedSubjectPrefix = ParamUtil.getString(
142: req, "emailMessageAddedSubjectPrefix");
143: String emailMessageAddedBody = ParamUtil.getString(req,
144: "emailMessageAddedBody");
145: String emailMessageAddedSignature = ParamUtil.getString(req,
146: "emailMessageAddedSignature");
147:
148: if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
149: SessionErrors.add(req, "emailMessageAddedSubjectPrefix");
150: } else if (Validator.isNull(emailMessageAddedBody)) {
151: SessionErrors.add(req, "emailMessageAddedBody");
152: } else {
153: prefs.setValue("email-message-added-enabled",
154: emailMessageAddedEnabled);
155: prefs.setValue("email-message-added-subject-prefix",
156: emailMessageAddedSubjectPrefix);
157: prefs.setValue("email-message-added-body",
158: emailMessageAddedBody);
159: prefs.setValue("email-message-added-signature",
160: emailMessageAddedSignature);
161: }
162: }
163:
164: protected void updateEmailMessageUpdated(ActionRequest req,
165: PortletPreferences prefs) throws Exception {
166:
167: String emailMessageUpdatedEnabled = ParamUtil.getString(req,
168: "emailMessageUpdatedEnabled");
169: String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
170: req, "emailMessageUpdatedSubjectPrefix");
171: String emailMessageUpdatedBody = ParamUtil.getString(req,
172: "emailMessageUpdatedBody");
173: String emailMessageUpdatedSignature = ParamUtil.getString(req,
174: "emailMessageUpdatedSignature");
175:
176: if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
177: SessionErrors.add(req, "emailMessageUpdatedSubjectPrefix");
178: } else if (Validator.isNull(emailMessageUpdatedBody)) {
179: SessionErrors.add(req, "emailMessageUpdatedBody");
180: } else {
181: prefs.setValue("email-message-updated-enabled",
182: emailMessageUpdatedEnabled);
183: prefs.setValue("email-message-updated-subject-prefix",
184: emailMessageUpdatedSubjectPrefix);
185: prefs.setValue("email-message-updated-body",
186: emailMessageUpdatedBody);
187: prefs.setValue("email-message-updated-signature",
188: emailMessageUpdatedSignature);
189: }
190: }
191:
192: protected void updateRSS(ActionRequest req, PortletPreferences prefs)
193: throws Exception {
194:
195: int rssDelta = ParamUtil.getInteger(req, "rssDelta");
196: String rssDisplayStyle = ParamUtil.getString(req,
197: "rssDisplayStyle");
198: String rssFormat = ParamUtil.getString(req, "rssFormat");
199:
200: prefs.setValue("rss-delta", String.valueOf(rssDelta));
201: prefs.setValue("rss-display-style", rssDisplayStyle);
202: prefs.setValue("rss-format", rssFormat);
203: }
204:
205: protected void updateThreadPriorities(ActionRequest req,
206: PortletPreferences prefs) throws Exception {
207:
208: Locale[] locales = LanguageUtil.getAvailableLocales();
209:
210: for (int i = 0; i < locales.length; i++) {
211: String languageId = LocaleUtil.toLanguageId(locales[i]);
212:
213: List priorities = new ArrayList();
214:
215: for (int j = 0; j < 10; j++) {
216: String name = ParamUtil.getString(req, "priorityName"
217: + j + "_" + languageId);
218: String image = ParamUtil.getString(req, "priorityImage"
219: + j + "_" + languageId);
220: double value = ParamUtil.getDouble(req, "priorityValue"
221: + j + "_" + languageId);
222:
223: if (Validator.isNotNull(name)
224: || Validator.isNotNull(image) || (value != 0.0)) {
225:
226: priorities.add(name + StringPool.COMMA + image
227: + StringPool.COMMA + value);
228: }
229: }
230:
231: LocalizationUtil.setPrefsValues(prefs, "priorities",
232: languageId, (String[]) priorities
233: .toArray(new String[0]));
234: }
235: }
236:
237: protected void updateUserRanks(ActionRequest req,
238: PortletPreferences prefs) throws Exception {
239:
240: Locale[] locales = LanguageUtil.getAvailableLocales();
241:
242: for (int i = 0; i < locales.length; i++) {
243: String languageId = LocaleUtil.toLanguageId(locales[i]);
244:
245: String[] ranks = StringUtil.split(ParamUtil.getString(req,
246: "ranks_" + languageId), StringPool.NEW_LINE);
247:
248: Map map = new TreeMap();
249:
250: for (int j = 0; j < ranks.length; j++) {
251: String[] kvp = StringUtil.split(ranks[j],
252: StringPool.EQUAL);
253:
254: String kvpName = kvp[0];
255: String kvpValue = kvp[1];
256:
257: map.put(kvpValue, kvpName);
258: }
259:
260: ranks = new String[map.size()];
261:
262: int count = 0;
263:
264: Iterator itr = map.entrySet().iterator();
265:
266: while (itr.hasNext()) {
267: Map.Entry entry = (Map.Entry) itr.next();
268:
269: String kvpValue = (String) entry.getKey();
270: String kvpName = (String) entry.getValue();
271:
272: ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
273: }
274:
275: LocalizationUtil.setPrefsValues(prefs, "ranks", languageId,
276: ranks);
277: }
278: }
279:
280: }
|