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

File WikiObjectComponentManagerEventListenerProxy.java

 

Coverage histogram

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

Code metrics

4
33
5
1
209
118
10
0.3
6.6
5
2

Classes

Class Line # Actions
WikiObjectComponentManagerEventListenerProxy 61 33 0% 10 3
0.928571492.9%
 

Contributing tests

This file is covered by 4 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.ArrayList;
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.apache.commons.lang3.exception.ExceptionUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.wiki.WikiComponent;
36    import org.xwiki.component.wiki.WikiComponentException;
37    import org.xwiki.component.wiki.WikiObjectComponentBuilder;
38    import org.xwiki.component.wiki.internal.WikiComponentManagerEventListenerHelper;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.DocumentReferenceResolver;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.model.reference.EntityReferenceSerializer;
43    import org.xwiki.model.reference.ObjectReference;
44    import org.xwiki.query.Query;
45    import org.xwiki.query.QueryManager;
46   
47    import com.xpn.xwiki.XWikiContext;
48    import com.xpn.xwiki.doc.XWikiDocument;
49    import com.xpn.xwiki.objects.BaseObject;
50    import com.xpn.xwiki.objects.BaseObjectReference;
51   
52    /**
53    * This component allows the {@link DefaultWikiObjectComponentManagerEventListener} to easily register new XObject based
54    * components against the {@link WikiComponentManagerEventListenerHelper}.
55    *
56    * @version $Id: 9a4af6dfc65c7faddc32c082cfe4080fb009dde5 $
57    * @since 9.5RC1
58    */
59    @Component(roles = WikiObjectComponentManagerEventListenerProxy.class)
60    @Singleton
 
