1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ViewAction extends PortletAction {
66  
67      public void processAction(
68              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
69              ActionRequest actionRequest, ActionResponse actionResponse)
70          throws Exception {
71  
72          List<String> validEmailAddresses = new ArrayList<String>();
73          Set<String> invalidEmailAddresses = new HashSet<String>();
74  
75          int emailMessageMaxRecipients =
76              InvitationUtil.getEmailMessageMaxRecipients();
77  
78          for (int i = 0; i < emailMessageMaxRecipients; i++) {
79              String emailAddress = ParamUtil.getString(
80                  actionRequest, "emailAddress" + i);
81  
82              if (Validator.isEmailAddress(emailAddress)) {
83                  if (!validEmailAddresses.contains(emailAddress)) {
84                      validEmailAddresses.add(emailAddress);
85                  }
86              }
87              else if (Validator.isNotNull(emailAddress)) {
88                  invalidEmailAddresses.add("emailAddress" + i);
89              }
90          }
91  
92          if (validEmailAddresses.isEmpty() && invalidEmailAddresses.isEmpty()) {
93              invalidEmailAddresses.add("emailAddress0");
94          }
95  
96          if (!invalidEmailAddresses.isEmpty()) {
97              SessionErrors.add(
98                  actionRequest, "emailAddresses", invalidEmailAddresses);
99  
100             return;
101         }
102 
103         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
104             WebKeys.THEME_DISPLAY);
105 
106         User user = themeDisplay.getUser();
107 
108         String fromAddress = user.getEmailAddress();
109         String fromName = user.getFullName();
110 
111         InternetAddress from = new InternetAddress(fromAddress, fromName);
112 
113         Layout layout = themeDisplay.getLayout();
114 
115         String portalURL = PortalUtil.getPortalURL(actionRequest);
116 
117         String layoutFullURL = PortalUtil.getLayoutFullURL(
118             layout, themeDisplay);
119 
120         PortletPreferences prefs =
121             PortletPreferencesFactoryUtil.getPortletSetup(
122                 actionRequest, PortletKeys.INVITATION);
123 
124         String subject = InvitationUtil.getEmailMessageSubject(prefs);
125         String body = InvitationUtil.getEmailMessageBody(prefs);
126 
127         subject = StringUtil.replace(
128             subject,
129             new String[] {
130                 "[$FROM_ADDRESS$]",
131                 "[$FROM_NAME$]",
132                 "[$PAGE_URL$]",
133                 "[$PORTAL_URL$]"
134             },
135             new String[] {
136                 fromAddress,
137                 fromName,
138                 layoutFullURL,
139                 portalURL
140             });
141 
142         body = StringUtil.replace(
143             body,
144             new String[] {
145                 "[$FROM_ADDRESS$]",
146                 "[$FROM_NAME$]",
147                 "[$PAGE_URL$]",
148                 "[$PORTAL_URL$]"
149             },
150             new String[] {
151                 fromAddress,
152                 fromName,
153                 layoutFullURL,
154                 portalURL
155             });
156 
157         for (String emailAddress : validEmailAddresses) {
158             InternetAddress to = new InternetAddress(emailAddress);
159 
160             MailMessage message = new MailMessage(
161                 from, to, subject, body, true);
162 
163             MailServiceUtil.sendEmail(message);
164         }
165 
166         SessionMessages.add(actionRequest, "invitationSent");
167 
168         String redirect = ParamUtil.getString(actionRequest, "redirect");
169 
170         actionResponse.sendRedirect(redirect);
171     }
172 
173     public ActionForward render(
174             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
175             RenderRequest renderRequest, RenderResponse renderResponse)
176         throws Exception {
177 
178         return mapping.findForward(
179             getForward(renderRequest, "portlet.invitation.view"));
180     }
181 
182 }