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

File CheckAnnotationClassEventListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

4
11
3
1
123
70
6
0.55
3.67
3
2

Classes

Class Line # Actions
CheckAnnotationClassEventListener 54 11 0% 6 14
0.2222222222.2%
 

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.annotation.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.Provider;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.annotation.AnnotationConfiguration;
32    import org.xwiki.bridge.event.DocumentCreatedEvent;
33    import org.xwiki.bridge.event.DocumentUpdatedEvent;
34    import org.xwiki.component.annotation.Role;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.observation.EventListener;
37    import org.xwiki.observation.event.Event;
38    import org.xwiki.observation.event.filter.RegexEventFilter;
39   
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
42    import com.xpn.xwiki.doc.XWikiDocument;
43   
44    /**
45    * When the wiki is initialized or the configuration document is updated, make sure that the configured annotation class
46    * contains the minimum required properties for the Annotation Application to function properly.
47    *
48    * @version $Id: bce9dfb4deeeb3db230c268d5f32f3bdf5eb3e03 $
49    * @since 4.0M2
50    */
51    @Role
52    @Named(CheckAnnotationClassEventListener.NAME)
53    @Singleton
 
54    public class CheckAnnotationClassEventListener implements EventListener
55    {
56    static final String NAME = "CheckAnnotationClassEventListener";
57   
58    /**
59    * The reference to match document the Annotations Application's configuration document on whatever wiki.
60    */
61    private static final RegexEventFilter CONFIGURATION_DOCUMENT_REFERENCE = new RegexEventFilter(String.format(
62    ".*:%s.%s", AnnotationConfiguration.CONFIGURATION_PAGE_SPACE_NAME,
63    AnnotationConfiguration.CONFIGURATION_PAGE_NAME));
64   
65    /**
66    * The matched events.
67    */
68    private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentUpdatedEvent(
69    CONFIGURATION_DOCUMENT_REFERENCE), new DocumentCreatedEvent(CONFIGURATION_DOCUMENT_REFERENCE));
70   
71    @Inject
72    protected Provider<XWikiContext> xcontextProvider;
73   
74    /**
75    * The Annotation Application's configuration.
76    */
77    @Inject
78    protected AnnotationConfiguration configuration;
79   
80    @Inject
81    @Named(AnnotationClassDocumentInitializer.HINT)
82    protected MandatoryDocumentInitializer initializer;
83   
84    @Inject
85    protected Logger logger;
86   
 
87  6 toggle @Override
88    public String getName()
89    {
90  6 return NAME;
91    }
92   
 
93  1 toggle @Override
94    public List<Event> getEvents()
95    {
96  1 return EVENTS;
97    }
98   
 
99  0 toggle @Override
100    public void onEvent(Event event, Object source, Object data)
101    {
102  0 DocumentReference annotationClassReference = this.configuration.getAnnotationClassReference();
103   
104  0 try {
105  0 if (!this.configuration.isInstalled()) {
106    // If the Annotations Application is not installed on the current wiki, do nothing.
107  0 return;
108    }
109   
110  0 XWikiContext deprecatedContext = this.xcontextProvider.get();
111  0 XWikiDocument annotationClassDocument =
112    deprecatedContext.getWiki().getDocument(annotationClassReference, deprecatedContext);
113   
114  0 if (this.initializer.updateDocument(annotationClassDocument)) {
115  0 deprecatedContext.getWiki().saveDocument(annotationClassDocument,
116    "Automatically added missing annotation class fields required by the Annotation Application.",
117    deprecatedContext);
118    }
119    } catch (Exception e) {
120  0 this.logger.error("Failed to update the configured annotation class [{}]", annotationClassReference, e);
121    }
122    }
123    }