61    public class WikiObjectComponentManagerEventListenerProxy
62    {
63    @Inject
64    private Logger logger;
65   
66    @Inject
67    @Named("context")
68    private ComponentManager contextComponent;
69   
70    @Inject
71    private WikiComponentManagerEventListenerHelper wikiComponentManagerEventListenerHelper;
72   
73    @Inject
74    private QueryManager queryManager;
75   
76    @Inject
77    private Provider<XWikiContext> xWikiContextProvider;
78   
79    @Inject
80    @Named("local")
81    private EntityReferenceSerializer<String> entityReferenceSerializer;
82   
83    @Inject
84    @Named("current")
85    private DocumentReferenceResolver<String> currentDocumentReferenceResolver;
86   
87    /**
88    * This method is responsible look through every {@link WikiObjectComponentBuilder} and get their role hints, which
89    * is also the class name of the wiki object they should be working with.
90    */
 
91  79 toggle private List<EntityReference> collectWikiObjectsList()
92    {
93  79 List<EntityReference> wikiObjectsList = new ArrayList<>();
94   
95  79 try {
96    // Get a list of WikiObjectComponentBuilder
97  79 List<WikiObjectComponentBuilder> componentBuilders =
98    this.contextComponent.getInstanceList(WikiObjectComponentBuilder.class);
99   
100  79 for (WikiObjectComponentBuilder componentBuilder : componentBuilders) {
101  231 wikiObjectsList.add(componentBuilder.getClassReference());
102    }
103    } catch (ComponentLookupException e) {
104  0 logger.warn("Unable to collect a list of wiki objects components: %s", e);
105    }
106   
107  79 return wikiObjectsList;
108    }
109   
110    /**
111    * This method goes through every XObject known (using
112    * {@link WikiObjectComponentManagerEventListenerProxy#collectWikiObjectsList()}) to be able to create a component
113    * and then builds those components.
114    */
 
115  78 toggle public void registerAllObjectComponents()
116    {
117  78 XWikiContext xWikiContext = this.xWikiContextProvider.get();
118  78 String builderHelper;
119   
120    // For every classes subject to WikiComponents
121  78 for (EntityReference xObjectClass : this.collectWikiObjectsList()) {
122  229 try {
123    // Query every document having an XObject of the given class attached
124  229 Query query =
125    queryManager.createQuery("select distinct doc.fullName from Document doc, doc.object("
126    + this.entityReferenceSerializer.serialize(xObjectClass) + ") as document", Query.XWQL);
127   
128  229 List<String> results = query.execute();
129   
130  229 DocumentReference sourceDocumentReference;
131  229 for (String result : results) {
132  1217 sourceDocumentReference = this.currentDocumentReferenceResolver.resolve(result);
133  1217 XWikiDocument document = xWikiContext.getWiki().getDocument(sourceDocumentReference, xWikiContext);
134   
135  1217 for (BaseObject xObject : document.getXObjects(xObjectClass)) {
136  1217 if (xObject != null) {
137  1217 BaseObjectReference xObjectReference = xObject.getReference();
138  1217 builderHelper =
139    this.entityReferenceSerializer.serialize(xObjectReference.getXClassReference());
140   
141  1217 this.registerObjectComponents(xObjectReference, xObject,
142    this.contextComponent.getInstance(WikiObjectComponentBuilder.class, builderHelper));
143    }
144    }
145    }
146    } catch (Exception e) {
147  0 logger.warn(String.format("Unable to register the components for [%s] XObjects: %s", xObjectClass,
148    ExceptionUtils.getRootCauseMessage(e)));
149    }
150    }
151    }
152   
153    /**
154    * This method uses the given objectReference and a XWikiDocument that is the source of every
155    * {@link com.xpn.xwiki.internal.event.XObjectEvent} to build and register the component(s) contained in this entity
156    * reference.
157    *
158    * @param objectReference the reference containing the parameters needed to instantiate the new component(s)
159    * @param baseObject the base object corresponding to the XObject
160    * @param componentBuilder the builder that should be used in order to build the component
161    */
 
162  1899 toggle public void registerObjectComponents(ObjectReference objectReference, BaseObject baseObject,
163    WikiObjectComponentBuilder componentBuilder)
164    {
165    // Unregister all wiki components registered under the given entity. We do this as otherwise we would need to
166    // handle the specific cases of elements added, elements updated and elements deleted, etc.
167    // Instead we unregister all wiki components and re-register them all.
168  1899 this.wikiComponentManagerEventListenerHelper.unregisterComponents(objectReference);
169   
170  1899 try {
171    /*
172    * If we are dealing with a WikiBaseObjectComponentBuilder, we directly get the base object corresponding to
173    * the current event and build the components from it.
174    */
175  1899 List<WikiComponent> wikiComponents;
176  1899 if (componentBuilder instanceof WikiBaseObjectComponentBuilder) {
177  1898 wikiComponents = ((WikiBaseObjectComponentBuilder) componentBuilder).buildComponents(baseObject);
178    } else {
179  1 wikiComponents = componentBuilder.buildComponents(objectReference);
180    }
181   
182  1895 this.wikiComponentManagerEventListenerHelper.registerComponentList(wikiComponents);
183    } catch (WikiComponentException e) {
184  4 logger.warn(String.format("Unable to register the component associated to [%s]: %s", objectReference,
185    ExceptionUtils.getRootCauseMessage(e)));
186    }
187    }
188   
189    /**
190    * Unregisters the component(s) linked to the given entity reference from the component manager.
191    *
192    * @param xObjectReference the reference containing every component that should be removed from the CM
193    */
 
194  258 toggle public void unregisterObjectComponents(ObjectReference xObjectReference)
195    {
196  258 this.wikiComponentManagerEventListenerHelper.unregisterComponents(xObjectReference);
197    }
198   
199    /**
200    * Get a list of XClasses that are able to produce a component (sc : that have a {@link WikiObjectComponentBuilder}
201    * bound to it).
202    *
203    * @return the list of classes
204    */
 
205  1 toggle public List<EntityReference> getWikiObjectsList()
206    {
207  1 return this.collectWikiObjectsList();
208    }
209    }