001    /**
002     * Copyright (c) 2000-2013 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.HashSet;
035    import java.util.Set;
036    
037    import javax.mail.internet.InternetAddress;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.PortletPreferences;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Charles May
052     */
053    public class ViewAction extends PortletAction {
054    
055            @Override
056            public void processAction(
057                            ActionMapping actionMapping, ActionForm actionForm,
058                            PortletConfig portletConfig, ActionRequest actionRequest,
059                            ActionResponse actionResponse)
060                    throws Exception {
061    
062                    Set<String> invalidEmailAddresses = new HashSet<String>();
063                    Set<String> validEmailAddresses = 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                                    validEmailAddresses.add(emailAddress);
074                            }
075                            else if (Validator.isNotNull(emailAddress)) {
076                                    invalidEmailAddresses.add("emailAddress" + i);
077                            }
078                    }
079    
080                    if (validEmailAddresses.isEmpty() && invalidEmailAddresses.isEmpty()) {
081                            invalidEmailAddresses.add("emailAddress0");
082                    }
083    
084                    if (!invalidEmailAddresses.isEmpty()) {
085                            SessionErrors.add(
086                                    actionRequest, "emailAddresses", invalidEmailAddresses);
087    
088                            return;
089                    }
090    
091                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
092                            WebKeys.THEME_DISPLAY);
093    
094                    User user = themeDisplay.getUser();
095    
096                    String fromAddress = user.getEmailAddress();
097                    String fromName = user.getFullName();
098    
099                    InternetAddress from = new InternetAddress(fromAddress, fromName);
100    
101                    Layout layout = themeDisplay.getLayout();
102    
103                    String portalURL = PortalUtil.getPortalURL(actionRequest);
104    
105                    String layoutFullURL = PortalUtil.getLayoutFullURL(
106                            layout, themeDisplay);
107    
108                    PortletPreferences preferences =
109                            PortletPreferencesFactoryUtil.getPortletSetup(
110                                    actionRequest, PortletKeys.INVITATION);
111    
112                    String subject = InvitationUtil.getEmailMessageSubject(preferences);
113                    String body = InvitationUtil.getEmailMessageBody(preferences);
114    
115                    subject = StringUtil.replace(
116                            subject,
117                            new String[] {
118                                    "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$PAGE_URL$]",
119                                    "[$PORTAL_URL$]"
120                            },
121                            new String[] {
122                                    fromAddress, fromName, layoutFullURL, portalURL
123                            });
124    
125                    body = StringUtil.replace(
126                            body,
127                            new String[] {
128                                    "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$PAGE_URL$]",
129                                    "[$PORTAL_URL$]"
130                            },
131                            new String[] {
132                                    fromAddress, fromName, layoutFullURL, portalURL
133                            });
134    
135                    for (String emailAddress : validEmailAddresses) {
136                            InternetAddress to = new InternetAddress(emailAddress);
137    
138                            MailMessage message = new MailMessage(
139                                    from, to, subject, body, true);
140    
141                            MailServiceUtil.sendEmail(message);
142                    }
143    
144                    SessionMessages.add(actionRequest, "invitationSent");
145    
146                    String redirect = PortalUtil.escapeRedirect(
147                            ParamUtil.getString(actionRequest, "redirect"));
148    
149                    if (Validator.isNotNull(redirect)) {
150                            actionResponse.sendRedirect(redirect);
151                    }
152            }
153    
154            @Override
155            public ActionForward render(
156                            ActionMapping actionMapping, ActionForm actionForm,
157                            PortletConfig portletConfig, RenderRequest renderRequest,
158                            RenderResponse renderResponse)
159                    throws Exception {
160    
161                    return actionMapping.findForward(
162                            getForward(renderRequest, "portlet.invitation.view"));
163            }
164    
165    }