1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.remote.json.processor.bean;
|
11
|
|
12
|
import java.beans.PropertyDescriptor;
|
13
|
import java.lang.reflect.InvocationTargetException;
|
14
|
import java.util.Collection;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import net.sf.json.JSONException;
|
21
|
import net.sf.json.JSONObject;
|
22
|
import net.sf.json.JsonConfig;
|
23
|
import net.sf.json.processors.JsonBeanProcessor;
|
24
|
import net.sf.json.processors.JsonValueProcessor;
|
25
|
import net.sf.json.processors.JsonVerifier;
|
26
|
import net.sf.json.util.PropertyFilter;
|
27
|
|
28
|
import org.apache.commons.beanutils.PropertyUtils;
|
29
|
import org.apache.log4j.Logger;
|
30
|
import org.hibernate.Hibernate;
|
31
|
|
32
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @date 30.03.2009
|
38
|
*
|
39
|
*/
|
40
|
public abstract class AbstractBeanProcessor<T extends Object> implements JsonBeanProcessor{
|
41
|
|
42
|
public static final Logger logger = Logger.getLogger(AbstractBeanProcessor.class);
|
43
|
|
44
|
private Set<String> excludes = new HashSet<String>();
|
45
|
|
46
|
private Set<String> mergedExcludes = null;
|
47
|
|
48
|
public Set<String> getExcludes() {
|
49
|
return excludes;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* This method allows supplying a List of property names to be ignored
|
54
|
* during the serialization to JSON. The <code>excludes</code> will be
|
55
|
* merged with the property names configured by subclasses which override
|
56
|
* {@link {@link #getIgnorePropNames()}.
|
57
|
*
|
58
|
* @param excludes
|
59
|
*/
|
60
|
public void setExcludes(Set<String> excludes) {
|
61
|
this.excludes = excludes;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Implementations of this abstract class may override this method in order
|
66
|
* to supply a List of property names to be ignored in
|
67
|
* {@link #processBean(Object, JsonConfig)}. This feature generally is used
|
68
|
* when {@link #processBeanSecondStep(CdmBase, JSONObject, JsonConfig)} is
|
69
|
* implemented. such that this method is responsible of serializing this
|
70
|
* property.
|
71
|
*
|
72
|
* @return a List of property names.
|
73
|
*/
|
74
|
public abstract List<String> getIgnorePropNames();
|
75
|
|
76
|
/**
|
77
|
* merges and returns {@link {@link #getIgnorePropNames()} with
|
78
|
* {@link #excludes}
|
79
|
*
|
80
|
* @return
|
81
|
*/
|
82
|
protected Set<String> getMergedExcludes(){
|
83
|
if(mergedExcludes == null) {
|
84
|
mergedExcludes = new HashSet<String>(excludes);
|
85
|
if(getIgnorePropNames() != null){
|
86
|
mergedExcludes.addAll(getIgnorePropNames());
|
87
|
}
|
88
|
}
|
89
|
|
90
|
return mergedExcludes;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
*
|
95
|
* @param json
|
96
|
* @param jsonConfig
|
97
|
* @param fieldName
|
98
|
* @param fieldObject
|
99
|
*/
|
100
|
protected void addJsonElement(JSONObject json, JsonConfig jsonConfig, String fieldName, Object fieldObject) {
|
101
|
if(Hibernate.isInitialized(fieldObject)){
|
102
|
json.element(fieldName, fieldObject, jsonConfig);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
|
107
|
|
108
|
/**
|
109
|
* {@inheritDoc}
|
110
|
*/
|
111
|
@Override
|
112
|
public final JSONObject processBean(Object bean, JsonConfig jsonConfig) {
|
113
|
|
114
|
if(logger.isDebugEnabled()){
|
115
|
logger.debug("processing " + bean.getClass());
|
116
|
}
|
117
|
|
118
|
JSONObject json = new JSONObject();
|
119
|
Collection<?> exclusions = jsonConfig.getMergedExcludes( bean.getClass() );
|
120
|
Set<PropertyDescriptor> props = AbstractBeanInitializer.getProperties(bean, null);
|
121
|
PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
|
122
|
for(PropertyDescriptor prop: props){
|
123
|
String key = prop.getName();
|
124
|
if(getMergedExcludes().contains(key) || exclusions.contains(key)){
|
125
|
if(logger.isDebugEnabled()){
|
126
|
logger.debug("skipping excluded property " + key);
|
127
|
}
|
128
|
continue;
|
129
|
}
|
130
|
|
131
|
try {
|
132
|
// ------ reusing snippet from JSONOnbject._fromBean()
|
133
|
Class<?> type = prop.getPropertyType();
|
134
|
Object value = PropertyUtils.getProperty( bean, key );
|
135
|
|
136
|
if( jsonPropertyFilter != null && jsonPropertyFilter.apply( bean, key, value ) ){
|
137
|
continue;
|
138
|
}
|
139
|
JsonValueProcessor jsonValueProcessor = jsonConfig.findJsonValueProcessor(bean.getClass(), type, key );
|
140
|
if( jsonValueProcessor != null ){
|
141
|
value = jsonValueProcessor.processObjectValue( key, value, jsonConfig );
|
142
|
if( !JsonVerifier.isValidJsonValue( value ) ){
|
143
|
throw new JSONException( "Value is not a valid JSON value. " + value );
|
144
|
}
|
145
|
}
|
146
|
// ----- END of snipped
|
147
|
if(logger.isDebugEnabled()){
|
148
|
logger.debug("processing " + key + " of " + bean.getClass());
|
149
|
}
|
150
|
if(CdmBase.class.isAssignableFrom(type)){
|
151
|
json.element(key, value, jsonConfig);
|
152
|
} else if(Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)){
|
153
|
json.element(key, value, jsonConfig);
|
154
|
} else if(Object.class.isAssignableFrom(type)){
|
155
|
json.element(key, value, jsonConfig);
|
156
|
} else {
|
157
|
json.element(key, value);
|
158
|
}
|
159
|
|
160
|
} catch (IllegalAccessException e) {
|
161
|
logger.error(e.getMessage(), e);
|
162
|
} catch (InvocationTargetException e) {
|
163
|
logger.error(e.getMessage(), e);
|
164
|
} catch (NoSuchMethodException e) {
|
165
|
logger.error(e.getMessage(), e);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
json = processBeanSecondStep((T) bean, json, jsonConfig);
|
170
|
|
171
|
return json;
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* This method is called ate the end of {@link #processBean(Object, JsonConfig)} just before the JSONObject is returned.
|
176
|
* By overriding this method it is possible to to further processing.
|
177
|
* <p>
|
178
|
* <b>See also {@link #getIgnorePropNames()}!</b>
|
179
|
*
|
180
|
* @param bean
|
181
|
* @param json
|
182
|
* @param jsonConfig
|
183
|
* @return
|
184
|
*/
|
185
|
public abstract JSONObject processBeanSecondStep(T bean, JSONObject json, JsonConfig jsonConfig) ;
|
186
|
|
187
|
|
188
|
}
|