1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File WikiEmailNotificationRenderer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
86% of files have more coverage

Code metrics

6
17
5
1
129
77
9
0.53
3.4
5
1.8

Classes

Class Line # Actions
WikiEmailNotificationRenderer 46 17 0% 9 28
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.notifications.notifiers.internal.email;
21   
22    import java.lang.reflect.Type;
23   
24    import org.xwiki.component.manager.ComponentLookupException;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.notifications.CompositeEvent;
28    import org.xwiki.notifications.NotificationException;
29    import org.xwiki.notifications.notifiers.email.NotificationEmailRenderer;
30    import org.xwiki.notifications.notifiers.internal.AbstractWikiNotificationRenderer;
31    import org.xwiki.rendering.syntax.Syntax;
32    import org.xwiki.script.ScriptContextManager;
33    import org.xwiki.template.Template;
34    import org.xwiki.template.TemplateManager;
35   
36    import com.xpn.xwiki.objects.BaseObject;
37   
38    /**
39    * This class is meant to be instanciated and then registered to the Component Manager by the
40    * {@link WikiEmailNotificationRendererComponentBuilder} component every time a document containing a
41    * NotificationEmailRendererClass is added, updated or deleted.
42    *
43    * @version $Id: 1b381b3303a827d55115f21328b56d5403c8435b $
44    * @since 9.11.1
45    */
 
46    public class WikiEmailNotificationRenderer extends AbstractWikiNotificationRenderer
47    implements NotificationEmailRenderer
48    {
49    private EmailTemplateRenderer emailTemplateRenderer;
50   
51    private Template htmlTemplate;
52   
53    private Template plainTextTemplate;
54   
55    private Template emailSubjectTemplate;
56   
57    /**
58    * Constructs a new {@link WikiEmailNotificationRenderer}.
59    *
60    * @param authorReference the author reference of the document
61    * @param templateManager the {@link TemplateManager} to use
62    * @param scriptContextManager the {@link ScriptContextManager} to use
63    * @param componentManager the {@link ComponentManager} to use
64    * @param baseObject the XObject which has the required properties to instantiate the component
65    * @throws NotificationException if the properties of the given BaseObject could not be loaded
66    */
 
67  0 toggle public WikiEmailNotificationRenderer(DocumentReference authorReference, TemplateManager templateManager,
68    ScriptContextManager scriptContextManager, ComponentManager componentManager, BaseObject baseObject)
69    throws NotificationException
70    {
71  0 super(authorReference, templateManager, scriptContextManager, componentManager, baseObject);
72  0 try {
73  0 emailTemplateRenderer = componentManager.getInstance(EmailTemplateRenderer.class);
74    } catch (ComponentLookupException e) {
75  0 throw new NotificationException("Failed to create a new instance of WikiEmailNotificationRenderer.", e);
76    }
77  0 this.htmlTemplate = extractTemplate(baseObject,
78    WikiEmailNotificationRendererDocumentInitializer.HTML_TEMPLATE);
79  0 this.plainTextTemplate = extractTemplate(baseObject,
80    WikiEmailNotificationRendererDocumentInitializer.PLAIN_TEXT_TEMPLATE);
81  0 this.emailSubjectTemplate = extractTemplate(baseObject,
82    WikiEmailNotificationRendererDocumentInitializer.EMAIL_SUBJECT_TEMPLATE);
83    }
84   
 
85  0 toggle @Override
86    public String renderHTML(CompositeEvent compositeEvent, String userId) throws NotificationException
87    {
88  0 if (this.htmlTemplate != null) {
89  0 return emailTemplateRenderer.renderHTML(
90    emailTemplateRenderer.executeTemplate(compositeEvent, userId, this.htmlTemplate, Syntax.XHTML_1_0)
91    );
92    }
93    // Fallback to the default renderer
94  0 return null;
95    }
96   
 
97  0 toggle @Override
98    public String renderPlainText(CompositeEvent compositeEvent, String userId) throws NotificationException
99    {
100  0 if (this.plainTextTemplate != null) {
101  0 return emailTemplateRenderer.renderPlainText(
102    emailTemplateRenderer.executeTemplate(compositeEvent, userId, this.plainTextTemplate,
103    Syntax.PLAIN_1_0)
104    );
105    }
106    // Fallback to the default renderer
107  0 return null;
108    }
109   
 
110  0 toggle @Override
111    public String generateEmailSubject(CompositeEvent compositeEvent, String userId)
112    throws NotificationException
113    {
114  0 if (this.emailSubjectTemplate != null) {
115  0 return emailTemplateRenderer.renderPlainText(
116    emailTemplateRenderer.executeTemplate(compositeEvent, userId, this.emailSubjectTemplate,
117    Syntax.PLAIN_1_0)
118    );
119    }
120    // Fallback to the default renderer
121  0 return null;
122    }
123   
 
124  0 toggle @Override
125    public Type getRoleType()
126    {
127  0 return NotificationEmailRenderer.class;
128    }
129    }