1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.filter.output

File BaseObjectOutputFilterStream.java

 

Coverage histogram

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

Code metrics

26
56
9
1
209
143
28
0.5
6.22
9
3.11

Classes

Class Line # Actions
BaseObjectOutputFilterStream 50 56 0% 28 9
0.901098990.1%
 

Contributing tests

This file is covered by 46 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 com.xpn.xwiki.internal.filter.output;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.annotation.InstantiationStrategy;
28    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
29    import org.xwiki.component.phase.Initializable;
30    import org.xwiki.component.phase.InitializationException;
31    import org.xwiki.filter.FilterEventParameters;
32    import org.xwiki.filter.FilterException;
33    import org.xwiki.filter.event.model.WikiObjectFilter;
34    import org.xwiki.model.reference.EntityReference;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.objects.BaseObject;
39    import com.xpn.xwiki.objects.BaseObjectReference;
40    import com.xpn.xwiki.objects.BaseProperty;
41    import com.xpn.xwiki.objects.classes.BaseClass;
42    import com.xpn.xwiki.objects.classes.PropertyClass;
43   
44    /**
45    * @version $Id: 41a711363cf623b0a2ed4e452e902c36efe18ed2 $
46    * @since 9.0RC1
47    */
48    @Component
49    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
50    public class BaseObjectOutputFilterStream extends AbstractEntityOutputFilterStream<BaseObject> implements Initializable
51    {
52    @Inject
53    private EntityOutputFilterStream<BaseClass> classFilter;
54   
55    @Inject
56    private EntityOutputFilterStream<BaseProperty> propertyFilter;
57   
58    @Inject
59    private Provider<XWikiContext> xcontextProvider;
60   
61    private BaseObject externalEntity;
62   
63    private BaseClass databaseXClass;
64   
65    private BaseClass currentXClass;
66   
 
67  7652 toggle @Override
68    public void initialize() throws InitializationException
69    {
70  7652 initialize(this.classFilter, this.propertyFilter);
71    }
72   
 
73  7370 toggle @Override
74    public void setEntity(BaseObject entity)
75    {
76  7370 super.setEntity(entity);
77   
78  7370 this.externalEntity = entity;
79    }
80   
 
81  36848 toggle private BaseClassOutputFilterStream getBaseClassOutputFilterStream()
82    {
83  36848 return (BaseClassOutputFilterStream) this.classFilter;
84    }
85   
 
86  141602 toggle private BasePropertyOutputFilterStream getBasePropertyOutputFilterStream()
87    {
88  141602 return (BasePropertyOutputFilterStream) this.propertyFilter;
89    }
90   
 
91  14736 toggle private void setCurrentXClass(BaseClass xclass)
92    {
93  14736 this.currentXClass = xclass;
94  14736 getBasePropertyOutputFilterStream().setCurrentXClass(xclass);
95    }
96   
97    // Events
98   
 
99  8051 toggle @Override
100    public void endWikiClass(FilterEventParameters parameters) throws FilterException
101    {
102  8051 if (this.entity != null) {
103  7369 BaseClass xclass = getBaseClassOutputFilterStream().getEntity();
104   
105  7369 if (xclass != null) {
106    // Re-create the object instance if not already provided and if there is a custom class
107  7369 if (this.externalEntity == null && StringUtils.isNotEmpty(xclass.getCustomClass())) {
108  10 BaseObject customObject;
109  10 try {
110  10 customObject = xclass.newCustomClassInstance(this.xcontextProvider.get());
111  0 customObject.setDocumentReference(this.entity.getDocumentReference());
112  0 customObject.setXClassReference(this.entity.getXClassReference());
113  0 customObject.setOwnerDocument(this.entity.getOwnerDocument());
114    // Pass false as an optimization since there is nothing to clean on a new object
115  0 customObject.apply(this.entity, false);
116   
117  0 this.entity = customObject;
118    } catch (XWikiException e) {
119    // TODO: should probably log a warning
120    }
121    }
122   
123  7369 setCurrentXClass(xclass);
124  7369 getBaseClassOutputFilterStream().setEntity(null);
125    }
126    }
127    }
128   
 
129  7370 toggle @Override
130    public void beginWikiObject(String name, FilterEventParameters parameters) throws FilterException
131    {
132  7370 super.beginWikiObject(name, parameters);
133   
134  7370 if (this.enabled) {
135  7370 if (this.entity == null) {
136  7370 this.entity = new BaseObject();
137    }
138   
139  7370 if (parameters.containsKey(WikiObjectFilter.PARAMETER_NAME)) {
140  7365 this.entity
141    .setDocumentReference(getDocumentReference(WikiObjectFilter.PARAMETER_NAME, parameters, null));
142    }
143   
144  7370 int number = getInt(WikiObjectFilter.PARAMETER_NUMBER, parameters, -1);
145   
146  7370 EntityReference classReference =
147    getEntityReference(WikiObjectFilter.PARAMETER_CLASS_REFERENCE, parameters, null);
148  7370 if (classReference == null) {
149  3 BaseObjectReference reference = new BaseObjectReference(this.currentEntityReference);
150   
151  3 classReference = reference.getXClassReference();
152   
153  3 if (number < 0 && reference.getObjectNumber() != null) {
154  2 number = reference.getObjectNumber();
155    }
156    }
157  7370 this.entity.setXClassReference(classReference);
158   
159    // Get the class in the current instance
160  7370 XWikiContext xcontext = this.xcontextProvider.get();
161  7370 if (xcontext != null && xcontext.getWiki() != null) {
162  7367 try {
163  7367 this.databaseXClass = xcontext.getWiki().getXClass(this.entity.getXClassReference(), xcontext);
164  7367 setCurrentXClass(this.databaseXClass);
165    } catch (XWikiException e) {
166    // TODO: log something ?
167    }
168    }
169   
170  7370 this.entity.setNumber(number);
171   
172  7370 this.entity.setGuid(getString(WikiObjectFilter.PARAMETER_GUID, parameters, null));
173   
174  7370 getBaseClassOutputFilterStream().enable();
175  7370 getBasePropertyOutputFilterStream().enable();
176    }
177    }
178   
 
179  7370 toggle @Override
180    public void endWikiObject(String name, FilterEventParameters parameters) throws FilterException
181    {
182    // Add missing properties from the class
183  7370 if (this.entity != null && this.databaseXClass != null) {
184  7367 for (String key : this.databaseXClass.getPropertyList()) {
185  45328 if (this.entity.safeget(key) == null) {
186  9819 PropertyClass classProperty = (PropertyClass) this.databaseXClass.getField(key);
187  9819 this.entity.safeput(key, classProperty.newProperty());
188    }
189    }
190    }
191   
192  7370 super.endWikiObject(name, parameters);
193   
194  7370 getBaseClassOutputFilterStream().disable();
195  7370 getBaseClassOutputFilterStream().disable();
196    }
197   
 
198  39866 toggle @Override
199    public void onWikiObjectProperty(String name, Object value, FilterEventParameters parameters) throws FilterException
200    {
201  39866 if (this.enabled) {
202  39866 if (getBasePropertyOutputFilterStream().getEntity() != null) {
203  39815 this.entity.safeput(name, getBasePropertyOutputFilterStream().getEntity());
204   
205  39815 getBasePropertyOutputFilterStream().setEntity(null);
206    }
207    }
208    }
209    }