001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.notification.email;
021
022import static org.jboss.seam.ScopeType.EVENT;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import javax.faces.context.FacesContext;
030
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Out;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.international.StatusMessage;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
039import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
040import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
041import org.nuxeo.ecm.core.event.Event;
042import org.nuxeo.ecm.core.event.EventProducer;
043import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
044import org.nuxeo.ecm.platform.ec.notification.NotificationConstants;
045import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
046import org.nuxeo.ecm.platform.usermanager.UserManager;
047import org.nuxeo.ecm.webapp.base.InputController;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * @author <a href="mailto:npaslaru@nuxeo.com">Narcis Paslaru</a>
052 */
053
054@Name("emailNotifSenderAction")
055@Scope(EVENT)
056public class EmailNotificationSenderActionsBean extends InputController implements EmailNotificationSenderActions,
057        Serializable {
058
059    private static final long serialVersionUID = 2125646683248052737L;
060
061    @In(create = true)
062    transient UserManager userManager;
063
064    @In(create = true, required = false)
065    transient CoreSession documentManager;
066
067    @In(required = false)
068    @Out(required = false)
069    private String mailSubject;
070
071    @In(required = false)
072    @Out(required = false)
073    private String mailContent;
074
075    @In(required = false)
076    @Out(required = false)
077    private String currentDocumentFullUrl;
078
079    @Out(required = false)
080    private String fromEmail;
081
082    @Out(required = false)
083    private List<NuxeoPrincipal> toEmail;
084
085    private List<String> recipients;
086
087    public String send() {
088        if (mailSubject == null || mailSubject.trim().length() == 0) {
089            facesMessages.add(StatusMessage.Severity.ERROR,
090                    resourcesAccessor.getMessages().get("label.email.subject.empty"));
091            return null;
092        }
093        /*
094         * if (mailContent == null || mailContent.trim().length() == 0){ facesMessages.add(FacesMessage.SEVERITY_ERROR,
095         * resourcesAccessor .getMessages().get("label.email.content.empty")); return; }
096         */
097        if (recipients == null || recipients.isEmpty()) {
098            facesMessages.add(StatusMessage.Severity.ERROR,
099                    resourcesAccessor.getMessages().get("label.email.nousers.selected"));
100            return null;
101        }
102        for (String user : recipients) {
103            sendNotificationEvent(user, mailSubject, mailContent);
104        }
105        facesMessages.add(StatusMessage.Severity.INFO, resourcesAccessor.getMessages().get("label.email.send.ok"));
106
107        // redirect to currentDocument default view
108        DocumentModel cDoc = navigationContext.getCurrentDocument();
109        if (cDoc == null) {
110            return null;
111        } else {
112            TypeInfo typeInfo = cDoc.getAdapter(TypeInfo.class);
113            if (typeInfo != null) {
114                return typeInfo.getDefaultView();
115            } else {
116                return null;
117            }
118        }
119    }
120
121    /**
122     * Sends an event that triggers a notification that sends emails to all selected entities.
123     *
124     * @param user
125     * @param theMailSubject
126     * @param theMailContent
127     */
128    private void sendNotificationEvent(String recipient, String theMailSubject, String theMailContent)
129            {
130
131        Map<String, Serializable> options = new HashMap<String, Serializable>();
132
133        // options for confirmation email
134        options.put(NotificationConstants.RECIPIENTS_KEY, new String[] { recipient });
135        options.put("mailSubject", theMailSubject);
136        options.put("mailContent", theMailContent);
137        options.put("category", DocumentEventCategories.EVENT_CLIENT_NOTIF_CATEGORY);
138
139        NuxeoPrincipal currentUser = (NuxeoPrincipal) FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
140
141        DocumentEventContext ctx = new DocumentEventContext(documentManager, currentUser,
142                navigationContext.getCurrentDocument());
143        ctx.setProperties(options);
144        Event event = ctx.newEvent(DocumentEventTypes.EMAIL_DOCUMENT_SEND);
145
146        EventProducer evtProducer = Framework.getService(EventProducer.class);
147        evtProducer.fireEvent(event);
148
149    }
150
151    /**
152     * @return the mailContent.
153     */
154    public String getMailContent() {
155        return mailContent;
156    }
157
158    /**
159     * @param mailContent the mailContent to set.
160     */
161    public void setMailContent(String mailContent) {
162        this.mailContent = mailContent;
163    }
164
165    /**
166     * @return the mailSubject.
167     */
168    public String getMailSubject() {
169        return mailSubject;
170    }
171
172    /**
173     * @param mailSubject the mailSubject to set.
174     */
175    public void setMailSubject(String mailSubject) {
176        this.mailSubject = mailSubject;
177    }
178
179    /**
180     * @return the fromEmail.
181     */
182    public String getFromEmail() {
183        return fromEmail;
184    }
185
186    /**
187     * @param fromEmail the fromEmail to set.
188     */
189    public void setFromEmail(String fromEmail) {
190        this.fromEmail = fromEmail;
191    }
192
193    /**
194     * @return the toEmail.
195     */
196    public List<NuxeoPrincipal> getToEmail() {
197        return toEmail;
198    }
199
200    /**
201     * @param toEmail the toEmail to set.
202     */
203    public void setToEmail(List<NuxeoPrincipal> toEmail) {
204        this.toEmail = toEmail;
205    }
206
207    public String getCurrentDocumentFullUrl() {
208        return currentDocumentFullUrl;
209    }
210
211    public void setCurrentDocumentFullUrl(String currentDocumentFullUrl) {
212        this.currentDocumentFullUrl = currentDocumentFullUrl;
213    }
214
215    /**
216     * @since 7.1
217     */
218    public List<String> getRecipients() {
219        return recipients;
220    }
221
222    /**
223     * @since 7.1
224     */
225    public void setRecipients(List<String> recipients) {
226        this.recipients = recipients;
227    }
228
229}