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