001: package ru.emdev.EmForge.email.velocity;
002:
003: import java.util.Map;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.apache.velocity.app.VelocityEngine;
008: import org.apache.velocity.exception.VelocityException;
009: import org.springframework.ui.velocity.VelocityEngineUtils;
010:
011: import ru.emdev.EmForge.email.Email;
012:
013: /** base implementation of emails, based on the Velocity Engine
014: *
015: * @author akakunin
016: *
017: */
018: public abstract class BaseVelocityEmailImpl implements Email {
019: private final Log log = LogFactory.getLog(getClass());
020:
021: private VelocityEngine velocityEngine;
022: private String templateName;
023: private String applicationPath;
024:
025: /** Email part */
026: private String toName;
027: private String fromName;
028: private String subject;
029: private int priority;
030: private String carbonCopy;
031:
032: public void setVelocityEngine(VelocityEngine velocityEngine) {
033: this .velocityEngine = velocityEngine;
034: }
035:
036: public VelocityEngine getVelocityEngine() {
037: return velocityEngine;
038: }
039:
040: public String getTemplateName() {
041: return templateName;
042: }
043:
044: public void setTemplateName(String templateName) {
045: this .templateName = templateName;
046: }
047:
048: public String getApplicationPath() {
049: return applicationPath;
050: }
051:
052: public void setApplicationPath(String applicationPath) {
053: this .applicationPath = applicationPath;
054: }
055:
056: /** Returns Model for text generation */
057: abstract public Map<String, Object> getModel();
058:
059: /* Email Part Implementation */
060: public String getCarbonCopy() {
061: return carbonCopy;
062: }
063:
064: public void setCarbonCopy(String carbonCopy) {
065: this .carbonCopy = carbonCopy;
066: }
067:
068: public String getFromName() {
069: return fromName;
070: }
071:
072: public void setFromName(String fromName) {
073: this .fromName = fromName;
074: }
075:
076: public int getPriority() {
077: return priority;
078: }
079:
080: public void setPriority(int priority) {
081: this .priority = priority;
082: }
083:
084: public String getSubject() {
085: return subject;
086: }
087:
088: public void setSubject(String subject) {
089: this .subject = subject;
090: }
091:
092: public String getToName() {
093: return toName;
094: }
095:
096: public void setToName(String toName) {
097: this .toName = toName;
098: }
099:
100: public String getContent() {
101: String text = null;
102: try {
103: Map model = getModel();
104:
105: text = VelocityEngineUtils.mergeTemplateIntoString(
106: velocityEngine, templateName + ".text", model);
107: } catch (VelocityException ex) {
108: log.error("Cannot generate message text:", ex);
109: }
110:
111: return text;
112:
113: }
114:
115: /** HTML Messages is not supported yet */
116: public String getHtmlContent() {
117: String text = null;
118: try {
119: Map model = getModel();
120:
121: text = VelocityEngineUtils.mergeTemplateIntoString(
122: velocityEngine, templateName + ".html", model);
123: } catch (VelocityException ex) {
124: // html template may not be defined - just ignore it
125: //log.error("Cannot generate message text:", ex);
126: }
127:
128: return text;
129: }
130:
131: }
|