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.invitation.action;
24  
25  import com.liferay.mail.service.MailServiceUtil;
26  import com.liferay.portal.kernel.mail.MailMessage;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.PortletPreferencesFactoryUtil;
38  import com.liferay.portlet.invitation.util.InvitationUtil;
39  import com.liferay.util.CollectionFactory;
40  import com.liferay.util.servlet.SessionErrors;
41  import com.liferay.util.servlet.SessionMessages;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Set;
46  
47  import javax.mail.internet.InternetAddress;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletPreferences;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
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="ViewAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Charles May
64   *
65   */
66  public class ViewAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig config,
70              ActionRequest req, ActionResponse res)
71          throws Exception {
72  
73          List validEmailAddresses = new ArrayList();
74          Set invalidEmailAddresses = CollectionFactory.getHashSet();
75  
76          int emailMessageMaxRecipients =
77              InvitationUtil.getEmailMessageMaxRecipients();
78  
79          for (int i = 0; i < emailMessageMaxRecipients; i++) {
80              String emailAddress = ParamUtil.getString(req, "emailAddress" + i);
81  
82              if (Validator.isEmailAddress(emailAddress)) {
83                  validEmailAddresses.add(emailAddress);
84              }
85              else if (Validator.isNotNull(emailAddress)) {
86                  invalidEmailAddresses.add("emailAddress" + i);
87              }
88          }
89  
90          if (invalidEmailAddresses.size() > 0) {
91              SessionErrors.add(req, "emailAddresses", invalidEmailAddresses);
92  
93              return;
94          }
95  
96          ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
97              WebKeys.THEME_DISPLAY);
98  
99          User user = themeDisplay.getUser();
100 
101         String fromAddress = user.getEmailAddress();
102         String fromName = user.getFullName();
103 
104         InternetAddress from = new InternetAddress(fromAddress, fromName);
105 
106         Layout layout = themeDisplay.getLayout();
107 
108         String portalURL = PortalUtil.getPortalURL(req);
109 
110         String pageURL =
111             portalURL + PortalUtil.getLayoutURL(layout, themeDisplay);
112 
113         PortletPreferences prefs =
114             PortletPreferencesFactoryUtil.getPortletSetup(
115                 req, PortletKeys.INVITATION, true, true);
116 
117         String subject = InvitationUtil.getEmailMessageSubject(prefs);
118         String body = InvitationUtil.getEmailMessageBody(prefs);
119 
120         subject = StringUtil.replace(
121             subject,
122             new String[] {
123                 "[$FROM_ADDRESS$]",
124                 "[$FROM_NAME$]",
125                 "[$PAGE_URL$]",
126                 "[$PORTAL_URL$]"
127             },
128             new String[] {
129                 fromAddress,
130                 fromName,
131                 pageURL,
132                 portalURL
133             });
134 
135         body = StringUtil.replace(
136             body,
137             new String[] {
138                 "[$FROM_ADDRESS$]",
139                 "[$FROM_NAME$]",
140                 "[$PAGE_URL$]",
141                 "[$PORTAL_URL$]"
142             },
143             new String[] {
144                 fromAddress,
145                 fromName,
146                 pageURL,
147                 portalURL
148             });
149 
150         for (int i = 0; i < validEmailAddresses.size(); i++) {
151             String emailAddress = (String)validEmailAddresses.get(i);
152 
153             InternetAddress to = new InternetAddress(emailAddress);
154 
155             MailMessage message = new MailMessage(
156                 from, to, subject, body, true);
157 
158             MailServiceUtil.sendEmail(message);
159         }
160 
161         SessionMessages.add(req, "invitationSent");
162 
163         String redirect = ParamUtil.getString(req, "redirect");
164 
165         res.sendRedirect(redirect);
166     }
167 
168     public ActionForward render(
169             ActionMapping mapping, ActionForm form, PortletConfig config,
170             RenderRequest req, RenderResponse res)
171         throws Exception {
172 
173         return mapping.findForward(getForward(req, "portlet.invitation.view"));
174     }
175 
176 }