1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.mail.action;
24  
25  import com.liferay.mail.model.Filter;
26  import com.liferay.mail.service.MailServiceUtil;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portlet.mail.util.recipient.RecipientFinder;
38  import com.liferay.portlet.mail.util.recipient.RecipientFinderLocator;
39  import com.liferay.util.servlet.SessionMessages;
40  
41  import java.rmi.RemoteException;
42  
43  import java.util.ArrayList;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.PortletException;
51  import javax.portlet.PortletPreferences;
52  import javax.portlet.RenderRequest;
53  import javax.portlet.RenderResponse;
54  
55  import org.apache.commons.collections.map.MultiValueMap;
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="EditPreferencesAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class EditPreferencesAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig config,
70              ActionRequest req, ActionResponse res)
71          throws Exception {
72  
73          String cmd = ParamUtil.getString(req, Constants.CMD);
74  
75          if (!cmd.equals(Constants.UPDATE)) {
76              return;
77          }
78  
79          long userId = PortalUtil.getUserId(req);
80  
81          PortletPreferences prefs = req.getPreferences();
82  
83          String tabs1 = ParamUtil.getString(req, "tabs1");
84  
85          if (tabs1.equals("recipients")) {
86              List recipientFinders = RecipientFinderLocator.getInstances();
87  
88              for (int i = 0; i < recipientFinders.size(); i++) {
89                  RecipientFinder recipientFinder =
90                      (RecipientFinder)recipientFinders.get(i);
91  
92                  String rfName = recipientFinder.getClass().getName();
93  
94                  boolean enabled = ParamUtil.getBoolean(req, rfName, true);
95  
96                  prefs.setValue(rfName, String.valueOf(enabled));
97  
98                  MultiValueMap options = recipientFinder.getOptions(userId);
99  
100                 Iterator itr = options.keySet().iterator();
101 
102                 while (itr.hasNext()) {
103                     String key =
104                         rfName + StringPool.PERIOD + (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         }
114         else if (tabs1.equals("filters")) {
115             List filters = new ArrayList();
116             List filterObjects = new ArrayList();
117 
118             for (int i = 0; i < 10; i++) {
119                 String emailAddress = ParamUtil.getString(
120                     req, "filterEmailAddress" + i);
121                 String folder = ParamUtil.getString(req, "filterFolder" + i);
122 
123                 if (Validator.isNotNull(emailAddress) &&
124                     Validator.isNotNull(folder)) {
125 
126                     filters.add(emailAddress + "[$FILTER_SEPARATOR$]" + folder);
127                     filterObjects.add(new Filter(emailAddress, folder));
128                 }
129             }
130 
131             prefs.setValues(
132                 "filters", (String[])filters.toArray(new String[0]));
133 
134             String forwardAddress = GetterUtil.getString(
135                 prefs.getValue("forward-address", StringPool.BLANK));
136 
137             List emailAddresses = getEmailAddresses(
138                 forwardAddress, StringPool.SPACE);
139 
140             boolean leaveCopy = GetterUtil.getBoolean(
141                 prefs.getValue("leave-copy", StringPool.BLANK));
142 
143             try {
144                 MailServiceUtil.addForward(
145                     userId, filterObjects, emailAddresses, leaveCopy);
146             }
147             catch (SystemException se) {
148                 throw new PortletException(se);
149             }
150         }
151         else if (tabs1.equals("forward-address")) {
152             String forwardAddress = ParamUtil.getString(req, "forwardAddress");
153 
154             List emailAddresses = getEmailAddresses(forwardAddress, "\n");
155 
156             if (emailAddresses.size() > 0) {
157                 forwardAddress = StringUtil.merge(
158                     emailAddresses, StringPool.SPACE);
159             }
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.getValues("filters", new String[0]);
172 
173             for (int i = 0; i < filters.length; i++) {
174                 String[] kvp = StringUtil.split(
175                     filters[i], "[$FILTER_SEPARATOR$]");
176 
177                 if (kvp.length == 2) {
178                     String emailAddress = kvp[0];
179                     String folder = kvp[1];
180 
181                     if (Validator.isNotNull(emailAddress) &&
182                         Validator.isNotNull(folder)) {
183 
184                         filterObjects.add(new Filter(emailAddress, folder));
185                     }
186                 }
187             }
188 
189             try {
190                 MailServiceUtil.addForward(
191                     userId, filterObjects, emailAddresses, leaveCopy);
192             }
193             catch (SystemException se) {
194                 throw new PortletException(se);
195             }
196 
197             res.setRenderParameter("forwardAddress", forwardAddress);
198         }
199         else if (tabs1.equals("signature")) {
200             String signature = ParamUtil.getString(req, "signature");
201 
202             prefs.setValue("signature", signature);
203         }
204         else if (tabs1.equals("vacation-message")) {
205             String vacationMessage = ParamUtil.getString(
206                 req, "vacationMessage");
207 
208             prefs.setValue("vacation-message", vacationMessage);
209 
210             try {
211                 User user = PortalUtil.getUser(req);
212 
213                 MailServiceUtil.addVacationMessage(
214                     user.getUserId(), user.getEmailAddress(), vacationMessage);
215             }
216             catch (RemoteException re) {
217                 throw new SystemException(re);
218             }
219             catch (SystemException se) {
220                 throw new PortletException(se);
221             }
222         }
223 
224         prefs.store();
225 
226         SessionMessages.add(req, config.getPortletName() + ".doEdit");
227     }
228 
229     public ActionForward render(
230             ActionMapping mapping, ActionForm form, PortletConfig config,
231             RenderRequest req, RenderResponse res)
232         throws Exception {
233 
234         return mapping.findForward("portlet.mail.edit");
235     }
236 
237     protected List getEmailAddresses(String forwardAddress, String delimiter) {
238         String[] forwardAddressArray = StringUtil.split(
239             forwardAddress, delimiter);
240 
241         List emailAddresses = new ArrayList();
242 
243         for (int i = 0; i < forwardAddressArray.length; i++) {
244             if (Validator.isEmailAddress(forwardAddressArray[i])) {
245                 emailAddresses.add(forwardAddressArray[i]);
246             }
247         }
248 
249         return emailAddresses;
250     }
251 
252 }