1   /**
2    * Copyright (c) 2000-2009 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.servlet.SessionErrors;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.PortletPreferencesFactoryUtil;
40  import com.liferay.portlet.invitation.util.InvitationUtil;
41  
42  import java.util.ArrayList;
43  import java.util.HashSet;
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 portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          List<String> validEmailAddresses = new ArrayList<String>();
74          Set<String> invalidEmailAddresses = new HashSet<String>();
75  
76          int emailMessageMaxRecipients =
77              InvitationUtil.getEmailMessageMaxRecipients();
78  
79          for (int i = 0; i < emailMessageMaxRecipients; i++) {
80              String emailAddress = ParamUtil.getString(
81                  actionRequest, "emailAddress" + i);
82  
83              if (Validator.isEmailAddress(emailAddress)) {
84                  if (!validEmailAddresses.contains(emailAddress)) {
85                      validEmailAddresses.add(emailAddress);
86                  }
87              }
88              else if (Validator.isNotNull(emailAddress)) {
89                  invalidEmailAddresses.add("emailAddress" + i);
90              }
91          }
92  
93          if (validEmailAddresses.isEmpty() && invalidEmailAddresses.isEmpty()) {
94              invalidEmailAddresses.add("emailAddress0");
95          }
96  
97          if (!invalidEmailAddresses.isEmpty()) {
98              SessionErrors.add(
99                  actionRequest, "emailAddresses", invalidEmailAddresses);
100 
101             return;
102         }
103 
104         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
105             WebKeys.THEME_DISPLAY);
106 
107         User user = themeDisplay.getUser();
108 
109         String fromAddress = user.getEmailAddress();
110         String fromName = user.getFullName();
111 
112         InternetAddress from = new InternetAddress(fromAddress, fromName);
113 
114         Layout layout = themeDisplay.getLayout();
115 
116         String portalURL = PortalUtil.getPortalURL(actionRequest);
117 
118         String pageURL =
119             portalURL + PortalUtil.getLayoutURL(layout, themeDisplay);
120 
121         PortletPreferences preferences =
122             PortletPreferencesFactoryUtil.getPortletSetup(
123                 actionRequest, PortletKeys.INVITATION);
124 
125         String subject = InvitationUtil.getEmailMessageSubject(preferences);
126         String body = InvitationUtil.getEmailMessageBody(preferences);
127 
128         subject = StringUtil.replace(
129             subject,
130             new String[] {
131                 "[$FROM_ADDRESS$]",
132                 "[$FROM_NAME$]",
133                 "[$PAGE_URL$]",
134                 "[$PORTAL_URL$]"
135             },
136             new String[] {
137                 fromAddress,
138                 fromName,
139                 pageURL,
140                 portalURL
141             });
142 
143         body = StringUtil.replace(
144             body,
145             new String[] {
146                 "[$FROM_ADDRESS$]",
147                 "[$FROM_NAME$]",
148                 "[$PAGE_URL$]",
149                 "[$PORTAL_URL$]"
150             },
151             new String[] {
152                 fromAddress,
153                 fromName,
154                 pageURL,
155                 portalURL
156             });
157 
158         for (String emailAddress : validEmailAddresses) {
159             InternetAddress to = new InternetAddress(emailAddress);
160 
161             MailMessage message = new MailMessage(
162                 from, to, subject, body, true);
163 
164             MailServiceUtil.sendEmail(message);
165         }
166 
167         SessionMessages.add(actionRequest, "invitationSent");
168 
169         String redirect = ParamUtil.getString(actionRequest, "redirect");
170 
171         actionResponse.sendRedirect(redirect);
172     }
173 
174     public ActionForward render(
175             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
176             RenderRequest renderRequest, RenderResponse renderResponse)
177         throws Exception {
178 
179         return mapping.findForward(
180             getForward(renderRequest, "portlet.invitation.view"));
181     }
182 
183 }