1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.watchlist.internal.documents

File WatchListClassDocumentInitializer.java

 

Coverage histogram

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

Code metrics

4
24
3
1
180
80
5
0.21
8
3
1.67

Classes

Class Line # Actions
WatchListClassDocumentInitializer 56 24 0% 5 0
1.0100%
 

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.watchlist.internal.documents;
21   
22    import java.util.Collection;
23    import java.util.List;
24   
25    import javax.annotation.Priority;
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.collections4.CollectionUtils;
32    import org.apache.commons.lang3.StringUtils;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.model.EntityType;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.watchlist.internal.WatchListNotificationCache;
37    import org.xwiki.watchlist.internal.api.AutomaticWatchMode;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.internal.mandatory.AbstractMandatoryDocumentInitializer;
42    import com.xpn.xwiki.objects.classes.BaseClass;
43    import com.xpn.xwiki.objects.classes.DBListClass;
44    import com.xpn.xwiki.objects.classes.ListClass;
45    import com.xpn.xwiki.objects.classes.StaticListClass;
46   
47    /**
48    * Document initializer for {@value #DOCUMENT_FULL_NAME}.
49    *
50    * @version $Id: 4189944f32c8f4f5c8d2dc849af6cced18bc6ad4 $
51    */
52    @Component
53    @Named(WatchListClassDocumentInitializer.DOCUMENT_FULL_NAME)
54    @Singleton
55    @Priority(10000)
 
56    public class WatchListClassDocumentInitializer extends AbstractMandatoryDocumentInitializer
57    {
58    /**
59    * Class document name.
60    */
61    public static final String DOCUMENT_NAME = "WatchListClass";
62   
63    /**
64    * Class document full name.
65    */
66    public static final String DOCUMENT_FULL_NAME = XWiki.SYSTEM_SPACE + "." + DOCUMENT_NAME;
67   
68    /**
69    * Class document reference.
70    */
71    public static final EntityReference DOCUMENT_REFERENCE = new EntityReference(DOCUMENT_NAME, EntityType.DOCUMENT,
72    new EntityReference(XWiki.SYSTEM_SPACE, EntityType.SPACE));
73   
74    /**
75    * Property of the watchlist class used to store the notification interval preference.
76    */
77    public static final String INTERVAL_PROPERTY = "interval";
78   
79    /**
80    * Property of the watchlist class used to store the list of wikis to watch.
81    */
82    public static final String WIKIS_PROPERTY = "wikis";
83   
84    /**
85    * Property of the watchlist class used to store the list of spaces to watch.
86    */
87    public static final String SPACES_PROPERTY = "spaces";
88   
89    /**
90    * Property of the watchlist class used to store the list of documents to watch.
91    */
92    public static final String DOCUMENTS_PROPERTY = "documents";
93   
94    /**
95    * Property of the watchlist class used to store the list of users to watch.
96    */
97    public static final String USERS_PROPERTY = "users";
98   
99    /**
100    * Property of the watchlist class used to indicate what should be automatically watched.
101    */
102    public static final String AUTOMATICWATCH_PROPERTY = "automaticwatch";
103   
104    /**
105    * Value for the {@value #AUTOMATICWATCH_PROPERTY} property to use the default behavior.
106    */
107    public static final String AUTOMATICWATCH_DEFAULT_VALUE = "default";
108   
109    /**
110    * Used to get the list of possible values for {@value #INTERVAL_PROPERTY}.
111    */
112    @Inject
113    private Provider<WatchListNotificationCache> notificationCacheProvider;
114   
115    /**
116    * Default constructor.
117    */
 
118  1 toggle public WatchListClassDocumentInitializer()
119    {
120  1 super(DOCUMENT_REFERENCE);
121    }
122   
 
123  4 toggle @Override
124    public boolean updateDocument(XWikiDocument document)
125    {
126  4 boolean needsUpdate = false;
127  4 BaseClass bclass = document.getXClass();
128   
129    // Interval property
130  4 needsUpdate |= bclass.addStaticListField(INTERVAL_PROPERTY, "Email Notifications Interval", "");
131   
132    // Check that the interval property contains all the available intervals
133  4 StaticListClass intervalClass = (StaticListClass) bclass.get(INTERVAL_PROPERTY);
134  4 List<String> intervalValues = ListClass.getListFromString(intervalClass.getValues());
135   
136    // Look for missing or outdated intervals in the interval list
137  4 Collection<String> availableIntervals = notificationCacheProvider.get().getIntervals();
138  4 if (!CollectionUtils.disjunction(availableIntervals, intervalValues).isEmpty()) {
139  3 needsUpdate = true;
140  3 intervalClass.setValues(StringUtils.join(availableIntervals, ListClass.DEFAULT_SEPARATOR));
141    }
142   
143    // Watched elements properties
144  4 needsUpdate |= this.addWatchedElementField(bclass, WIKIS_PROPERTY, "Wiki List");
145  4 needsUpdate |= this.addWatchedElementField(bclass, SPACES_PROPERTY, "Space List");
146  4 needsUpdate |= this.addWatchedElementField(bclass, DOCUMENTS_PROPERTY, "Document List");
147  4 needsUpdate |= this.addWatchedElementField(bclass, USERS_PROPERTY, "User List");
148   
149    // Automatic watching property
150  4 String automaticWatchValues =
151    String.format("%s%s%s", AUTOMATICWATCH_DEFAULT_VALUE, ListClass.DEFAULT_SEPARATOR,
152    StringUtils.join(AutomaticWatchMode.values(), ListClass.DEFAULT_SEPARATOR));
153  4 needsUpdate |= bclass.addStaticListField(AUTOMATICWATCH_PROPERTY, "Automatic Watching", automaticWatchValues);
154   
155    // Handle the fields and the sheet of the document containing the class.
156  4 needsUpdate |= setClassDocumentFields(document, "XWiki WatchList Notification Rules Class");
157   
158  4 return needsUpdate;
159    }
160   
161    /**
162    * @param bclass the class to add to
163    * @param name the name of the property to add
164    * @param prettyName the pretty name of the property to add
165    * @return true if the property was added; false otherwise
166    */
 
167  16 toggle private boolean addWatchedElementField(BaseClass bclass, String name, String prettyName)
168    {
169  16 boolean needsUpdate = false;
170   
171  16 needsUpdate = bclass.addDBListField(name, prettyName, 80, true, null);
172  16 if (needsUpdate) {
173    // Set the input display type in order to easily debug from the object editor.
174  4 DBListClass justAddedProperty = (DBListClass) bclass.get(name);
175  4 justAddedProperty.setDisplayType(ListClass.DISPLAYTYPE_INPUT);
176    }
177   
178  16 return needsUpdate;
179    }
180    }