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

File AbstractRecordableEventDescriptor.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
22
6
1
140
76
11
0.5
3.67
6
1.83

Classes

Class Line # Actions
AbstractRecordableEventDescriptor 39 22 0% 11 1
0.972222297.2%
 

Contributing tests

This file is covered by 9 tests. .

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.eventstream;
21   
22    import javax.inject.Inject;
23   
24    import org.apache.commons.lang3.builder.EqualsBuilder;
25    import org.apache.commons.lang3.builder.HashCodeBuilder;
26    import org.slf4j.Logger;
27    import org.xwiki.component.manager.ComponentManager;
28    import org.xwiki.component.manager.NamespacedComponentManager;
29    import org.xwiki.component.namespace.NamespaceContextExecutor;
30    import org.xwiki.component.namespace.NamespaceUtils;
31    import org.xwiki.localization.ContextualLocalizationManager;
32   
33    /**
34    * Abstract implementation of {@link RecordableEventDescriptor}.
35    *
36    * @version $Id: 617314823231d5c4319ed3ded7cd323bcab9350b $
37    * @since 9.4RC1
38    */
 
39    public abstract class AbstractRecordableEventDescriptor implements RecordableEventDescriptor
40    {
41    @Inject
42    protected Logger logger;
43   
44    @Inject
45    protected ComponentManager componentManager;
46   
47    @Inject
48    protected ContextualLocalizationManager contextualLocalizationManager;
49   
50    @Inject
51    protected NamespaceContextExecutor namespaceContextExecutor;
52   
53    protected String descriptionTranslationKey;
54   
55    protected String applicationTranslationKey;
56   
57    /**
58    * Construct an AbstractRecordableEventDescriptor.
59    * @param descriptionTranslationKey the name of the translation key that describe the event
60    * @param applicationTranslationKey the translation key of the name of the application that send this event
61    */
 
62  108 toggle public AbstractRecordableEventDescriptor(String descriptionTranslationKey,
63    String applicationTranslationKey)
64    {
65  108 this.descriptionTranslationKey = descriptionTranslationKey;
66  108 this.applicationTranslationKey = applicationTranslationKey;
67    }
68   
69    /**
70    * Render a translation key in the context of the namespace (e.g. the current wiki) where the component has been
71    * loaded.
72    *
73    * Use-case: an event descriptor coming from the sub wiki is loaded and displayed in the main wiki. If the
74    * translation resource is located in the sub wiki with the "WIKI" scope, the translation could not be rendered in
75    * the main wiki. That's why we need to execute the localization in the context of the sub wiki.
76    *
77    * @param key the key to render
78    * @return the rendered localization.
79    *
80    * @since 10.6RC1
81    * @since 10.5
82    * @since 9.11.6
83    */
 
84  1586 toggle protected String getLocalizedMessage(String key)
85    {
86  1586 if (componentManager instanceof NamespacedComponentManager) {
87  1586 NamespacedComponentManager namespacedComponentManager = (NamespacedComponentManager) componentManager;
88  1586 String namespaceOfTheDescriptor = namespacedComponentManager.getNamespace();
89   
90  1586 if (namespaceOfTheDescriptor != null) {
91  4 try {
92  4 return namespaceContextExecutor.execute(NamespaceUtils.toNamespace(namespaceOfTheDescriptor),
93    () -> contextualLocalizationManager.getTranslationPlain(key));
94    } catch (Exception e) {
95  2 logger.warn("Failed to render the translation key [{}] in the namespace [{}] for the event "
96    + "descriptor of [{}].", key, namespaceOfTheDescriptor, getEventType(), e);
97    }
98    }
99    }
100   
101  1584 return contextualLocalizationManager.getTranslationPlain(key);
102    }
103   
 
104  792 toggle @Override
105    public String getDescription()
106    {
107  792 return getLocalizedMessage(descriptionTranslationKey);
108    }
109   
 
110  794 toggle @Override
111    public String getApplicationName()
112    {
113  794 return getLocalizedMessage(applicationTranslationKey);
114    }
115   
 
116  117299 toggle @Override
117    public int hashCode()
118    {
119  117299 return new HashCodeBuilder().append(getApplicationId()).append(getEventType()).toHashCode();
120    }
121   
 
122  5 toggle @Override
123    public boolean equals(Object o)
124    {
125  5 if (o == this) {
126  1 return true;
127    }
128   
129  4 if (o instanceof RecordableEventDescriptor) {
130  3 RecordableEventDescriptor other = (RecordableEventDescriptor) o;
131  3 EqualsBuilder equalsBuilder = new EqualsBuilder();
132  3 equalsBuilder.append(other.getApplicationId(), this.getApplicationId());
133  3 equalsBuilder.append(other.getEventType(), this.getEventType());
134   
135  3 return equalsBuilder.isEquals();
136    }
137   
138  1 return false;
139    }
140    }