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.email;
021
022import static org.jboss.seam.ScopeType.STATELESS;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import javax.faces.context.FacesContext;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
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.faces.Renderer;
036import org.jboss.seam.international.StatusMessage;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DataModel;
039import org.nuxeo.ecm.core.api.NuxeoPrincipal;
040import org.nuxeo.ecm.core.api.PropertyException;
041import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
042import org.nuxeo.ecm.platform.usermanager.UserManager;
043import org.nuxeo.ecm.webapp.base.InputController;
044import org.nuxeo.ecm.webapp.security.PrincipalListManager;
045
046/**
047 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a>
048 */
049@Name("emailSenderAction")
050@Scope(STATELESS)
051public class EmailSenderActionsBean extends InputController implements EmailSenderActions {
052
053    private static final Log log = LogFactory.getLog(EmailSenderActionsBean.class);
054
055    @In(create = true)
056    UserManager userManager;
057
058    @In(create = true, required = false)
059    CoreSession documentManager;
060
061    @In(create = true)
062    private Renderer renderer;
063
064    @In(required = false)
065    @Out(required = false)
066    private String mailSubject;
067
068    @In(required = false)
069    @Out(required = false)
070    private String mailContent;
071
072    @In(required = false)
073    @Out(required = false)
074    private String currentDocumentFullUrl;
075
076    @In(create = true)
077    @Out
078    private PrincipalListManager principalListManager;
079
080    @Out(required = false)
081    private String fromEmail;
082
083    @Out(required = false)
084    private List<NuxeoPrincipal> toEmail;
085
086    @Override
087    public void 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;
092        }
093        if (principalListManager.getSelectedUserListEmpty()) {
094            facesMessages.add(StatusMessage.Severity.ERROR,
095                    resourcesAccessor.getMessages().get("label.email.nousers.selected"));
096        } else {
097            NuxeoPrincipal currentUser = (NuxeoPrincipal) FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
098            // XXX hack, principals have only one model
099            DataModel dm = currentUser.getModel().getDataModels().values().iterator().next();
100            try {
101                fromEmail = (String) dm.getData(userManager.getUserEmailField());
102            } catch (PropertyException e1) {
103                fromEmail = null;
104            }
105            List<NuxeoPrincipal> listEmails = new ArrayList<NuxeoPrincipal>();
106            for (String user : principalListManager.getSelectedUsers()) {
107                NuxeoPrincipal principal = userManager.getPrincipal(user);
108                listEmails.add(principal);
109            }
110            toEmail = listEmails;
111            currentDocumentFullUrl = DocumentModelFunctions.documentUrl(navigationContext.getCurrentDocument());
112            log.debug("URL : " + DocumentModelFunctions.documentUrl(navigationContext.getCurrentDocument()));
113
114            try {
115                log.debug("Subject : " + mailSubject);
116                log.debug("Content : " + mailContent);
117                renderer.render("/mail_template.xhtml");
118                facesMessages.add(StatusMessage.Severity.INFO,
119                        resourcesAccessor.getMessages().get("label.email.send.ok"));
120            } catch (RuntimeException e) { // stupid Seam FaceletsRenderer throws RuntimeException
121                facesMessages.add(StatusMessage.Severity.ERROR,
122                        resourcesAccessor.getMessages().get("label.email.send.failed"));
123                log.error("Email sending failed:" + e.getMessage());
124            }
125        }
126    }
127
128    @Override
129    public String getMailContent() {
130        return mailContent;
131    }
132
133    @Override
134    public void setMailContent(String mailContent) {
135        this.mailContent = mailContent;
136    }
137
138    @Override
139    public String getMailSubject() {
140        return mailSubject;
141    }
142
143    @Override
144    public void setMailSubject(String mailSubject) {
145        this.mailSubject = mailSubject;
146    }
147
148    public PrincipalListManager getPrincipalListManager() {
149        return principalListManager;
150    }
151
152    public void setPrincipalListManager(PrincipalListManager principalListManager) {
153        this.principalListManager = principalListManager;
154    }
155
156    public String getFromEmail() {
157        return fromEmail;
158    }
159
160    public void setFromEmail(String fromEmail) {
161        this.fromEmail = fromEmail;
162    }
163
164    public List<NuxeoPrincipal> getToEmail() {
165        return toEmail;
166    }
167
168    public void setToEmail(List<NuxeoPrincipal> toEmail) {
169        this.toEmail = toEmail;
170    }
171
172    public String getCurrentDocumentFullUrl() {
173        return currentDocumentFullUrl;
174    }
175
176    public void setCurrentDocumentFullUrl(String currentDocumentFullUrl) {
177        this.currentDocumentFullUrl = currentDocumentFullUrl;
178    }
179
180}