3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.remote
.json
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import net
.sf
.json
.JSONObject
;
19 import net
.sf
.json
.JsonConfig
;
20 import net
.sf
.json
.processors
.JsonBeanProcessor
;
21 import net
.sf
.json
.processors
.JsonBeanProcessorMatcher
;
22 import net
.sf
.json
.processors
.JsonValueProcessor
;
23 import net
.sf
.json
.processors
.JsonValueProcessorMatcher
;
24 import net
.sf
.json
.util
.CycleDetectionStrategy
;
25 import net
.sf
.json
.util
.PropertyFilter
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.springframework
.beans
.factory
.FactoryBean
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.cdm
.remote
.json
.processor
.bean
.AbstractCdmBeanProcessor
;
36 * @author a.kohlbecker
38 public class JsonConfigFactoryBean
implements FactoryBean
{
40 public static final Logger logger
= Logger
.getLogger(JsonConfigFactoryBean
.class);
42 private JsonConfig jsonConfig
= null;
44 private CycleDetectionStrategy cycleDetectionStrategy
= CycleDetectionStrategy
.LENIENT
;
47 * Default is true, to avoid LayzyLoadingExceptions. See
48 * {@link #setIgnoreJPATransient(boolean)}
50 private boolean ignoreJPATransient
= true;
52 private Map
<Class
,JsonBeanProcessor
> jsonBeanProcessors
= new HashMap
<Class
,JsonBeanProcessor
>();
53 private PropertyFilter jsonPropertyFilter
= null;
54 private Map
<Class
,JsonValueProcessor
> jsonValueProcessors
= new HashMap
<Class
,JsonValueProcessor
>();
55 private JsonBeanProcessorMatcher jsonBeanProcessorMatcher
= JsonBeanProcessorMatcher
.DEFAULT
;
56 private JsonValueProcessorMatcher jsonValueProcessorMatcher
= JsonValueProcessorMatcher
.DEFAULT
;
57 private boolean ignoreDefaultExcludes
= false;
58 private List
<String
> excludes
= new ArrayList
<String
>();
60 public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy
) {
61 this.cycleDetectionStrategy
= cycleDetectionStrategy
;
65 * Default is true, to avoid LayzyLoadingExceptions.
68 * @deprecated Setting this property to false will cause
69 * LazyLoadingExceptions and will thus completely break the JSON
70 * serialization!! <br>
71 * In the cdm model all getters returning cdm entity or product
72 * of cdm entities which are not directly returning a
73 * HibernateProxy are annotated as @Transient. In order to
74 * serialize these properties you have to do two things:
76 * <li>Explicitly serialize the property by overriding
77 * {@link AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, JsonConfig)}
78 * in the according {AbstractCdmBeanProcessor} implementation.
79 * If there is no matching {AbstractCdmBeanProcessor}
80 * implementation you would have to create one. for example:
83 public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {
84 json.element("taggedName", getTaggedName(bean), jsonConfig);
88 * </li> <li>Provide the service method which is used to
89 * retrieve the object graph in question with an appropriate
90 * initialization strategy.</li>
92 * <strong>please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization</strong>
94 * @param ignoreJPATransient
97 public void setIgnoreJPATransient(boolean ignoreJPATransient
) {
98 if(!ignoreJPATransient
){
99 logger
.error("ignoreJPATransient must not be set to false. Doing so will cause LazyLoadingExceptions and will thus completely break the JSON serialization.");
101 this.ignoreJPATransient
= ignoreJPATransient
;
104 public void setJsonBeanProcessors(Map
<Class
, JsonBeanProcessor
> jsonBeanProcessors
) {
105 this.jsonBeanProcessors
= jsonBeanProcessors
;
108 public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter
) {
109 this.jsonPropertyFilter
= jsonPropertyFilter
;
112 public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher
) {
113 this.jsonBeanProcessorMatcher
= jsonBeanProcessorMatcher
;
116 public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher
) {
117 this.jsonValueProcessorMatcher
= jsonValueProcessorMatcher
;
120 public void setJsonValueProcessors(Map
<Class
, JsonValueProcessor
> jsonValueProcessors
) {
121 this.jsonValueProcessors
= jsonValueProcessors
;
124 public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes
) {
125 this.ignoreDefaultExcludes
= ignoreDefaultExcludes
;
128 public void setExcludes(List
<String
> excludes
) {
129 this.excludes
= excludes
;
133 jsonConfig
= new JsonConfig();
135 jsonConfig
.setCycleDetectionStrategy(cycleDetectionStrategy
);
137 jsonConfig
.setIgnoreJPATransient(ignoreJPATransient
);
139 jsonConfig
.setJsonValueProcessorMatcher(jsonValueProcessorMatcher
);
141 jsonConfig
.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher
);
143 jsonConfig
.setExcludes(excludes
.toArray(new String
[]{}));
145 jsonConfig
.setIgnoreDefaultExcludes(ignoreDefaultExcludes
);
147 jsonConfig
.setJsonPropertyFilter(jsonPropertyFilter
);
149 for(Class clazz
: jsonBeanProcessors
.keySet()) {
150 jsonConfig
.registerJsonBeanProcessor(clazz
, jsonBeanProcessors
.get(clazz
));
156 for(Class clazz
: jsonValueProcessors
.keySet()) {
157 jsonConfig
.registerJsonValueProcessor(clazz
, jsonValueProcessors
.get(clazz
));
164 public Object
getObject() throws Exception
{
165 if(jsonConfig
== null) {
171 public Class
getObjectType() {
172 return JsonConfig
.class;
175 public boolean isSingleton() {