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