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

File AbstractWatchListJobDocumentInitializer.java

 

Coverage histogram

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

Code metrics

14
60
7
1
257
127
15
0.25
8.57
7
2.14

Classes

Class Line # Actions
AbstractWatchListJobDocumentInitializer 48 60 0% 15 13
0.8395061584%
 

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.Date;
23   
24    import javax.inject.Inject;
25    import javax.inject.Provider;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.slf4j.Logger;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.watchlist.internal.DefaultWatchListNotifier;
31    import org.xwiki.watchlist.internal.job.WatchListJob;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.XWikiException;
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.internal.mandatory.AbstractMandatoryDocumentInitializer;
37    import com.xpn.xwiki.objects.BaseObject;
38    import com.xpn.xwiki.plugin.scheduler.JobState;
39    import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin;
40    import com.xpn.xwiki.user.api.XWikiRightService;
41   
42    /**
43    * Abstract Document initializer for the default {@value WatchListJobClassDocumentInitializer#DOCUMENT_FULL_NAME}.
44    * documents.
45    *
46    * @version $Id: b8facad3d647da3d6e84c1c770e0220d73287813 $
47    */
 
48    public abstract class AbstractWatchListJobDocumentInitializer extends AbstractMandatoryDocumentInitializer
49    {
50    /**
51    * Name of the groups property in the XWiki rights class.
52    */
53    protected static final String XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY = "groups";
54   
55    /**
56    * Name of the levels property in the XWiki rights class.
57    */
58    protected static final String XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY = "levels";
59   
60    /**
61    * Name of the allow property in the XWiki rights class.
62    */
63    protected static final String XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY = "allow";
64   
65    /**
66    * Name of the space where default Scheduler jobs are located.
67    */
68    protected static final String SCHEDULER_SPACE = "Scheduler";
69   
70    /**
71    * Name of the jobClass property in the Scheduler job class.
72    */
73    protected static final String SCHEDULER_JOB_CLASS_PROPERTY = "jobClass";
74   
75    /**
76    * Used to access the XWiki model.
77    */
78    @Inject
79    private Provider<XWikiContext> contextProvider;
80   
81    /**
82    * Logging framework.
83    */
84    @Inject
85    private Logger logger;
86   
87    /**
88    * @param spaceName the space name of the document
89    * @param documentName the document name of the document
90    */
 
91  3 toggle public AbstractWatchListJobDocumentInitializer(String spaceName, String documentName)
92    {
93  3 super(spaceName, documentName);
94    }
95   
 
96  3 toggle @Override
97    public boolean updateDocument(XWikiDocument document)
98    {
99  3 XWikiContext context = contextProvider.get();
100  3 boolean needsUpdate = false;
101   
102  3 try {
103    // Create objects.
104  3 needsUpdate |= createSchedulerJobObject(document, getJobName(), getCron(), context);
105   
106  3 needsUpdate |= createWatchListJobRightsObject(document, context);
107   
108  3 needsUpdate |= createWatchListJobObject(document, getMessageTemplateDocument(), context);
109   
110    // Set basic document fields.
111  3 needsUpdate |= setDocumentFields(document, getDocumentTitle());
112    } catch (Exception e) {
113  0 logger.error("Failed to initialize document [{}]", getDocumentReference(), e);
114    }
115   
116  3 return needsUpdate;
117    }
118   
119    /**
120    * @return the cron expression used to run the Scheduler job
121    */
122    protected abstract String getCron();
123   
124    /**
125    * @return the name of the Scheduler job
126    */
127    protected abstract String getJobName();
128   
129    /**
130    * @return the document title
131    */
 
132  3 toggle protected String getDocumentTitle()
133    {
134  3 String title = String.format("$services.localization.render('%s')", getDocumentTitleTranslationKey());
135  3 return title;
136    }
137   
138    /**
139    * @return the translation key to be used in the document's title
140    */
141    protected abstract String getDocumentTitleTranslationKey();
142   
143    /**
144    * @return the name of the document where the message template for this job is located
145    */
 
146  3 toggle protected String getMessageTemplateDocument()
147    {
148  3 return DefaultWatchListNotifier.DEFAULT_EMAIL_TEMPLATE;
149    }
150   
151    /**
152    * @param doc the document to update
153    * @param jobName the scheduler job name
154    * @param cron the cron expression
155    * @param context the XWiki context
156    * @return true if the document has been updated, false otherwise
157    * @throws XWikiException
158    */
 
159  3 toggle private static boolean createSchedulerJobObject(XWikiDocument document, String jobName, String cron,
160    XWikiContext context) throws XWikiException
161    {
162  3 boolean needsUpdate = false;
163   
164  3 BaseObject job = document.getXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE);
165  3 if (job == null) {
166  3 needsUpdate = true;
167  3 job = document.newXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE, context);
168  3 job.setStringValue("jobName", jobName);
169  3 job.setStringValue(SCHEDULER_JOB_CLASS_PROPERTY, WatchListJob.class.getName());
170  3 job.setStringValue("cron", cron);
171  3 job.setStringValue("contextUser", XWikiRightService.SUPERADMIN_USER_FULLNAME);
172  3 job.setStringValue("contextLang", "en");
173  3 job.setStringValue("contextDatabase", "xwiki");
174  3 job.setStringValue("status", JobState.STATE_NORMAL);
175    } else {
176    // Make sure we are using the proper job class.
177  0 String jobClass = job.getStringValue(SCHEDULER_JOB_CLASS_PROPERTY);
178  0 if (!WatchListJob.class.getName().equals(jobClass)) {
179  0 job.setStringValue(SCHEDULER_JOB_CLASS_PROPERTY, WatchListJob.class.getName());
180   
181  0 needsUpdate = true;
182    }
183    }
184   
185  3 return needsUpdate;
186    }
187   
188    /**
189    * Create the XWiki rights object in the scheduler job document.
190    *
191    * @param doc the document to update
192    * @param context the XWiki context
193    * @return true if the document has been updated, false otherwise
194    * @throws XWikiException if the object creation fails
195    */
 
