missing implementations for the generic controller architeture (works for data portal...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / json / JsonConfigFactoryBean.java
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.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;
26
27 import org.apache.log4j.Logger;
28 import org.springframework.beans.factory.FactoryBean;
29
30 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 import eu.etaxonomy.cdm.remote.json.processor.bean.AbstractCdmBeanProcessor;
32
33 /**
34 *
35 * @author ben.clark
36 * @author a.kohlbecker
37 */
38 public class JsonConfigFactoryBean implements FactoryBean {
39
40 public static final Logger logger = Logger.getLogger(JsonConfigFactoryBean.class);
41
42 private JsonConfig jsonConfig = null;
43
44 private CycleDetectionStrategy cycleDetectionStrategy = CycleDetectionStrategy.LENIENT;
45
46 /**
47 * Default is true, to avoid LayzyLoadingExceptions. See
48 * {@link #setIgnoreJPATransient(boolean)}
49 */
50 private boolean ignoreJPATransient = true;
51
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>();
59
60 public void setCycleDetectionStrategy(CycleDetectionStrategy cycleDetectionStrategy) {
61 this.cycleDetectionStrategy = cycleDetectionStrategy;
62 }
63
64 /**
65 * Default is true, to avoid LayzyLoadingExceptions.
66 * <p>
67 *
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:
75 * <ol>
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:
81 * <pre>
82 @Override
83 public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {
84 json.element("taggedName", getTaggedName(bean), jsonConfig);
85 return json;
86 }
87 * </pre>
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>
91 * </ol>
92 * <strong>please see also http://dev.e-taxonomy.eu/trac/wiki/CdmEntityInitalization</strong>
93 *
94 * @param ignoreJPATransient
95 */
96 @Deprecated
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.");
100 }
101 this.ignoreJPATransient = ignoreJPATransient;
102 }
103
104 public void setJsonBeanProcessors(Map<Class, JsonBeanProcessor> jsonBeanProcessors) {
105 this.jsonBeanProcessors = jsonBeanProcessors;
106 }
107
108 public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter) {
109 this.jsonPropertyFilter = jsonPropertyFilter;
110 }
111
112 public void setJsonBeanProcessorMatcher(JsonBeanProcessorMatcher jsonBeanProcessorMatcher) {
113 this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;
114 }
115
116 public void setJsonValueProcessorMatcher(JsonValueProcessorMatcher jsonValueProcessorMatcher ) {
117 this.jsonValueProcessorMatcher = jsonValueProcessorMatcher;
118 }
119
120 public void setJsonValueProcessors(Map<Class, JsonValueProcessor> jsonValueProcessors) {
121 this.jsonValueProcessors = jsonValueProcessors;
122 }
123
124 public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) {
125 this.ignoreDefaultExcludes = ignoreDefaultExcludes;
126 }
127
128 public void setExcludes(List<String> excludes) {
129 this.excludes = excludes;
130 }
131
132 public void init() {
133 jsonConfig = new JsonConfig();
134
135 jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);
136
137 jsonConfig.setIgnoreJPATransient(ignoreJPATransient);
138
139 jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);
140
141 jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
142
143 jsonConfig.setExcludes(excludes.toArray(new String[]{}));
144
145 jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);
146
147 jsonConfig.setJsonPropertyFilter(jsonPropertyFilter);
148
149 for(Class clazz : jsonBeanProcessors.keySet()) {
150 jsonConfig.registerJsonBeanProcessor(clazz, jsonBeanProcessors.get(clazz));
151 }
152
153
154
155
156 for(Class clazz : jsonValueProcessors.keySet()) {
157 jsonConfig.registerJsonValueProcessor(clazz, jsonValueProcessors.get(clazz));
158 }
159
160
161 }
162
163
164 public Object getObject() throws Exception {
165 if(jsonConfig == null) {
166 init();
167 }
168 return jsonConfig;
169 }
170
171 public Class getObjectType() {
172 return JsonConfig.class;
173 }
174
175 public boolean isSingleton() {
176 return true;
177 }
178
179 }