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