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.email;
023
024import static org.jboss.seam.ScopeType.STATELESS;
025
026import java.util.ArrayList;
027import java.util.List;
028
029import javax.faces.context.FacesContext;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
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.faces.Renderer;
038import org.jboss.seam.international.StatusMessage;
039import org.nuxeo.ecm.core.api.CoreSession;
040import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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            fromEmail = currentUser.getEmail();
099            List<NuxeoPrincipal> listEmails = new ArrayList<NuxeoPrincipal>();
100            for (String user : principalListManager.getSelectedUsers()) {
101                NuxeoPrincipal principal = userManager.getPrincipal(user);
102                listEmails.add(principal);
103            }
104            toEmail = listEmails;
105            currentDocumentFullUrl = DocumentModelFunctions.documentUrl(navigationContext.getCurrentDocument());
106            log.debug("URL : " + DocumentModelFunctions.documentUrl(navigationContext.getCurrentDocument()));
107
108            try {
109                log.debug("Subject : " + mailSubject);
110                log.debug("Content : " + mailContent);
111                renderer.render("/mail_template.xhtml");
112                facesMessages.add(StatusMessage.Severity.INFO,
113                        resourcesAccessor.getMessages().get("label.email.send.ok"));
114            } catch (RuntimeException e) { // stupid Seam FaceletsRenderer throws RuntimeException
115                facesMessages.add(StatusMessage.Severity.ERROR,
116                        resourcesAccessor.getMessages().get("label.email.send.failed"));
117                log.error("Email sending failed:" + e.getMessage());
118            }
119        }
120    }
121
122    @Override
123    public String getMailContent() {
124        return mailContent;
125    }
126
127    @Override
128    public void setMailContent(String mailContent) {
129        this.mailContent = mailContent;
130    }
131
132    @Override
133    public String getMailSubject() {
134        return mailSubject;
135    }
136
137    @Override
138    public void setMailSubject(String mailSubject) {
139        this.mailSubject = mailSubject;
140    }
141
142    public PrincipalListManager getPrincipalListManager() {
143        return principalListManager;
144    }
145
146    public void setPrincipalListManager(PrincipalListManager principalListManager) {
147        this.principalListManager = principalListManager;
148    }
149
150    public String getFromEmail() {
151        return fromEmail;
152    }
153
154    public void setFromEmail(String fromEmail) {
155        this.fromEmail = fromEmail;
156    }
157
158    public List<NuxeoPrincipal> getToEmail() {
159        return toEmail;
160    }
161
162    public void setToEmail(List<NuxeoPrincipal> toEmail) {
163        this.toEmail = toEmail;
164    }
165
166    public String getCurrentDocumentFullUrl() {
167        return currentDocumentFullUrl;
168    }
169
170    public void setCurrentDocumentFullUrl(String currentDocumentFullUrl) {
171        this.currentDocumentFullUrl = currentDocumentFullUrl;
172    }
173
174}