1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.invitation.action;
21  
22  import com.liferay.mail.service.MailServiceUtil;
23  import com.liferay.portal.kernel.mail.MailMessage;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.servlet.SessionMessages;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.PortletPreferencesFactoryUtil;
37  import com.liferay.portlet.invitation.util.InvitationUtil;
38  
39  import java.util.ArrayList;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Set;
43  
44  import javax.mail.internet.InternetAddress;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
49  import javax.portlet.PortletPreferences;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Charles May
61   *
62   */
63  public class ViewAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          List<String> validEmailAddresses = new ArrayList<String>();
71          Set<String> invalidEmailAddresses = new HashSet<String>();
72  
73          int emailMessageMaxRecipients =
74              InvitationUtil.getEmailMessageMaxRecipients();
75  
76          for (int i = 0; i < emailMessageMaxRecipients; i++) {
77              String emailAddress = ParamUtil.getString(
78                  actionRequest, "emailAddress" + i);
79  
80              if (Validator.isEmailAddress(emailAddress)) {
81                  if (!validEmailAddresses.contains(emailAddress)) {
82                      validEmailAddresses.add(emailAddress);
83                  }
84              }
85              else if (Validator.isNotNull(emailAddress)) {
86                  invalidEmailAddresses.add("emailAddress" + i);
87              }
88          }
89  
90          if (validEmailAddresses.isEmpty() && invalidEmailAddresses.isEmpty()) {
91              invalidEmailAddresses.add("emailAddress0");
92          }
93  
94          if (!invalidEmailAddresses.isEmpty()) {
95              SessionErrors.add(
96                  actionRequest, "emailAddresses", invalidEmailAddresses);
97  
98              return;
99          }
100 
101         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
102             WebKeys.THEME_DISPLAY);
103 
104         User user = themeDisplay.getUser();
105 
106         String fromAddress = user.getEmailAddress();
107         String fromName = user.getFullName();
108 
109         InternetAddress from = new InternetAddress(fromAddress, fromName);
110 
111         Layout layout = themeDisplay.getLayout();
112 
113         String portalURL = PortalUtil.getPortalURL(actionRequest);
114 
115         String pageURL =
116             portalURL + PortalUtil.getLayoutURL(layout, themeDisplay);
117 
118         PortletPreferences prefs =
119             PortletPreferencesFactoryUtil.getPortletSetup(
120                 actionRequest, PortletKeys.INVITATION);
121 
122         String subject = InvitationUtil.getEmailMessageSubject(prefs);
123         String body = InvitationUtil.getEmailMessageBody(prefs);
124 
125         subject = StringUtil.replace(
126             subject,
127             new String[] {
128                 "[$FROM_ADDRESS$]",
129                 "[$FROM_NAME$]",
130                 "[$PAGE_URL$]",
131                 "[$PORTAL_URL$]"
132             },
133             new String[] {
134                 fromAddress,
135                 fromName,
136                 pageURL,
137                 portalURL
138             });
139 
140         body = StringUtil.replace(
141             body,
142             new String[] {
143                 "[$FROM_ADDRESS$]",
144                 "[$FROM_NAME$]",
145                 "[$PAGE_URL$]",
146                 "[$PORTAL_URL$]"
147             },
148             new String[] {
149                 fromAddress,
150                 fromName,
151                 pageURL,
152                 portalURL
153             });
154 
155         for (String emailAddress : validEmailAddresses) {
156             InternetAddress to = new InternetAddress(emailAddress);
157 
158             MailMessage message = new MailMessage(
159                 from, to, subject, body, true);
160 
161             MailServiceUtil.sendEmail(message);
162         }
163 
164         SessionMessages.add(actionRequest, "invitationSent");
165 
166         String redirect = ParamUtil.getString(actionRequest, "redirect");
167 
168         actionResponse.sendRedirect(redirect);
169     }
170 
171     public ActionForward render(
172             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
173             RenderRequest renderRequest, RenderResponse renderResponse)
174         throws Exception {
175 
176         return mapping.findForward(
177             getForward(renderRequest, "portlet.invitation.view"));
178     }
179 
180 }