X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/cd7fb617d6f3db9212634e2cc1b43b0bdb8091d7..cbbeb4fe648927e2a6f28a7079d71b6d8e849f0f:/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/JsonConfigFactoryBean.java diff --git a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/JsonConfigFactoryBean.java b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/JsonConfigFactoryBean.java index 63c21b31d5..76ff54e69b 100644 --- a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/JsonConfigFactoryBean.java +++ b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/JsonConfigFactoryBean.java @@ -1,3 +1,4 @@ +// $Id$ /** * Copyright (C) 2007 EDIT * European Distributed Institute of Taxonomy @@ -14,6 +15,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonBeanProcessor; import net.sf.json.processors.JsonBeanProcessorMatcher; @@ -22,32 +24,80 @@ import net.sf.json.processors.JsonValueProcessorMatcher; import net.sf.json.util.CycleDetectionStrategy; import net.sf.json.util.PropertyFilter; +import org.apache.log4j.Logger; import org.springframework.beans.factory.FactoryBean; +import eu.etaxonomy.cdm.model.name.TaxonNameBase; +import eu.etaxonomy.cdm.remote.json.processor.bean.AbstractCdmBeanProcessor; + /** * * @author ben.clark * @author a.kohlbecker */ public class JsonConfigFactoryBean implements FactoryBean { + + public static final Logger logger = Logger.getLogger(JsonConfigFactoryBean.class); private JsonConfig jsonConfig = null; private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT; - private boolean ignoreJPATransient = false; + /** + * Default is true, to avoid LayzyLoadingExceptions. See + * {@link #setIgnoreJPATransient(boolean)} + */ + private boolean ignoreJPATransient = true; private Map jsonBeanProcessors = new HashMap(); - private List jsonPropertyFilters = new ArrayList(); - private List jsonBeanProcessorMatchers = new ArrayList(); + private PropertyFilter jsonPropertyFilter = null; private Map jsonValueProcessors = new HashMap(); + private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = JsonBeanProcessorMatcher.DEFAULT; private JsonValueProcessorMatcher jsonValueProcessorMatcher = JsonValueProcessorMatcher.DEFAULT; + private boolean ignoreDefaultExcludes = false; + private List excludes = new ArrayList(); public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) { this.cycleDetectionStrategy = cycleDetectionStrategy; } + /** + * Default is true, to avoid LayzyLoadingExceptions. + *

+ * + * @deprecated Setting this property to false will cause + * LazyLoadingExceptions and will thus completely break the JSON + * serialization!!
+ * In the cdm model all getters returning cdm entity or product + * of cdm entities which are not directly returning a + * HibernateProxy are annotated as @Transient. In order to + * serialize these properties you have to do two things: + *

    + *
  1. Explicitly serialize the property by overriding + * {@link AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, JsonConfig)} + * in the according {AbstractCdmBeanProcessor} implementation. + * If there is no matching {AbstractCdmBeanProcessor} + * implementation you would have to create one. for example: + *
    +	 	@Override
    +		public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {
    +		  json.element("taggedName", getTaggedName(bean), jsonConfig);
    +		  return json;
    +		}
    +	 *             
    + *
  2. Provide the service method which is used to + * retrieve the object graph in question with an appropriate + * initialization strategy.
  3. + *
+ * please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization + * + * @param ignoreJPATransient + */ + @Deprecated public void setIgnoreJPATransient(boolean ignoreJPATransient) { + if(!ignoreJPATransient){ + logger.error("ignoreJPATransient must not be set to false. Doing so will cause LazyLoadingExceptions and will thus completely break the JSON serialization."); + } this.ignoreJPATransient = ignoreJPATransient; } @@ -55,12 +105,12 @@ public class JsonConfigFactoryBean implements FactoryBean { this.jsonBeanProcessors = jsonBeanProcessors; } - public void setJsonPropertyFilters(List jsonPropertyFilters) { - this.jsonPropertyFilters = jsonPropertyFilters; + public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) { + this.jsonPropertyFilter = jsonPropertyFilter; } - public void setJsonBeanProcessorMatchers(List jsonBeanProcessorMatchers) { - this.jsonBeanProcessorMatchers = jsonBeanProcessorMatchers; + public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) { + this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher; } public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) { @@ -71,6 +121,14 @@ public class JsonConfigFactoryBean implements FactoryBean { this.jsonValueProcessors = jsonValueProcessors; } + public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) { + this.ignoreDefaultExcludes = ignoreDefaultExcludes; + } + + public void setExcludes(List excludes) { + this.excludes = excludes; + } + public void init() { jsonConfig = new JsonConfig(); @@ -79,18 +137,21 @@ public class JsonConfigFactoryBean implements FactoryBean { jsonConfig.setIgnoreJPATransient(ignoreJPATransient); jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher); + + jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher); + + jsonConfig.setExcludes(excludes.toArray(new String[]{})); + jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes); + + jsonConfig.setJsonPropertyFilter(jsonPropertyFilter); + for(Class clazz : jsonBeanProcessors.keySet()) { jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz)); } - for(PropertyFilter propertyFilter : jsonPropertyFilters) { - jsonConfig.setJsonPropertyFilter(propertyFilter); - } + - for(JsonBeanProcessorMatcher jsonBeanProcessorMatcher : jsonBeanProcessorMatchers) { - jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher); - } for(Class clazz : jsonValueProcessors.keySet()) { jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessors.get(clazz));