+// $Id$\r
/**\r
* Copyright (C) 2007 EDIT\r
* European Distributed Institute of Taxonomy \r
import java.util.List;\r
import java.util.Map;\r
\r
+import net.sf.json.JSONObject;\r
import net.sf.json.JsonConfig;\r
import net.sf.json.processors.JsonBeanProcessor;\r
import net.sf.json.processors.JsonBeanProcessorMatcher;\r
import net.sf.json.util.CycleDetectionStrategy;\r
import net.sf.json.util.PropertyFilter;\r
\r
+import org.apache.log4j.Logger;\r
import org.springframework.beans.factory.FactoryBean;\r
\r
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;\r
+import eu.etaxonomy.cdm.remote.json.processor.bean.AbstractCdmBeanProcessor;\r
+\r
/**\r
* \r
* @author ben.clark\r
* @author a.kohlbecker\r
*/\r
public class JsonConfigFactoryBean implements FactoryBean {\r
+ \r
+ public static final Logger logger = Logger.getLogger(JsonConfigFactoryBean.class);\r
\r
private JsonConfig jsonConfig = null;\r
\r
private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT;\r
\r
- private boolean ignoreJPATransient = false;\r
+ /**\r
+ * Default is true, to avoid LayzyLoadingExceptions. See\r
+ * {@link #setIgnoreJPATransient(boolean)}\r
+ */\r
+ private boolean ignoreJPATransient = true;\r
\r
private Map<Class,JsonBeanProcessor> jsonBeanProcessors = new HashMap<Class,JsonBeanProcessor>();\r
- private List<PropertyFilter> jsonPropertyFilters = new ArrayList<PropertyFilter>();\r
- private List<JsonBeanProcessorMatcher> jsonBeanProcessorMatchers = new ArrayList<JsonBeanProcessorMatcher>();\r
+ private PropertyFilter jsonPropertyFilter = null;\r
private Map<Class,JsonValueProcessor> jsonValueProcessors = new HashMap<Class,JsonValueProcessor>();\r
+ private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = JsonBeanProcessorMatcher.DEFAULT;\r
private JsonValueProcessorMatcher jsonValueProcessorMatcher = JsonValueProcessorMatcher.DEFAULT;\r
+ private boolean ignoreDefaultExcludes = false;\r
+ private List<String> excludes = new ArrayList<String>();\r
\r
public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) {\r
this.cycleDetectionStrategy = cycleDetectionStrategy;\r
}\r
\r
+ /**\r
+ * Default is true, to avoid LayzyLoadingExceptions.\r
+ * <p>\r
+ * \r
+ * @deprecated Setting this property to false will cause\r
+ * LazyLoadingExceptions and will thus completely break the JSON\r
+ * serialization!! <br>\r
+ * In the cdm model all getters returning cdm entity or product\r
+ * of cdm entities which are not directly returning a\r
+ * HibernateProxy are annotated as @Transient. In order to\r
+ * serialize these properties you have to do two things:\r
+ * <ol>\r
+ * <li>Explicitly serialize the property by overriding\r
+ * {@link AbstractCdmBeanProcessor#processBeanSecondStep(eu.etaxonomy.cdm.model.common.CdmBase, net.sf.json.JSONObject, JsonConfig)}\r
+ * in the according {AbstractCdmBeanProcessor} implementation.\r
+ * If there is no matching {AbstractCdmBeanProcessor}\r
+ * implementation you would have to create one. for example:\r
+ * <pre>\r
+ @Override\r
+ public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {\r
+ json.element("taggedName", getTaggedName(bean), jsonConfig);\r
+ return json;\r
+ }\r
+ * </pre>\r
+ * </li> <li>Provide the service method which is used to\r
+ * retrieve the object graph in question with an appropriate\r
+ * initialization strategy.</li>\r
+ * </ol>\r
+ * <strong>please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization</strong>\r
+ * \r
+ * @param ignoreJPATransient\r
+ */\r
+ @Deprecated\r
public void setIgnoreJPATransient(boolean ignoreJPATransient) {\r
+ if(!ignoreJPATransient){\r
+ logger.error("ignoreJPATransient must not be set to false. Doing so will cause LazyLoadingExceptions and will thus completely break the JSON serialization.");\r
+ }\r
this.ignoreJPATransient = ignoreJPATransient;\r
}\r
\r
this.jsonBeanProcessors = jsonBeanProcessors;\r
}\r
\r
- public void setJsonPropertyFilters(List<PropertyFilter> jsonPropertyFilters) {\r
- this.jsonPropertyFilters = jsonPropertyFilters;\r
+ public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) {\r
+ this.jsonPropertyFilter = jsonPropertyFilter;\r
}\r
\r
- public void setJsonBeanProcessorMatchers(List<JsonBeanProcessorMatcher> jsonBeanProcessorMatchers) {\r
- this.jsonBeanProcessorMatchers = jsonBeanProcessorMatchers;\r
+ public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) {\r
+ this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;\r
}\r
\r
public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) {\r
this.jsonValueProcessors = jsonValueProcessors;\r
}\r
\r
+ public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) {\r
+ this.ignoreDefaultExcludes = ignoreDefaultExcludes;\r
+ }\r
+\r
+ public void setExcludes(List<String> excludes) {\r
+ this.excludes = excludes;\r
+ }\r
+\r
public void init() {\r
jsonConfig = new JsonConfig();\r
\r
jsonConfig.setIgnoreJPATransient(ignoreJPATransient);\r
\r
jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);\r
+\r
+ jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);\r
+ \r
+ jsonConfig.setExcludes(excludes.toArray(new String[]{}));\r
\r
+ jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);\r
+ \r
+ jsonConfig.setJsonPropertyFilter(jsonPropertyFilter);\r
+\r
for(Class clazz : jsonBeanProcessors.keySet()) {\r
jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz));\r
}\r
\r
- for(PropertyFilter propertyFilter : jsonPropertyFilters) {\r
- jsonConfig.setJsonPropertyFilter(propertyFilter);\r
- }\r
+ \r
\r
- for(JsonBeanProcessorMatcher jsonBeanProcessorMatcher : jsonBeanProcessorMatchers) {\r
- jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);\r
- }\r
\r
for(Class clazz : jsonValueProcessors.keySet()) {\r
jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessors.get(clazz)); \r