1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.remote.json;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.beans.factory.FactoryBean;
|
19
|
|
20
|
import net.sf.json.JsonConfig;
|
21
|
import net.sf.json.processors.DefaultValueProcessor;
|
22
|
import net.sf.json.processors.DefaultValueProcessorMatcher;
|
23
|
import net.sf.json.processors.JsonBeanProcessor;
|
24
|
import net.sf.json.processors.JsonBeanProcessorMatcher;
|
25
|
import net.sf.json.processors.JsonValueProcessor;
|
26
|
import net.sf.json.processors.JsonValueProcessorMatcher;
|
27
|
import net.sf.json.util.CycleDetectionStrategy;
|
28
|
import net.sf.json.util.PropertyFilter;
|
29
|
|
30
|
/**
|
31
|
*
|
32
|
* @author ben.clark
|
33
|
* @author a.kohlbecker
|
34
|
*/
|
35
|
public class JsonConfigFactoryBean implements FactoryBean<JsonConfig> {
|
36
|
|
37
|
public static final Logger logger = Logger.getLogger(JsonConfigFactoryBean.class);
|
38
|
|
39
|
private JsonConfig jsonConfig = null;
|
40
|
|
41
|
private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT;
|
42
|
|
43
|
/**
|
44
|
* Default is true, to avoid LayzyLoadingExceptions. See
|
45
|
* {@link #setIgnoreJPATransient(boolean)}
|
46
|
*/
|
47
|
private boolean ignoreJPATransient = true;
|
48
|
|
49
|
private Map<Class<?>,JsonBeanProcessor> jsonBeanProcessors = new HashMap<>();
|
50
|
private PropertyFilter jsonPropertyFilter = null;
|
51
|
private Map<Class<?>,JsonValueProcessor> jsonValueProcessorsByClass = new HashMap<>();
|
52
|
private DefaultValueProcessorMatcher defaultValueProcessorMatcher = null;
|
53
|
private Map<Class<?>, DefaultValueProcessor> defaultValueProcessorMap = new HashMap<>();
|
54
|
private Map<String, JsonValueProcessor> jsonValueProcessorsByProperty = new HashMap<>();
|
55
|
private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = JsonBeanProcessorMatcher.DEFAULT;
|
56
|
private JsonValueProcessorMatcher jsonValueProcessorMatcher = JsonValueProcessorMatcher.DEFAULT;
|
57
|
private boolean ignoreDefaultExcludes = false;
|
58
|
private List<String> excludes = new ArrayList<>();
|
59
|
|
60
|
|
61
|
public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) {
|
62
|
this.cycleDetectionStrategy = cycleDetectionStrategy;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Default is true, to avoid LayzyLoadingExceptions.
|
67
|
* <p>
|
68
|
*
|
69
|
* @deprecated Setting this property to false will cause
|
70
|
* LazyLoadingExceptions and will thus completely break the JSON
|
71
|
* serialization!! <br>
|
72
|
* In the cdm model all getters returning cdm entity or product
|
73
|
* of cdm entities which are not directly returning a
|
74
|
* HibernateProxy are annotated as @Transient. In order to
|
75
|
* serialize these properties you have to do two things:
|
76
|
* <ol>
|
77
|
* <li>Explicitly serialize the property by overriding
|
78
|
* {@link AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, JsonConfig)}
|
79
|
* in the according {AbstractCdmBeanProcessor} implementation.
|
80
|
* If there is no matching {AbstractCdmBeanProcessor}
|
81
|
* implementation you would have to create one. for example:
|
82
|
* <pre>
|
83
|
@Override
|
84
|
public JSONObject processBeanSecondStep(TaxonName bean, JSONObject json, JsonConfig jsonConfig) {
|
85
|
json.element("taggedName", getTaggedName(bean), jsonConfig);
|
86
|
return json;
|
87
|
}
|
88
|
* </pre>
|
89
|
* </li> <li>Provide the service method which is used to
|
90
|
* retrieve the object graph in question with an appropriate
|
91
|
* initialization strategy.</li>
|
92
|
* </ol>
|
93
|
* <strong>please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization</strong>
|
94
|
*
|
95
|
* @param ignoreJPATransient
|
96
|
*/
|
97
|
@Deprecated
|
98
|
public void setIgnoreJPATransient(boolean ignoreJPATransient) {
|
99
|
if(!ignoreJPATransient){
|
100
|
logger.error("ignoreJPATransient must not be set to false. Doing so will cause LazyLoadingExceptions and will thus completely break the JSON serialization.");
|
101
|
}
|
102
|
this.ignoreJPATransient = ignoreJPATransient;
|
103
|
}
|
104
|
|
105
|
public void setJsonBeanProcessors(Map<Class<?>, JsonBeanProcessor> jsonBeanProcessors) {
|
106
|
this.jsonBeanProcessors = jsonBeanProcessors;
|
107
|
}
|
108
|
|
109
|
public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) {
|
110
|
this.jsonPropertyFilter = jsonPropertyFilter;
|
111
|
}
|
112
|
|
113
|
public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) {
|
114
|
this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;
|
115
|
}
|
116
|
|
117
|
public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) {
|
118
|
this.jsonValueProcessorMatcher = jsonValueProcessorMatcher;
|
119
|
}
|
120
|
|
121
|
public void setDefaultValueProcessorMap(Map<Class<?>, DefaultValueProcessor> defaultValueProcessorMap){
|
122
|
this.defaultValueProcessorMap = defaultValueProcessorMap;
|
123
|
}
|
124
|
|
125
|
public void setDefaultValueProcessorMatcher(DefaultValueProcessorMatcher defaultValueProcessorMatcher){
|
126
|
this.defaultValueProcessorMatcher = defaultValueProcessorMatcher;
|
127
|
}
|
128
|
|
129
|
public void setJsonValueProcessorsByClass(Map<Class<?>, JsonValueProcessor> jsonValueProcessors) {
|
130
|
this.jsonValueProcessorsByClass = jsonValueProcessors;
|
131
|
}
|
132
|
|
133
|
public void setJsonValueProcessorsByProperty(Map<String, JsonValueProcessor> jsonValueProcessors) {
|
134
|
this.jsonValueProcessorsByProperty = jsonValueProcessors;
|
135
|
}
|
136
|
|
137
|
public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) {
|
138
|
this.ignoreDefaultExcludes = ignoreDefaultExcludes;
|
139
|
}
|
140
|
|
141
|
public void setExcludes(List<String> excludes) {
|
142
|
this.excludes = excludes;
|
143
|
}
|
144
|
|
145
|
public void init() {
|
146
|
jsonConfig = new JsonConfig();
|
147
|
|
148
|
jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);
|
149
|
|
150
|
jsonConfig.setIgnoreJPATransient(ignoreJPATransient);
|
151
|
|
152
|
jsonConfig.setDefaultValueProcessorMatcher(defaultValueProcessorMatcher);
|
153
|
|
154
|
jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);
|
155
|
|
156
|
jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
|
157
|
|
158
|
jsonConfig.setExcludes(excludes.toArray(new String[]{}));
|
159
|
|
160
|
jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);
|
161
|
|
162
|
jsonConfig.setJsonPropertyFilter(jsonPropertyFilter);
|
163
|
|
164
|
for(Class<?> clazz : jsonBeanProcessors.keySet()) {
|
165
|
jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz));
|
166
|
}
|
167
|
|
168
|
for(Class<?> clazz : defaultValueProcessorMap.keySet()) {
|
169
|
jsonConfig.registerDefaultValueProcessor(clazz, defaultValueProcessorMap.get(clazz));
|
170
|
}
|
171
|
|
172
|
for(Class<?> clazz : jsonValueProcessorsByClass.keySet()) {
|
173
|
jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessorsByClass.get(clazz));
|
174
|
}
|
175
|
|
176
|
for(String prop : jsonValueProcessorsByProperty.keySet()) {
|
177
|
jsonConfig.registerJsonValueProcessor(prop, jsonValueProcessorsByClass.get(prop));
|
178
|
}
|
179
|
}
|
180
|
|
181
|
|
182
|
@Override
|
183
|
public JsonConfig getObject() throws Exception {
|
184
|
if(jsonConfig == null) {
|
185
|
init();
|
186
|
}
|
187
|
return jsonConfig;
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
public Class<?> getObjectType() {
|
192
|
return JsonConfig.class;
|
193
|
}
|
194
|
|
195
|
@Override
|
196
|
public boolean isSingleton() {
|
197
|
return true;
|
198
|
}
|
199
|
|
200
|
}
|