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

File WikiDescriptorListener.java

 

Coverage histogram

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

Code metrics

10
18
4
1
119
74
12
0.67
4.5
4
3

Classes

Class Line # Actions
WikiDescriptorListener 54 18 0% 12 1
0.9687596.9%
 

Contributing tests

This file is covered by 6 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.wiki.internal.descriptor.listener;
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.event.DocumentCreatedEvent;
30    import org.xwiki.bridge.event.DocumentDeletedEvent;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.observation.EventListener;
36    import org.xwiki.observation.event.Event;
37    import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;
38    import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder;
39    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
40    import org.xwiki.wiki.internal.manager.WikiDescriptorCache;
41   
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.objects.BaseObject;
44   
45    /**
46    * Used to refresh the Wiki Descriptor Cache.
47    *
48    * @version $Id: 866db38c7b659a77c23683ed005353ceb6c2b89b $
49    * @since 5.3M2
50    */
51    @Component
52    @Named("wikidescriptor")
53    @Singleton
 
54    public class WikiDescriptorListener implements EventListener
55    {
56    /**
57    * Relative reference to the XWiki.XWikiServerClass containing wiki descriptor metadata.
58    */
59    static final EntityReference SERVER_CLASS = new EntityReference("XWikiServerClass", EntityType.DOCUMENT,
60    new EntityReference("XWiki", EntityType.SPACE));
61   
62    @Inject
63    private WikiDescriptorBuilder builder;
64   
65    @Inject
66    private WikiDescriptorCache cache;
67   
68    @Inject
69    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
70   
 
71  512 toggle @Override
72    public String getName()
73    {
74  512 return "wikidescriptor";
75    }
76   
 
77  64 toggle @Override
78    public List<Event> getEvents()
79    {
80  64 return Arrays
81    .<Event> asList(new DocumentCreatedEvent(), new DocumentUpdatedEvent(), new DocumentDeletedEvent());
82    }
83   
 
84  3971 toggle @Override
85    public void onEvent(Event event, Object source, Object data)
86    {
87  3971 XWikiDocument document = (XWikiDocument) source;
88   
89    // If the document is deleted then check the original document to see if it had XWiki Server objects and if
90    // so then unregister them
91  3971 if (event instanceof DocumentDeletedEvent || event instanceof DocumentUpdatedEvent) {
92  586 removeExistingDescriptor(document.getOriginalDocument());
93    }
94   
95    // Register the new XWiki Server objects if any
96  3971 List<BaseObject> serverClassObjects = document.getXObjects(SERVER_CLASS);
97  3971 if (serverClassObjects != null && !serverClassObjects.isEmpty()) {
98  106 DefaultWikiDescriptor descriptor = this.builder.buildDescriptorObject(serverClassObjects, document);
99  106 if (descriptor != null) {
100  106 this.cache.add(descriptor);
101  106 this.cache.setWikiIds(null);
102    }
103    }
104    }
105   
 
106  586 toggle private void removeExistingDescriptor(XWikiDocument document)
107    {
108  586 List<BaseObject> existingServerClassObjects = document.getXObjects(SERVER_CLASS);
109  586 if (existingServerClassObjects != null && !existingServerClassObjects.isEmpty()) {
110  72 String wikiId =
111    this.wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(document.getDocumentReference());
112  72 DefaultWikiDescriptor existingDescriptor = this.cache.getFromId(wikiId);
113  72 if (existingDescriptor != null) {
114  46 this.cache.remove(existingDescriptor);
115  46 this.cache.setWikiIds(null);
116    }
117    }
118    }
119    }