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

File AbstractWikiNotificationRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

2
20
8
1
145
83
11
0.55
2.5
8
1.38

Classes

Class Line # Actions
AbstractWikiNotificationRenderer 43 20 0% 11 4
0.866666786.7%
 

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;
21   
22    import org.xwiki.component.manager.ComponentManager;
23    import org.xwiki.component.wiki.WikiComponent;
24    import org.xwiki.component.wiki.WikiComponentScope;
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.model.reference.EntityReference;
27    import org.xwiki.notifications.NotificationException;
28    import org.xwiki.script.ScriptContextManager;
29    import org.xwiki.template.Template;
30    import org.xwiki.template.TemplateManager;
31    import org.xwiki.text.StringUtils;
32   
33    import com.xpn.xwiki.objects.BaseObject;
34    import com.xpn.xwiki.objects.BaseObjectReference;
35   
36    /**
37    * Helper to build wiki components that render templates related to notifications.
38    *
39    * @version $Id: 445e9fda80aca2ac46525feb2413b60ae15a88e7 $
40    * @since 10.0
41    * @since 9.11.1
42    */
 
43    public abstract class AbstractWikiNotificationRenderer implements WikiComponent
44    {
45    protected static final String EVENT_BINDING_NAME = "event";
46   
47    protected TemplateManager templateManager;
48   
49    protected ScriptContextManager scriptContextManager;
50   
51    protected ComponentManager componentManager;
52   
53    protected BaseObjectReference objectReference;
54   
55    protected DocumentReference authorReference;
56   
57    protected String eventType;
58   
59    /**
60    * Constructs a new {@link AbstractWikiNotificationRenderer}.
61    *
62    * @param authorReference the author reference of the document
63    * @param templateManager the {@link TemplateManager} to use
64    * @param scriptContextManager the {@link ScriptContextManager} to use
65    * @param componentManager the {@link ComponentManager} to use
66    * @param baseObject the XObject which has the required properties to instantiate the component
67    * @throws NotificationException if the properties of the given BaseObject could not be loaded
68    */
 
69  2 toggle public AbstractWikiNotificationRenderer(DocumentReference authorReference, TemplateManager templateManager,
70    ScriptContextManager scriptContextManager, ComponentManager componentManager, BaseObject baseObject)
71    throws NotificationException
72    {
73  2 this.objectReference = baseObject.getReference();
74  2 this.authorReference = authorReference;
75  2 this.templateManager = templateManager;
76  2 this.scriptContextManager = scriptContextManager;
77  2 this.componentManager = componentManager;
78   
79  2 this.eventType = this.extractProperty(baseObject, WikiNotificationDisplayerDocumentInitializer.EVENT_TYPE);
80    }
81   
 
82  2 toggle protected Template extractTemplate(BaseObject baseObject, String propertyName) throws NotificationException
83    {
84  2 try {
85  2 String xObjectTemplate = this.extractProperty(baseObject, propertyName);
86  2 if (StringUtils.isNotBlank(xObjectTemplate)) {
87  1 return templateManager.createStringTemplate(xObjectTemplate, this.getAuthorReference());
88    }
89    } catch (Exception e) {
90  0 throw new NotificationException(
91    String.format("Unable to render the template provided in the base object [%s]",
92    baseObject), e);
93    }
94   
95  1 return null;
96    }
97   
98    /**
99    * Extract the the given property value from the given XObject.
100    *
101    * @param baseObject the XObject that should contain the parameters
102    * @param propertyName the value of the property that should be extracted
103    * @throws NotificationException if an error occurred while extracting the parameter from the base object
104    */
 
105  4 toggle protected String extractProperty(BaseObject baseObject, String propertyName) throws NotificationException
106    {
107  4 try {
108  4 return baseObject.getStringValue(propertyName);
109    } catch (Exception e) {
110  0 throw new NotificationException(
111    String.format("Unable to extract the parameter [%s] from the [%s] NotificationDisplayerClass.",
112    propertyName, baseObject), e);
113    }
114    }
115   
 
116  0 toggle @Override
117    public DocumentReference getDocumentReference()
118    {
119  0 return (DocumentReference) this.objectReference.getParent();
120    }
121   
 
122  8 toggle @Override
123    public EntityReference getEntityReference()
124    {
125  8 return this.objectReference;
126    }
127   
 
128  5 toggle @Override
129    public DocumentReference getAuthorReference()
130    {
131  5 return this.authorReference;
132    }
133   
 
134  4 toggle @Override
135    public String getRoleHint()
136    {
137  4 return this.eventType;
138    }
139   
 
140  4 toggle @Override
141    public WikiComponentScope getScope()
142    {
143  4 return WikiComponentScope.WIKI;
144    }
145    }