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.mail.handlers.MgnlMailHandler;
37  import info.magnolia.module.mail.templates.MailAttachment;
38  import info.magnolia.module.mail.templates.MgnlEmail;
39  import info.magnolia.module.mail.templates.impl.SimpleEmail;
40  import info.magnolia.objectfactory.Classes;
41  
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Map;
45  
46  import org.apache.commons.beanutils.BeanUtils;
47  import org.apache.commons.collections.CollectionUtils;
48  import org.apache.commons.lang.StringUtils;
49  
50  
51  /**
52   * This reads the repository to know what kind of email to instantiate.
53   *
54   * @author <a href="mailto:niko@macnica.com">Nicolas Modrzyk</a>
55   */
56  public class MgnlMailFactory {
57      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MgnlMailFactory.class);
58  
59      private Map<String, String> renderers = new HashMap<String, String>();
60  
61  
62      /**
63       * Use getInstance to get the current used instance.
64       */
65      public MgnlMailFactory() {
66  
67      }
68  
69      public MgnlMailHandler getEmailHandler() {
70          return MailModule.getInstance().getHandler();
71      }
72  
73  
74      /**
75       * Creates email with no attachments.
76       */
77      public MgnlEmail getEmail(Map<String, Object> params) {
78          return getEmail(params, null);
79      }
80  
81      /**
82       * Creates email with no attachments.
83       */
84      public MgnlEmail getEmailFromType(Map<String, Object> params, String type) {
85  
86          return getEmailFromType(params, type, null);
87      }
88  
89      public MgnlEmail getEmailFromType(Map<String, Object> params, String type, String contentType, List<MailAttachment> attachments) {
90          Map<String, Object> newParams = new HashMap<String, Object>();
91          newParams.putAll(params);
92          if(!StringUtils.isEmpty(type)) {
93              newParams.put(MailTemplate.MAIL_TYPE, type);
94          }
95          if(!StringUtils.isEmpty(contentType)) {
96              newParams.put(MailTemplate.MAIL_CONTENT_TYPE, contentType);
97          }
98          return getEmail(newParams, attachments);
99      }
100 
101     /**
102      * Creates email with attachments.
103      */
104     public MgnlEmail getEmailFromType(Map<String, Object> params, String type, List<MailAttachment> attachments) {
105         return getEmailFromType(params, type, null, attachments);
106     }
107 
108     /**
109      * Creates email with attachments.
110      */
111     public MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments) {
112         MailTemplate template = new MailTemplate();
113         return getEmail(params, attachments, template);
114     }
115 
116     /**
117      * Creates email using predefined template.
118      */
119     public MgnlEmail getEmailFromTemplate(String id, List<MailAttachment> attachments, Map<String, Object> params) throws Exception {
120         MailTemplate template = getTemplate(id);
121         if (template == null) {
122             log.error("Template {} can't be found", id);
123             return null;
124         }
125         if(!CollectionUtils.isEmpty(attachments)) {
126             // add to attachments also attachments from template configuration
127             attachments.addAll(template.getAttachments());
128         }
129         return getEmail(params, attachments, template);
130     }
131 
132     /**
133      * Creates email using predefined template with no attachments.
134      */
135     public MgnlEmail getEmailFromTemplate(String id, Map<String, Object> params) throws Exception {
136         return getEmailFromTemplate(id, null, params);
137     }
138 
139     protected MgnlEmail getEmail(Map<String, Object> params, List<MailAttachment> attachments, MailTemplate template) {
140         template.setValues(params, attachments);
141 
142         try {
143             return getEmailFromType(template);
144 
145         } catch (Exception e) {
146             log.error("Couln't instantiate email type: " + template.getType(), e);
147             return null;
148         }
149     }
150 
151     protected MgnlEmail getEmailFromType(MailTemplate template) throws Exception {
152         MgnlEmail mail = null;
153         if(StringUtils.isEmpty(template.getType()) || !renderers.containsKey(template.getType().toLowerCase())) {
154             mail = new SimpleEmail(template);
155 
156         } else {
157             String rendererClass = renderers.get(template.getType().toLowerCase());
158             mail = Classes.newInstance(rendererClass, template);
159         }
160 
161         //set all mail parameters
162         if(!StringUtils.isEmpty(template.getFrom())) {
163             mail.setFrom(template.getFrom());
164         }
165         if(!StringUtils.isEmpty(template.getTo())) {
166             mail.setToList(template.getTo());
167         }
168         if(!StringUtils.isEmpty(template.getCc())) {
169             mail.setCcList(template.getCc());
170         }
171         if(!StringUtils.isEmpty(template.getBcc())) {
172             mail.setBccList(template.getBcc());
173         }
174         if(!StringUtils.isEmpty(template.getReplyTo())) {
175             mail.setReplyToList(template.getReplyTo());
176         }
177         if(!StringUtils.isEmpty(template.getSubject())) {
178             mail.setSubject(template.getSubject());
179         }
180 
181         return mail;
182     }
183 
184 
185     protected MailTemplate getTemplate(String templateName) throws Exception {
186         final List<MailTemplate> configuration = MailModule.getInstance().getTemplatesConfiguration();
187         for (MailTemplate mailTemplate : configuration) {
188             if (StringUtils.equals(mailTemplate.getName(), templateName)) {
189                 return (MailTemplate) BeanUtils.cloneBean(mailTemplate);
190             }
191         }
192         return null;
193     }
194 
195 
196     public Map<String, String> getRenderers() {
197         return renderers;
198     }
199 
200     public void setRenderers(Map<String, String> renderers) {
201         this.renderers = renderers;
202     }
203 
204 }