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

File DefaultWatchedEntitiesConfiguration.java

 

Coverage histogram

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

Code metrics

16
31
8
1
179
110
21
0.68
3.88
8
2.62

Classes

Class Line # Actions
DefaultWatchedEntitiesConfiguration 50 31 0% 21 2
0.9636363496.4%
 

Contributing tests

This file is covered by 2 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.notifications.filters.watch.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.LocalDocumentReference;
34    import org.xwiki.model.reference.WikiReference;
35    import org.xwiki.notifications.filters.watch.AutomaticWatchMode;
36    import org.xwiki.notifications.filters.watch.WatchedEntitiesConfiguration;
37    import org.xwiki.text.StringUtils;
38    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
39   
40    import com.xpn.xwiki.internal.XWikiCfgConfigurationSource;
41   
42    /**
43    * Default implementation of {@link WatchedEntitiesConfiguration}.
44    *
45    * @version $Id: 8532ffc5d142710951119de5e7692bea95e2c38c $
46    * @since 9.8RC1
47    */
48    @Component
49    @Singleton
 
50    public class DefaultWatchedEntitiesConfiguration implements WatchedEntitiesConfiguration
51    {
52    private static final String XWIKI_SPACE = "XWiki";
53   
54    private static final List<String> CODE_SPACE = Arrays.asList(XWIKI_SPACE, "Notifications", "Code");
55   
56    private static final LocalDocumentReference CLASS_REFERENCE = new LocalDocumentReference(CODE_SPACE,
57    "AutomaticWatchModeClass");
58   
59    private static final LocalDocumentReference WATCHLIST_REFERENCE = new LocalDocumentReference(XWIKI_SPACE,
60    "WatchListClass");
61   
62    private static final LocalDocumentReference CONFIGURATION_REFERENCE = new LocalDocumentReference(CODE_SPACE,
63    "NotificationAdministration");
64   
65    private static final String AUTOMATIC_WATCH_MODE = "automaticWatchMode";
66   
67    private static final String WATCHLIST_AUTOWATCH_PROPERTY = "xwiki.plugin.watchlist.automaticwatch";
68   
69    @Inject
70    private DocumentAccessBridge documentAccessBridge;
71   
72    @Inject
73    private ConfigurationSource configurationSource;
74   
75    @Inject
76    @Named(XWikiCfgConfigurationSource.ROLEHINT)
77    private ConfigurationSource xwikiCfgConfigurationSource;
78   
79    @Inject
80    private WikiDescriptorManager wikiDescriptorManager;
81   
 
82  34274 toggle @Override
83    public boolean isEnabled()
84    {
85  34274 return configurationSource.getProperty("notifications.watchedEntities.enabled", true);
86    }
87   
 
88  926 toggle @Override
89    public AutomaticWatchMode getAutomaticWatchMode(DocumentReference user)
90    {
91  926 Object value = documentAccessBridge.getProperty(user, getAbsoluteClassReference(user),
92    AUTOMATIC_WATCH_MODE);
93  926 if (value != null && StringUtils.isNotBlank((String) value)) {
94  82 return AutomaticWatchMode.valueOf((String) value);
95    }
96   
97    // Fallback to the value of the Watchlist
98  844 value = documentAccessBridge.getProperty(user, getAbsoluteWatchlistClassReference(user), "automaticwatch");
99  844 if (value != null && StringUtils.isNotBlank((String) value) && !"default".equals(value)) {
100  761 return AutomaticWatchMode.valueOf((String) value);
101    }
102   
103    // Fallback to default
104  83 return getDefaultAutomaticWatchMode(user.getWikiReference());
105    }
106   
 
107  88 toggle @Override
108    public AutomaticWatchMode getDefaultAutomaticWatchMode(WikiReference wikiReference)
109    {
110  88 Object value = documentAccessBridge.getProperty(getAbsoluteConfigurationReference(wikiReference),
111    getAbsoluteClassReference(wikiReference), AUTOMATIC_WATCH_MODE);
112  88 if (value != null && StringUtils.isNotBlank((String) value)) {
113  1 return AutomaticWatchMode.valueOf((String) value);
114    }
115   
116    // Fallback to the value of the main wiki
117  87 WikiReference mainWiki = new WikiReference(wikiDescriptorManager.getMainWikiId());
118  87 if (!wikiReference.equals(mainWiki)) {
119  5 value = documentAccessBridge.getProperty(getAbsoluteConfigurationReference(mainWiki),
120    getAbsoluteClassReference(mainWiki), AUTOMATIC_WATCH_MODE);
121  5 if (value != null && StringUtils.isNotBlank((String) value)) {
122  1 return AutomaticWatchMode.valueOf((String) value);
123    }
124    }
125   
126    // Fallback to the default automatic watch mode as it is configured in properties files.
127  86 return getAutomaticWatchMode();
128    }
129   
 
130  86 toggle private AutomaticWatchMode getAutomaticWatchMode()
131    {
132    // Look into the configuration.
133  86 Object value = configurationSource.getProperty("notifications.watchedEntities.autoWatch");
134  86 if (value != null) {
135  1 return AutomaticWatchMode.valueOf(((String) value).toUpperCase());
136    }
137   
138    // Fallback to the configuration of the watchlist (if it exists)
139    // For historic reason, we look both in xwiki.properties and xwiki.cfg.
140    // - xwiki.cfg because it is where the watchlist configuration belongs to
141    // - xwiki.properties because we badly look there between XWiki 9.8RC1 and 10.6 and we don't want to break the
142    // configuration of people who rely on it.
143  85 value = configurationSource.getProperty(WATCHLIST_AUTOWATCH_PROPERTY);
144  85 if (value != null) {
145  1 return AutomaticWatchMode.valueOf(((String) value).toUpperCase());
146    }
147   
148    // So here we load in xwiki.cfg.
149    // Note that it would have been better to just use the "all" ConfigurationSource, but for some reason it does
150    // not handle xwiki.cfg.
151  84 value = xwikiCfgConfigurationSource.getProperty(WATCHLIST_AUTOWATCH_PROPERTY);
152  84 if (value != null) {
153  0 return AutomaticWatchMode.valueOf(((String) value).toUpperCase());
154    }
155   
156    // Default
157  84 return AutomaticWatchMode.MAJOR;
158    }
159   
 
160  926 toggle private DocumentReference getAbsoluteClassReference(DocumentReference user)
161    {
162  926 return new DocumentReference(CLASS_REFERENCE, user.getWikiReference());
163    }
164   
 
165  93 toggle private DocumentReference getAbsoluteClassReference(WikiReference wikiReference)
166    {
167  93 return new DocumentReference(CLASS_REFERENCE, wikiReference);
168    }
169   
 
170  844 toggle private DocumentReference getAbsoluteWatchlistClassReference(DocumentReference user)
171    {
172  844 return new DocumentReference(WATCHLIST_REFERENCE, user.getWikiReference());
173    }
174   
 
175  93 toggle private DocumentReference getAbsoluteConfigurationReference(WikiReference wikiReference)
176    {
177  93 return new DocumentReference(CONFIGURATION_REFERENCE, wikiReference);
178    }
179    }