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

File AbstractBeanEntityEventGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

4
19
7
1
135
73
10
0.53
2.71
7
1.43

Classes

Class Line # Actions
AbstractBeanEntityEventGenerator 43 19 0% 10 10
0.666666766.7%
 

Contributing tests

This file is covered by 19 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.filter.instance.internal.input;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.phase.InitializationException;
28    import org.xwiki.component.util.ReflectionUtils;
29    import org.xwiki.filter.FilterException;
30    import org.xwiki.filter.descriptor.DefaultFilterStreamBeanDescriptor;
31    import org.xwiki.filter.instance.input.AbstractEntityEventGenerator;
32    import org.xwiki.filter.instance.input.BeanEntityEventGenerator;
33    import org.xwiki.filter.instance.input.EntityEventGenerator;
34    import org.xwiki.properties.BeanManager;
35   
36    /**
37    * @param <E> the type of the entity (XWikiDocument, BaseObject, BaseClass, etc.)
38    * @param <F> the type of the filter declaring the events supported by this {@link EntityEventGenerator}
39    * @param <P> the type of the properties bean
40    * @version $Id: 5242738bf2ea161029b024bee6fcc6a28f2de0a6 $
41    * @since 6.2M1
42    */
 
43    public abstract class AbstractBeanEntityEventGenerator<E, F, P> extends AbstractEntityEventGenerator<E, F>
44    implements BeanEntityEventGenerator<E, P>
45    {
46    @Inject
47    private BeanManager beanManager;
48   
49    private String name;
50   
51    private String description;
52   
53    private Class<P> propertiesType;
54   
 
55  476 toggle @Override
56    public void initialize() throws InitializationException
57    {
58  476 super.initialize();
59   
60    // Get the type of the properties
61  476 ParameterizedType genericType =
62    (ParameterizedType) ReflectionUtils.resolveType(AbstractBeanEntityEventGenerator.class, getClass());
63  476 this.propertiesType = ReflectionUtils.getTypeClass(genericType.getActualTypeArguments()[2]);
64   
65    // Initialize FilterStream Descriptor.
66  476 DefaultFilterStreamBeanDescriptor descriptor =
67    new DefaultFilterStreamBeanDescriptor(getName(), getDescription(), this.beanManager
68  476 .getBeanDescriptor(!this.propertiesType.isInterface() ? this.propertiesType : Object.class));
69   
70  476 setDescriptor(descriptor);
71    }
72   
73    /**
74    * @param name the name to set
75    */
 
76  0 toggle public void setName(String name)
77    {
78  0 this.name = name;
79    }
80   
81    /**
82    * @return the name
83    */
 
84  476 toggle public String getName()
85    {
86  476 return this.name;
87    }
88   
89    /**
90    * @param description the description to set
91    */
 
92  0 toggle public void setDescription(String description)
93    {
94  0 this.description = description;
95    }
96   
97    /**
98    * @return the description
99    */
 
100  476 toggle public String getDescription()
101    {
102  476 return this.description;
103    }
104   
 
105  29737 toggle @Override
106    protected void write(E entity, Object filter, F internalFilter, Map<String, Object> properties)
107    throws FilterException
108    {
109  29737 P propertiesBean;
110   
111  29737 if (this.propertiesType.isInstance(properties)) {
112  29737 propertiesBean = (P) properties;
113    } else {
114  0 try {
115  0 propertiesBean = this.propertiesType.newInstance();
116   
117  0 this.beanManager.populate(propertiesBean, properties);
118    } catch (Exception e) {
119  0 throw new FilterException("Failed to convert properties to Java bean", e);
120    }
121    }
122   
123  29737 write(entity, filter, internalFilter, propertiesBean);
124    }
125   
 
126  7593 toggle @Override
127    public void write(E entity, Object filter, P properties) throws FilterException
128    {
129  7593 F internalFilter = this.filterDescriptorManager.createFilterProxy(filter, this.filterType);
130   
131  7593 write(entity, filter, internalFilter, properties);
132    }
133   
134    protected abstract void write(E entity, Object filter, F internalFilter, P properties) throws FilterException;
135    }