View Javadoc

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