01: package ru.emdev.EmForge.email.velocity;
02:
03: import java.util.Map;
04:
05: import org.apache.velocity.app.VelocityEngine;
06: import org.springframework.beans.factory.InitializingBean;
07:
08: import ru.emdev.EmForge.EmForgeContext;
09: import ru.emdev.EmForge.email.Email;
10: import ru.emdev.EmForge.email.EmailFactory;
11:
12: /** Implementation of Email Factory
13: *
14: * This implementation used Velocity JbpmServiceImpl Engine for Email Generation
15: * @author akakunin
16: *
17: */
18: public class VelocityEmailFactoryImpl implements EmailFactory,
19: InitializingBean {
20: private VelocityEngine velocityEngine;
21:
22: private Map<String, String> emailsTemplatingMap;
23:
24: /** Application bean is used for getting information about application path */
25: EmForgeContext appContext;
26:
27: public void setVelocityEngine(VelocityEngine velocityEngine) {
28: this .velocityEngine = velocityEngine;
29: }
30:
31: public void setAppContext(EmForgeContext appContext) {
32: this .appContext = appContext;
33: }
34:
35: /** Creates email class by specified template name
36: *
37: * @todo Currently creation of concrete email class from specified template name
38: * is hardcoded here. It should be moved into configuration
39: */
40: public Email createEmail(String emailTemplateName) {
41: BaseVelocityEmailImpl email = null;
42:
43: try {
44: String className = getEmailsTemplatingMap().get(
45: emailTemplateName);
46: email = (BaseVelocityEmailImpl) Class.forName(className)
47: .newInstance();
48: } catch (Exception e) {
49: // there is nothing to do, map is ok
50: // I have check it in afterPropertiesSet method
51: }
52:
53: if (email != null) {
54: //initialize it
55: email.setVelocityEngine(velocityEngine);
56: email.setTemplateName(emailTemplateName);
57: email.setApplicationPath(appContext.getApplicationPath());
58: }
59:
60: return email;
61: }
62:
63: /**
64: * @return the emailsTemplatingMap
65: */
66: private Map<String, String> getEmailsTemplatingMap() {
67: return emailsTemplatingMap;
68: }
69:
70: /**
71: * @param emailsTemplatingMap the emailsTemplatingMap to set
72: */
73: public void setEmailsTemplatingMap(
74: Map<String, String> emailsTemplatingMap) {
75: this .emailsTemplatingMap = emailsTemplatingMap;
76: }
77:
78: public void afterPropertiesSet() throws Exception {
79:
80: // Check map for values
81: // here we only check possibility to create each object
82: // of class with name in map
83: try {
84: for (String key : getEmailsTemplatingMap().keySet()) {
85: Class.forName(getEmailsTemplatingMap().get(key))
86: .newInstance();
87: }
88: } catch (Exception e) {
89: throw new IllegalArgumentException(
90: "VelocityEmailFactoryImpl.afterPropertiesSet: emailsTemplatingMap has wrong value",
91: e);
92: }
93: }
94:
95: }
|