Project

General

Profile

Download (5.99 KB) Statistics
| Branch: | Tag: | Revision:
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 eu.etaxonomy.cdm.remote.json.processor.bean.AbstractCdmBeanProcessor;
21
import net.sf.json.JsonConfig;
22
import net.sf.json.processors.JsonBeanProcessor;
23
import net.sf.json.processors.JsonBeanProcessorMatcher;
24
import net.sf.json.processors.JsonValueProcessor;
25
import net.sf.json.processors.JsonValueProcessorMatcher;
26
import net.sf.json.util.CycleDetectionStrategy;
27
import net.sf.json.util.PropertyFilter;
28

    
29
/**
30
 *
31
 * @author ben.clark
32
 * @author a.kohlbecker
33
 */
34
public class JsonConfigFactoryBean implements FactoryBean<JsonConfig> {
35

    
36
	public static final Logger logger = Logger.getLogger(JsonConfigFactoryBean.class);
37

    
38
	private JsonConfig jsonConfig = null;
39

    
40
	private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT;
41

    
42
	/**
43
	 * Default is true, to avoid LayzyLoadingExceptions. See
44
	 * {@link #setIgnoreJPATransient(boolean)}
45
	 */
46
	private boolean ignoreJPATransient = true;
47

    
48
	private Map<Class<?>,JsonBeanProcessor> jsonBeanProcessors = new HashMap<>();
49
	private PropertyFilter jsonPropertyFilter = null;
50
	private Map<Class<?>,JsonValueProcessor> jsonValueProcessors = new HashMap<>();
51
	private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = JsonBeanProcessorMatcher.DEFAULT;
52
	private JsonValueProcessorMatcher jsonValueProcessorMatcher = JsonValueProcessorMatcher.DEFAULT;
53
	private boolean ignoreDefaultExcludes = false;
54
	private List<String> excludes = new ArrayList<>();
55

    
56
	public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) {
57
		this.cycleDetectionStrategy = cycleDetectionStrategy;
58
	}
59

    
60
	/**
61
	 * Default is true, to avoid LayzyLoadingExceptions.
62
	 * <p>
63
	 *
64
	 * @deprecated Setting this property to false will cause
65
	 *             LazyLoadingExceptions and will thus completely break the JSON
66
	 *             serialization!! <br>
67
	 *             In the cdm model all getters returning cdm entity or product
68
	 *             of cdm entities which are not directly returning a
69
	 *             HibernateProxy are annotated as @Transient. In order to
70
	 *             serialize these properties you have to do two things:
71
	 *             <ol>
72
	 *             <li>Explicitly serialize the property by overriding
73
	 *             {@link AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, JsonConfig)}
74
	 *             in the according {AbstractCdmBeanProcessor} implementation.
75
	 *             If there is no matching {AbstractCdmBeanProcessor}
76
	 *             implementation you would have to create one. for example:
77
	 *             <pre>
78
	 	@Override
79
		public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {
80
		  json.element("taggedName", getTaggedName(bean), jsonConfig);
81
		  return json;
82
		}
83
	 *             </pre>
84
	 *             </li> <li>Provide the service method which is used to
85
	 *             retrieve the object graph in question with an appropriate
86
	 *             initialization strategy.</li>
87
	 *             </ol>
88
	 * <strong>please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization</strong>
89
	 *
90
	 * @param ignoreJPATransient
91
	 */
92
	@Deprecated
93
	public void setIgnoreJPATransient(boolean ignoreJPATransient) {
94
		if(!ignoreJPATransient){
95
			logger.error("ignoreJPATransient must not be set to false. Doing so will cause LazyLoadingExceptions and will thus completely break the JSON serialization.");
96
		}
97
		this.ignoreJPATransient = ignoreJPATransient;
98
	}
99

    
100
	public void setJsonBeanProcessors(Map<Class<?>, JsonBeanProcessor> jsonBeanProcessors) {
101
		this.jsonBeanProcessors = jsonBeanProcessors;
102
	}
103

    
104
	public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) {
105
		this.jsonPropertyFilter = jsonPropertyFilter;
106
	}
107

    
108
	public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) {
109
		this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;
110
	}
111

    
112
	public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) {
113
		this.jsonValueProcessorMatcher = jsonValueProcessorMatcher;
114
	}
115

    
116
	public void setJsonValueProcessors(Map<Class<?>, JsonValueProcessor> jsonValueProcessors) {
117
		this.jsonValueProcessors = jsonValueProcessors;
118
	}
119

    
120
	public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) {
121
		this.ignoreDefaultExcludes = ignoreDefaultExcludes;
122
	}
123

    
124
	public void setExcludes(List<String> excludes) {
125
		this.excludes = excludes;
126
	}
127

    
128
	public void init() {
129
		jsonConfig = new JsonConfig();
130

    
131
		jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);
132

    
133
		jsonConfig.setIgnoreJPATransient(ignoreJPATransient);
134

    
135
		jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);
136

    
137
		jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
138

    
139
		jsonConfig.setExcludes(excludes.toArray(new String[]{}));
140

    
141
		jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);
142

    
143
		jsonConfig.setJsonPropertyFilter(jsonPropertyFilter);
144

    
145
		for(Class<?> clazz : jsonBeanProcessors.keySet()) {
146
			jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz));
147
		}
148

    
149

    
150
		for(Class<?> clazz : jsonValueProcessors.keySet()) {
151
		    jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessors.get(clazz));
152
		}
153
	}
154

    
155

    
156
	@Override
157
    public JsonConfig getObject() throws Exception {
158
		if(jsonConfig == null) {
159
			init();
160
		}
161
		return jsonConfig;
162
	}
163

    
164
    @Override
165
    public Class<?> getObjectType() {
166
		return JsonConfig.class;
167
	}
168

    
169
	@Override
170
    public boolean isSingleton() {
171
		return true;
172
	}
173

    
174
}
(1-1/2)