View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail;
35  
36  import info.magnolia.module.ModuleRegistry;
37  import info.magnolia.module.mail.handlers.MgnlMailHandler;
38  import info.magnolia.module.mail.templates.MailAttachment;
39  import info.magnolia.module.mail.templates.MgnlEmail;
40  import info.magnolia.module.mail.templates.impl.SimpleEmail;
41  import info.magnolia.objectfactory.Classes;
42  import info.magnolia.objectfactory.Components;
43  
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import javax.inject.Inject;
49  
50  import org.apache.commons.beanutils.BeanUtils;
51  import org.apache.commons.collections.CollectionUtils;
52  import org.apache.commons.lang.StringUtils;
53  
54  
55  /**
56   * This reads the repository to know what kind of email to instantiate.
57   *
58   * @author <a href="mailto:niko@macnica.com">Nicolas Modrzyk</a>
59   */
60  public class MgnlMailFactory {
61      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MgnlMailFactory.class);
62  
63      private Map<String, String> renderers = new HashMap<String, String>();
64  
65      private ModuleRegistry moduleRegistry;
66  
67      /**
68       *@deprecated since 5.2.1 Please, use {@link #MgnlMailFactory(ModuleRegistry moduleRegistry)} instead.
69       **/
70      public MgnlMailFactory() {
71          this.moduleRegistry = Components.getComponent(ModuleRegistry.class);
72      }
73  
74      @Inject
75      public MgnlMailFactory(ModuleRegistry moduleRegistry) {
76          this.moduleRegistry = moduleRegistry;
77      }
78  
79      public MgnlMailHandler getEmailHandler() {
80          return moduleRegistry.getModuleInstance(MailModule.class).getHandler();
81      }
82  
83  
84      /**
85       * Creates email with no attachments.
86       */
87      public MgnlEmail getEmail(Map<String, Object> params) {
88          return getEmail(params, null);
89      }
90  
91      /**
92       * Creates email with no attachments.
93       */
94      public MgnlEmail getEmailFromType(Map<String, Object> params, String type) {
95  
96          return getEmailFromType(params, type, null);
97      }
98  
99      public MgnlEmail getEmailFromType(Map<String, Object> params, String type, String contentType, List<MailAttachment> attachments) {
100         Map<String, Object> newParams = new HashMap<String, Object>();
101         newParams.putAll(params);
102         if(!StringUtils.isEmpty(type)) {
103             newParams.put(MailTemplate.MAIL_TYPE, type);
104         }
105         if(!StringUtils.isEmpty(contentType)) {
106             newParams.put(MailTemplate.MAIL_CONTENT_TYPE, contentType);
107         }
108         return getEmail(newParams, attachments);
109     }
110 
111     /**
112      * Creates email with attachments.
113      */
114     public MgnlEmail getEmailFromType(Map<String, Object> params, String type, List<MailAttachment> attachments) {
115         return getEmailFromType(params, type, null, attachments);
116     }
117 
118     /**
119      * Creates email with attachments.
120      */
121     public MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments) {
122         MailTemplate template = new MailTemplate();
123         return getEmail(params, attachments, template);
124     }
125 
126     /**
127      * Creates email using predefined template.
128      */
129     public MgnlEmail getEmailFromTemplate(String id, List<MailAttachment> attachments, Map<String, Object> params) throws Exception {
130         MailTemplate template = getTemplate(id);
131         if (template == null) {
132             log.error("Template {} can't be found", id);
133             return null;
134         }
135         if(!CollectionUtils.isEmpty(attachments)) {
136             // add to attachments also attachments from template configuration
137             attachments.addAll(template.getAttachments());
138         }
139         return getEmail(params, attachments, template);
140     }
141 
142     /**
143      * Creates email using predefined template with no attachments.
144      */
145     public MgnlEmail getEmailFromTemplate(String id, Map<String, Object> params) throws Exception {
146         return getEmailFromTemplate(id, null, params);
147     }
148 
149     protected MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments, MailTemplate template) {
150         template.setValues(params, attachments);
151 
152         try {
153             return getEmailFromType(template);
154 
155         } catch (Exception e) {
156             log.error("Couln't instantiate email type: " + template.getType(), e);
157             return null;
158         }
159     }
160 
161     protected MgnlEmail getEmailFromType(MailTemplate template) throws Exception {
162         MgnlEmail mail = null;
163         if(StringUtils.isEmpty(template.getType()) || !renderers.containsKey(template.getType().toLowerCase())) {
164             mail = new SimpleEmail(template);
165 
166         } else {
167             String rendererClass = renderers.get(template.getType().toLowerCase());
168             mail = Classes.newInstance(rendererClass, template);
169         }
170 
171         //set all mail parameters
172         if(!StringUtils.isEmpty(template.getFrom())) {
173             mail.setFrom(template.getFrom());
174         }
175         if(!StringUtils.isEmpty(template.getTo())) {
176             mail.setToList(template.getTo());
177         }
178         if(!StringUtils.isEmpty(template.getCc())) {
179             mail.setCcList(template.getCc());
180         }
181         if(!StringUtils.isEmpty(template.getBcc())) {
182             mail.setBccList(template.getBcc());
183         }
184         if(!StringUtils.isEmpty(template.getReplyTo())) {
185             mail.setReplyToList(template.getReplyTo());
186         }
187         if(!StringUtils.isEmpty(template.getSubject())) {
188             mail.setSubject(template.getSubject());
189         }
190 
191         return mail;
192     }
193 
194 
195     protected MailTemplate getTemplate(String templateName) throws Exception {
196         final List<MailTemplate> configuration = Components.getComponent(ModuleRegistry.class).getModuleInstance(MailModule.class).getTemplatesConfiguration();
197         for (MailTemplate mailTemplate : configuration) {
198             if (StringUtils.equals(mailTemplate.getName(), templateName)) {
199                 return (MailTemplate) BeanUtils.cloneBean(mailTemplate);
200             }
201         }
202         return null;
203     }
204 
205 
206     public Map<String, String> getRenderers() {
207         return renderers;
208     }
209 
210     public void setRenderers(Map<String, String> renderers) {
211         this.renderers = renderers;
212     }
213 
214 }