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 (invalidEmailAddresses.size() > 0) {
94              SessionErrors.add(
95                  actionRequest, "emailAddresses", invalidEmailAddresses);
96  
97              return;
98          }
99  
100         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
101             WebKeys.THEME_DISPLAY);
102 
103         User user = themeDisplay.getUser();
104 
105         String fromAddress = user.getEmailAddress();
106         String fromName = user.getFullName();
107 
108         InternetAddress from = new InternetAddress(fromAddress, fromName);
109 
110         Layout layout = themeDisplay.getLayout();
111 
112         String portalURL = PortalUtil.getPortalURL(actionRequest);
113 
114         String pageURL =
115             portalURL + PortalUtil.getLayoutURL(layout, themeDisplay);
116 
117         PortletPreferences preferences =
118             PortletPreferencesFactoryUtil.getPortletSetup(
119                 actionRequest, PortletKeys.INVITATION);
120 
121         String subject = InvitationUtil.getEmailMessageSubject(preferences);
122         String body = InvitationUtil.getEmailMessageBody(preferences);
123 
124         subject = StringUtil.replace(
125             subject,
126             new String[] {
127                 "[$FROM_ADDRESS$]",
128                 "[$FROM_NAME$]",
129                 "[$PAGE_URL$]",
130                 "[$PORTAL_URL$]"
131             },
132             new String[] {
133                 fromAddress,
134                 fromName,
135                 pageURL,
136                 portalURL
137             });
138 
139         body = StringUtil.replace(
140             body,
141             new String[] {
142                 "[$FROM_ADDRESS$]",
143                 "[$FROM_NAME$]",
144                 "[$PAGE_URL$]",
145                 "[$PORTAL_URL$]"
146             },
147             new String[] {
148                 fromAddress,
149                 fromName,
150                 pageURL,
151                 portalURL
152             });
153 
154         for (String emailAddress : validEmailAddresses) {
155             InternetAddress to = new InternetAddress(emailAddress);
156 
157             MailMessage message = new MailMessage(
158                 from, to, subject, body, true);
159 
160             MailServiceUtil.sendEmail(message);
161         }
162 
163         SessionMessages.add(actionRequest, "invitationSent");
164 
165         String redirect = ParamUtil.getString(actionRequest, "redirect");
166 
167         actionResponse.sendRedirect(redirect);
168     }
169 
170     public ActionForward render(
171             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
172             RenderRequest renderRequest, RenderResponse renderResponse)
173         throws Exception {
174 
175         return mapping.findForward(
176             getForward(renderRequest, "portlet.invitation.view"));
177     }
178 
179 }