1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki.internal.bridge

File DefaultWikiObjectComponentManagerEventListener.java

 

Coverage histogram

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

Code metrics

20
31
6
1
189
116
20
0.65
5.17
6
3.33

Classes

Class Line # Actions
DefaultWikiObjectComponentManagerEventListener 60 31 0% 20 5
0.912280791.2%
 

Contributing tests

This file is covered by 20 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.component.wiki.internal.bridge;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.bridge.event.AbstractDocumentEvent;
31    import org.xwiki.bridge.event.ApplicationReadyEvent;
32    import org.xwiki.bridge.event.DocumentCreatedEvent;
33    import org.xwiki.bridge.event.DocumentDeletedEvent;
34    import org.xwiki.bridge.event.DocumentUpdatedEvent;
35    import org.xwiki.bridge.event.WikiReadyEvent;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.component.manager.ComponentLookupException;
38    import org.xwiki.component.manager.ComponentManager;
39    import org.xwiki.component.wiki.WikiObjectComponentBuilder;
40    import org.xwiki.component.wiki.internal.DefaultWikiComponentManagerEventListener;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.EntityReferenceSerializer;
43    import org.xwiki.observation.AbstractEventListener;
44    import org.xwiki.observation.EventListener;
45    import org.xwiki.observation.event.Event;
46   
47    import com.xpn.xwiki.doc.XWikiDocument;
48    import com.xpn.xwiki.objects.BaseObject;
49   
50    /**
51    * This {@link EventListener} is responsible for registering wiki components derived from XObjects using the same
52    * principle as {@link DefaultWikiComponentManagerEventListener}.
53    *
54    * @version $Id: 0d96f530f42fe4b20aa65a7861b633d538927435 $
55    * @since 9.5RC1
56    */
57    @Component
58    @Named(DefaultWikiObjectComponentManagerEventListener.EVENT_LISTENER_NAME)
59    @Singleton
 
60    public class DefaultWikiObjectComponentManagerEventListener extends AbstractEventListener
61    {
62    /**
63    * The event listener name.
64    */
65    public static final String EVENT_LISTENER_NAME = "defaultWikiObjectComponentManagerEventListener";
66   
67    @Inject
68    @Named("context")
69    private ComponentManager componentManager;
70   
71    @Inject
72    private WikiObjectComponentManagerEventListenerProxy wikiObjectComponentManagerEventListenerProxy;
73   
74    @Inject
75    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
76   
77    @Inject
78    @Named("local")
79    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
80   
81    @Inject
82    private Logger logger;
83   
84    /**
85    * Builds a new {@link DefaultWikiObjectComponentManagerEventListener}.
86    */
 
87  96 toggle public DefaultWikiObjectComponentManagerEventListener()
88    {
89  96 super(DefaultWikiObjectComponentManagerEventListener.EVENT_LISTENER_NAME, new ApplicationReadyEvent(),
90    new WikiReadyEvent(), new DocumentCreatedEvent(), new DocumentUpdatedEvent(), new DocumentDeletedEvent());
91    }
92   
 
93  8384 toggle @Override
94    public void onEvent(Event event, Object source, Object data)
95    {
96  8384 if (event instanceof AbstractDocumentEvent) {
97  8303 handleDocumentEvents((AbstractDocumentEvent) event, (XWikiDocument) source);
98   
99    /*
100    * If we are at application startup time, we have to instantiate every document or object that we can find
101    * in the wiki
102    */
103  81 } else if (event instanceof ApplicationReadyEvent || event instanceof WikiReadyEvent) {
104    // These 2 events are created when the database is ready. We register all wiki components.
105    // Collect every WikiObjectComponentBuilder
106  80 this.wikiObjectComponentManagerEventListenerProxy.registerAllObjectComponents();
107    }
108    }
109   
 
110  8303 toggle private void handleDocumentEvents(AbstractDocumentEvent event, XWikiDocument document)
111    {
112  8303 if (event instanceof DocumentUpdatedEvent || event instanceof DocumentDeletedEvent) {
113  2647 unRegisterObjectComponents(document.getOriginalDocument());
114    }
115   
116  8303 if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
117  8014 registerObjectComponents(document);
118    }
119    }
120   
 
121  8014 toggle private void registerObjectComponents(XWikiDocument document)
122    {
123  8014 for (Map.Entry<DocumentReference, List<BaseObject>> entry : document.getXObjects().entrySet()) {
124  7399 WikiObjectComponentBuilder componentBuilder = getAssociatedComponentBuilder(entry.getKey());
125   
126  7399 if (componentBuilder != null) {
127  682 for (BaseObject baseObject : entry.getValue()) {
128  683 if (baseObject != null) {
129  682 this.wikiObjectComponentManagerEventListenerProxy
130    .registerObjectComponents(baseObject.getReference(), baseObject, componentBuilder);
131    }
132    }
133    }
134    }
135    }
136   
 
137  2647 toggle private void unRegisterObjectComponents(XWikiDocument document)
138    {
139  2647 for (Map.Entry<DocumentReference, List<BaseObject>> entry : document.getXObjects().entrySet()) {
140  3074 WikiObjectComponentBuilder componentBuilder = getAssociatedComponentBuilder(entry.getKey());
141   
142  3074 if (componentBuilder != null) {
143  258 for (BaseObject baseObject : entry.getValue()) {
144  258 if (baseObject != null) {
145  258 this.wikiObjectComponentManagerEventListenerProxy
146    .unregisterObjectComponents(baseObject.getReference());
147    }
148    }
149    }
150    }
151    }
152   
153    /**
154    * Get the {@link WikiObjectComponentBuilder} associated to the given {@link DocumentReference}. This method checks
155    * if a component builder exists for the document reference and then for the local document reference. If no builder
156    * can is found, returns null.
157    *
158    * @param xClassReference the class reference used when searching for the component builder
159    * @return the first component builder that has been found, null if no builder is found
160    */
 
161  10473 toggle private WikiObjectComponentBuilder getAssociatedComponentBuilder(DocumentReference xClassReference)
162    {
163  10473 try {
164    // First, try to find a component that has a hint related to the DocumentReference of the XClass
165  10473 String serializedXClassReference = this.defaultEntityReferenceSerializer.serialize(xClassReference);
166  10473 if (this.componentManager.hasComponent(WikiObjectComponentBuilder.class, serializedXClassReference)) {
167  0 return this.componentManager.getInstance(WikiObjectComponentBuilder.class, serializedXClassReference);
168    } else {
169    // If no component has been found, try again with a LocalDocumentReference
170  10473 serializedXClassReference = this.localEntityReferenceSerializer.serialize(xClassReference);
171  10473 if (this.componentManager.hasComponent(WikiObjectComponentBuilder.class, serializedXClassReference)) {
172  940 return this.componentManager.getInstance(WikiObjectComponentBuilder.class,
173    serializedXClassReference);
174    } else {
175  9533 return null;
176    }
177    }
178    } catch (ComponentLookupException e) {
179    /*
180    * As we test if a component is present in the component manager before fetching it, we shouldn't get any
181    * exception at this point.
182    */
183  0 this.logger.error(String.format(
184    "Unable to find a WikiObjectComponentBuilder associated to the helper [%s]: %s", xClassReference, e));
185   
186  0 return null;
187    }
188    }
189    }