001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.invitation.action;
016    
017    import com.liferay.mail.service.MailServiceUtil;
018    import com.liferay.portal.kernel.mail.MailMessage;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.PortletPreferencesFactoryUtil;
032    import com.liferay.portlet.invitation.util.InvitationUtil;
033    
034    import java.util.ArrayList;
035    import java.util.HashSet;
036    import java.util.List;
037    import java.util.Set;
038    
039    import javax.mail.internet.InternetAddress;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.PortletPreferences;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Charles May
054     */
055    public class ViewAction extends PortletAction {
056    
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    List<String> validEmailAddresses = new ArrayList<String>();
063                    Set<String> invalidEmailAddresses = new HashSet<String>();
064    
065                    int emailMessageMaxRecipients =
066                            InvitationUtil.getEmailMessageMaxRecipients();
067    
068                    for (int i = 0; i < emailMessageMaxRecipients; i++) {
069                            String emailAddress = ParamUtil.getString(
070                                    actionRequest, "emailAddress" + i);
071    
072                            if (Validator.isEmailAddress(emailAddress)) {
073                                    if (!validEmailAddresses.contains(emailAddress)) {
074                                            validEmailAddresses.add(emailAddress);
075                                    }
076                            }
077                            else if (Validator.isNotNull(emailAddress)) {
078                                    invalidEmailAddresses.add("emailAddress" + i);
079                            }
080                    }
081    
082                    if (validEmailAddresses.isEmpty() && invalidEmailAddresses.isEmpty()) {
083                            invalidEmailAddresses.add("emailAddress0");
084                    }
085    
086                    if (!invalidEmailAddresses.isEmpty()) {
087                            SessionErrors.add(
088                                    actionRequest, "emailAddresses", invalidEmailAddresses);
089    
090                            return;
091                    }
092    
093                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
094                            WebKeys.THEME_DISPLAY);
095    
096                    User user = themeDisplay.getUser();
097    
098                    String fromAddress = user.getEmailAddress();
099                    String fromName = user.getFullName();
100    
101                    InternetAddress from = new InternetAddress(fromAddress, fromName);
102    
103                    Layout layout = themeDisplay.getLayout();
104    
105                    String portalURL = PortalUtil.getPortalURL(actionRequest);
106    
107                    String layoutFullURL = PortalUtil.getLayoutFullURL(
108                            layout, themeDisplay);
109    
110                    PortletPreferences preferences =
111                            PortletPreferencesFactoryUtil.getPortletSetup(
112                                    actionRequest, PortletKeys.INVITATION);
113    
114                    String subject = InvitationUtil.getEmailMessageSubject(preferences);
115                    String body = InvitationUtil.getEmailMessageBody(preferences);
116    
117                    subject = StringUtil.replace(
118                            subject,
119                            new String[] {
120                                    "[$FROM_ADDRESS$]",
121                                    "[$FROM_NAME$]",
122                                    "[$PAGE_URL$]",
123                                    "[$PORTAL_URL$]"
124                            },
125                            new String[] {
126                                    fromAddress,
127                                    fromName,
128                                    layoutFullURL,
129                                    portalURL
130                            });
131    
132                    body = StringUtil.replace(
133                            body,
134                            new String[] {
135                                    "[$FROM_ADDRESS$]",
136                                    "[$FROM_NAME$]",
137                                    "[$PAGE_URL$]",
138                                    "[$PORTAL_URL$]"
139                            },
140                            new String[] {
141                                    fromAddress,
142                                    fromName,
143                                    layoutFullURL,
144                                    portalURL
145                            });
146    
147                    for (String emailAddress : validEmailAddresses) {
148                            InternetAddress to = new InternetAddress(emailAddress);
149    
150                            MailMessage message = new MailMessage(
151                                    from, to, subject, body, true);
152    
153                            MailServiceUtil.sendEmail(message);
154                    }
155    
156                    SessionMessages.add(actionRequest, "invitationSent");
157    
158                    String redirect = ParamUtil.getString(actionRequest, "redirect");
159    
160                    actionResponse.sendRedirect(redirect);
161            }
162    
163            public ActionForward render(
164                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
165                            RenderRequest renderRequest, RenderResponse renderResponse)
166                    throws Exception {
167    
168                    return mapping.findForward(
169                            getForward(renderRequest, "portlet.invitation.view"));
170            }
171    
172    }