001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.invitation.action;
022:
023: import com.liferay.mail.service.MailServiceUtil;
024: import com.liferay.portal.kernel.mail.MailMessage;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.struts.PortletAction;
031: import com.liferay.portal.theme.ThemeDisplay;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.portal.util.PortletKeys;
034: import com.liferay.portal.util.WebKeys;
035: import com.liferay.portlet.PortletPreferencesFactoryUtil;
036: import com.liferay.portlet.invitation.util.InvitationUtil;
037: import com.liferay.util.CollectionFactory;
038: import com.liferay.util.servlet.SessionErrors;
039: import com.liferay.util.servlet.SessionMessages;
040:
041: import java.util.ArrayList;
042: import java.util.List;
043: import java.util.Set;
044:
045: import javax.mail.internet.InternetAddress;
046:
047: import javax.portlet.ActionRequest;
048: import javax.portlet.ActionResponse;
049: import javax.portlet.PortletConfig;
050: import javax.portlet.PortletPreferences;
051: import javax.portlet.RenderRequest;
052: import javax.portlet.RenderResponse;
053:
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Charles May
062: *
063: */
064: public class ViewAction extends PortletAction {
065:
066: public void processAction(ActionMapping mapping, ActionForm form,
067: PortletConfig config, ActionRequest req, ActionResponse res)
068: throws Exception {
069:
070: List validEmailAddresses = new ArrayList();
071: Set invalidEmailAddresses = CollectionFactory.getHashSet();
072:
073: int emailMessageMaxRecipients = InvitationUtil
074: .getEmailMessageMaxRecipients();
075:
076: for (int i = 0; i < emailMessageMaxRecipients; i++) {
077: String emailAddress = ParamUtil.getString(req,
078: "emailAddress" + i);
079:
080: if (Validator.isEmailAddress(emailAddress)) {
081: validEmailAddresses.add(emailAddress);
082: } else if (Validator.isNotNull(emailAddress)) {
083: invalidEmailAddresses.add("emailAddress" + i);
084: }
085: }
086:
087: if (invalidEmailAddresses.size() > 0) {
088: SessionErrors.add(req, "emailAddresses",
089: invalidEmailAddresses);
090:
091: return;
092: }
093:
094: ThemeDisplay themeDisplay = (ThemeDisplay) req
095: .getAttribute(WebKeys.THEME_DISPLAY);
096:
097: User user = themeDisplay.getUser();
098:
099: String fromAddress = user.getEmailAddress();
100: String fromName = user.getFullName();
101:
102: InternetAddress from = new InternetAddress(fromAddress,
103: fromName);
104:
105: Layout layout = themeDisplay.getLayout();
106:
107: String portalURL = PortalUtil.getPortalURL(req);
108:
109: String pageURL = portalURL
110: + PortalUtil.getLayoutURL(layout, themeDisplay);
111:
112: PortletPreferences prefs = PortletPreferencesFactoryUtil
113: .getPortletSetup(req, PortletKeys.INVITATION, true,
114: true);
115:
116: String subject = InvitationUtil.getEmailMessageSubject(prefs);
117: String body = InvitationUtil.getEmailMessageBody(prefs);
118:
119: subject = StringUtil.replace(subject, new String[] {
120: "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$PAGE_URL$]",
121: "[$PORTAL_URL$]" }, new String[] { fromAddress,
122: fromName, pageURL, portalURL });
123:
124: body = StringUtil.replace(body, new String[] {
125: "[$FROM_ADDRESS$]", "[$FROM_NAME$]", "[$PAGE_URL$]",
126: "[$PORTAL_URL$]" }, new String[] { fromAddress,
127: fromName, pageURL, portalURL });
128:
129: for (int i = 0; i < validEmailAddresses.size(); i++) {
130: String emailAddress = (String) validEmailAddresses.get(i);
131:
132: InternetAddress to = new InternetAddress(emailAddress);
133:
134: MailMessage message = new MailMessage(from, to, subject,
135: body, true);
136:
137: MailServiceUtil.sendEmail(message);
138: }
139:
140: SessionMessages.add(req, "invitationSent");
141:
142: String redirect = ParamUtil.getString(req, "redirect");
143:
144: res.sendRedirect(redirect);
145: }
146:
147: public ActionForward render(ActionMapping mapping, ActionForm form,
148: PortletConfig config, RenderRequest req, RenderResponse res)
149: throws Exception {
150:
151: return mapping.findForward(getForward(req,
152: "portlet.invitation.view"));
153: }
154:
155: }
|