196  3 toggle private static boolean createWatchListJobRightsObject(XWikiDocument doc, XWikiContext context)
197    throws XWikiException
198    {
199  3 boolean needsUpdate = false;
200  3 DocumentReference righsClassReference = context.getWiki().getRightsClass(context).getDocumentReference();
201   
202  3 BaseObject editRights = doc.getXObject(righsClassReference, 0);
203  3 BaseObject viewRights = doc.getXObject(righsClassReference, 1);
204   
205  3 if (editRights == null) {
206  3 editRights = doc.newXObject(righsClassReference, context);
207  3 editRights.setLargeStringValue(XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY, "XWiki.XWikiAdminGroup");
208  3 editRights.setStringValue(XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY, "edit,delete");
209  3 editRights.setIntValue(XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY, 1);
210  3 needsUpdate = true;
211    }
212   
213  3 if (viewRights == null) {
214  3 viewRights = doc.newXObject(righsClassReference, context);
215  3 viewRights.setLargeStringValue(XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY, "XWiki.XWikiAllGroup");
216  3 viewRights.setStringValue(XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY, "view");
217  3 viewRights.setIntValue(XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY, 1);
218  3 needsUpdate = true;
219    }
220   
221  3 return needsUpdate;
222    }
223   
224    /**
225    * Create the watchlist job object in the scheduler job document.
226    *
227    * @param doc Scheduler job document
228    * @param emailTemplate email template to use for the job
229    * @param context the XWiki context
230    * @return true if the document has been updated, false otherwise
231    * @throws XWikiException if the object creation fails
232    */
 
233  3 toggle private static boolean createWatchListJobObject(XWikiDocument doc, String emailTemplate, XWikiContext context)
234    throws XWikiException
235    {
236  3 BaseObject obj = null;
237  3 boolean needsupdate = false;
238   
239  3 obj = doc.getXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE);
240  3 if (obj == null) {
241  3 obj = doc.newXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE, context);
242  3 needsupdate = true;
243    }
244   
245  3 if (StringUtils.isBlank(obj.getStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD))) {
246  3 obj.setStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD, emailTemplate);
247  3 needsupdate = true;
248    }
249   
250  3 if (obj.getDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD) == null) {
251  3 obj.setDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD, new Date());
252  3 needsupdate = true;
253    }
254   
255  3 return needsupdate;
256    }
257    }