-
Notifications
You must be signed in to change notification settings - Fork 2
/
EmailTemplatesGrailsPlugin.groovy
114 lines (93 loc) · 4.18 KB
/
EmailTemplatesGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import org.grails.plugin.emailTemplates.EmailTemplateArtefactHandler
import org.springframework.beans.factory.NoSuchBeanDefinitionException
class EmailTemplatesGrailsPlugin {
def version = "0.22"
def grailsVersion = "2.0 > *"
// the other plugins this plugin depends on
def loadAfter = ['mail', 'platformCore', 'database-migration']
def pluginExcludes = [
"grails-app/emailTemplates/org/grails/plugin/emailTemplates/ResetPasswordEmailTemplate.groovy",
"grails-app/emailTemplates/org/grails/plugin/emailTemplates/AttachmentEmailTemplate.groovy",
"grails-app/emailTemplates/org/grails/plugin/emailTemplates/ResetPasswordNoListenerDefinedEmailTemplate.groovy",
"grails-app/domain/org/grails/plugin/emailTemplates/test/*",
"grails-app/conf/TestDataConfig.groovy"
]
def title = "Email Templates"
def author = "Eamonn O'Connell"
def authorEmail = "@34m0"
def description = '''
Quickly and easy email templates
'''
def documentation = "http://grails.org/plugin/email-templates"
def license = "APACHE"
def developers = [ [ name: "Eamonn O'Connell", email: "[email protected]" ] ]
def issueManagement = [ system: "GIT", url: "https://github.com/halfbaked/grails-email-templates/issues" ]
def scm = [ url: "https://github.com/halfbaked/grails-email-templates" ]
def watchedResources = "field:./grails-app/emailTemplates/**/*EmailTemplate.groovy"
def artefacts = [ EmailTemplateArtefactHandler ]
def doWithSpring = { ctx ->
def disabledEmailTemplates = application.config.grails?.plugin?.emailTemplates?.disabledEmailTemplates
application.emailTemplateClasses.each { emailTemplateClass ->
if (emailTemplateClass.isAbstract()) return
def beanName = emailTemplateClass.fullName
def shortBeanName = generateShortBeanNameFromClass(emailTemplateClass)
if (!disabledEmailTemplates || !disabledEmailTemplates.any{ it =~ beanName || it =~ shortBeanName}) {
"$beanName"(emailTemplateClass.clazz) { bean ->
bean.autowire = true
mustache = new com.github.mustachejava.DefaultMustacheFactory()
}
springConfig.addAlias shortBeanName, beanName
}
}
}
def doWithApplicationContext = { appCtx ->
application.emailTemplateClasses.each { emailTemplateClass ->
try {
log.trace "Processing $emailTemplateClass"
if (emailTemplateClass.isAbstract()) return
def beanName = generateBeanNameFromClass(emailTemplateClass)
def emailTemplate = appCtx.getBean(beanName)
emailTemplate.persistEmailTemplateDataIfDoesNotExist()
def listener = emailTemplate.hasProperty("listener") ? emailTemplate.listener : null
if (listener && listener.topic) {
def sendMethod = emailTemplate.class.methods.find { it.name =~ /^sendWithDataMessage$/ }
appCtx.grailsEventsRegistry.on (listener.namespace, listener.topic, emailTemplate, sendMethod)
}
} catch (NoSuchBeanDefinitionException e) {
log.warn("No bean registered for the emailTemplate artifact. It might be disabled", e)
}
}
}
def doWithDynamicMethods = { ctx ->
try {
for (domainClass in application.domainClasses) {
domainClass.metaClass.static.dataKeys = {
delegate.newInstance().asDataMap().keySet()
}
}
} catch (Exception e){
println "Error $e"
}
}
def onChange = { event ->
if(application.isArtefactOfType(EmailTemplateArtefactHandler.TYPE, event.source)) {
def oldClass = application.getEmailTemplateClass(event.source.name)
application.addArtefact(EmailTemplateArtefactHandler.TYPE, event.source)
application.emailTemplateClasses.each {
if (it.clazz != event.source && oldClass.clazz.isAssignableFrom(it.clazz)) {
def newClass = application.classLoader.reloadClass(it.clazz.name)
application.addArtefact(EmailTemplateArtefactHandler.TYPE, newClass)
}
}
}
}
def doWithConfigOptions = {
'locale.default'(type: String, defaultValue: 'en')
}
private generateShortBeanNameFromClass(def clazz) {
clazz.shortName[0].toLowerCase() + clazz.shortName[1..-1]
}
private generateBeanNameFromClass(def clazz) {
clazz.fullName
}
}