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.mail.action;
022:
023: import com.liferay.mail.model.Filter;
024: import com.liferay.mail.service.MailServiceUtil;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.util.Constants;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.ParamUtil;
029: import com.liferay.portal.kernel.util.StringPool;
030: import com.liferay.portal.kernel.util.StringUtil;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.User;
033: import com.liferay.portal.struts.PortletAction;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portlet.mail.util.recipient.RecipientFinder;
036: import com.liferay.portlet.mail.util.recipient.RecipientFinderLocator;
037: import com.liferay.util.servlet.SessionMessages;
038:
039: import java.rmi.RemoteException;
040:
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import javax.portlet.ActionRequest;
046: import javax.portlet.ActionResponse;
047: import javax.portlet.PortletConfig;
048: import javax.portlet.PortletException;
049: import javax.portlet.PortletPreferences;
050: import javax.portlet.RenderRequest;
051: import javax.portlet.RenderResponse;
052:
053: import org.apache.commons.collections.map.MultiValueMap;
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * <a href="EditPreferencesAction.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Brian Wing Shun Chan
062: *
063: */
064: public class EditPreferencesAction extends PortletAction {
065:
066: public void processAction(ActionMapping mapping, ActionForm form,
067: PortletConfig config, ActionRequest req, ActionResponse res)
068: throws Exception {
069:
070: String cmd = ParamUtil.getString(req, Constants.CMD);
071:
072: if (!cmd.equals(Constants.UPDATE)) {
073: return;
074: }
075:
076: long userId = PortalUtil.getUserId(req);
077:
078: PortletPreferences prefs = req.getPreferences();
079:
080: String tabs1 = ParamUtil.getString(req, "tabs1");
081:
082: if (tabs1.equals("recipients")) {
083: List recipientFinders = RecipientFinderLocator
084: .getInstances();
085:
086: for (int i = 0; i < recipientFinders.size(); i++) {
087: RecipientFinder recipientFinder = (RecipientFinder) recipientFinders
088: .get(i);
089:
090: String rfName = recipientFinder.getClass().getName();
091:
092: boolean enabled = ParamUtil.getBoolean(req, rfName,
093: true);
094:
095: prefs.setValue(rfName, String.valueOf(enabled));
096:
097: MultiValueMap options = recipientFinder
098: .getOptions(userId);
099:
100: Iterator itr = options.keySet().iterator();
101:
102: while (itr.hasNext()) {
103: String key = rfName + StringPool.PERIOD
104: + (String) itr.next();
105:
106: String value = ParamUtil.getString(req, key);
107:
108: if (Validator.isNotNull(key)) {
109: prefs.setValue(key, value);
110: }
111: }
112: }
113: } else if (tabs1.equals("filters")) {
114: List filters = new ArrayList();
115: List filterObjects = new ArrayList();
116:
117: for (int i = 0; i < 10; i++) {
118: String emailAddress = ParamUtil.getString(req,
119: "filterEmailAddress" + i);
120: String folder = ParamUtil.getString(req, "filterFolder"
121: + i);
122:
123: if (Validator.isNotNull(emailAddress)
124: && Validator.isNotNull(folder)) {
125:
126: filters.add(emailAddress + "[$FILTER_SEPARATOR$]"
127: + folder);
128: filterObjects.add(new Filter(emailAddress, folder));
129: }
130: }
131:
132: prefs.setValues("filters", (String[]) filters
133: .toArray(new String[0]));
134:
135: String forwardAddress = GetterUtil.getString(prefs
136: .getValue("forward-address", StringPool.BLANK));
137:
138: List emailAddresses = getEmailAddresses(forwardAddress,
139: StringPool.SPACE);
140:
141: boolean leaveCopy = GetterUtil.getBoolean(prefs.getValue(
142: "leave-copy", StringPool.BLANK));
143:
144: try {
145: MailServiceUtil.addForward(userId, filterObjects,
146: emailAddresses, leaveCopy);
147: } catch (SystemException se) {
148: throw new PortletException(se);
149: }
150: } else if (tabs1.equals("forward-address")) {
151: String forwardAddress = ParamUtil.getString(req,
152: "forwardAddress");
153:
154: List emailAddresses = getEmailAddresses(forwardAddress,
155: "\n");
156:
157: if (emailAddresses.size() > 0) {
158: forwardAddress = StringUtil.merge(emailAddresses,
159: StringPool.SPACE);
160: } else {
161: forwardAddress = StringPool.BLANK;
162: }
163:
164: boolean leaveCopy = ParamUtil.getBoolean(req, "leaveCopy");
165:
166: prefs.setValue("forward-address", forwardAddress);
167: prefs.setValue("leave-copy", String.valueOf(leaveCopy));
168:
169: List filterObjects = new ArrayList();
170:
171: String[] filters = prefs
172: .getValues("filters", new String[0]);
173:
174: for (int i = 0; i < filters.length; i++) {
175: String[] kvp = StringUtil.split(filters[i],
176: "[$FILTER_SEPARATOR$]");
177:
178: if (kvp.length == 2) {
179: String emailAddress = kvp[0];
180: String folder = kvp[1];
181:
182: if (Validator.isNotNull(emailAddress)
183: && Validator.isNotNull(folder)) {
184:
185: filterObjects.add(new Filter(emailAddress,
186: folder));
187: }
188: }
189: }
190:
191: try {
192: MailServiceUtil.addForward(userId, filterObjects,
193: emailAddresses, leaveCopy);
194: } catch (SystemException se) {
195: throw new PortletException(se);
196: }
197:
198: res.setRenderParameter("forwardAddress", forwardAddress);
199: } else if (tabs1.equals("signature")) {
200: String signature = ParamUtil.getString(req, "signature");
201:
202: prefs.setValue("signature", signature);
203: } else if (tabs1.equals("vacation-message")) {
204: String vacationMessage = ParamUtil.getString(req,
205: "vacationMessage");
206:
207: prefs.setValue("vacation-message", vacationMessage);
208:
209: try {
210: User user = PortalUtil.getUser(req);
211:
212: MailServiceUtil.addVacationMessage(user.getUserId(),
213: user.getEmailAddress(), vacationMessage);
214: } catch (RemoteException re) {
215: throw new SystemException(re);
216: } catch (SystemException se) {
217: throw new PortletException(se);
218: }
219: }
220:
221: prefs.store();
222:
223: SessionMessages.add(req, config.getPortletName() + ".doEdit");
224: }
225:
226: public ActionForward render(ActionMapping mapping, ActionForm form,
227: PortletConfig config, RenderRequest req, RenderResponse res)
228: throws Exception {
229:
230: return mapping.findForward("portlet.mail.edit");
231: }
232:
233: protected List getEmailAddresses(String forwardAddress,
234: String delimiter) {
235: String[] forwardAddressArray = StringUtil.split(forwardAddress,
236: delimiter);
237:
238: List emailAddresses = new ArrayList();
239:
240: for (int i = 0; i < forwardAddressArray.length; i++) {
241: if (Validator.isEmailAddress(forwardAddressArray[i])) {
242: emailAddresses.add(forwardAddressArray[i]);
243: }
244: }
245:
246: return emailAddresses;
247: }
248:
249: }
|