Project

General

Profile

Download (6.49 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 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
package eu.etaxonomy.cdm.remote.json.processor.bean;
10

    
11
import java.beans.PropertyDescriptor;
12
import java.lang.reflect.InvocationTargetException;
13
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.commons.beanutils.PropertyUtils;
20
import org.apache.log4j.Logger;
21
import org.hibernate.Hibernate;
22

    
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
25
import net.sf.json.JSONException;
26
import net.sf.json.JSONObject;
27
import net.sf.json.JsonConfig;
28
import net.sf.json.processors.JsonBeanProcessor;
29
import net.sf.json.processors.JsonValueProcessor;
30
import net.sf.json.processors.JsonVerifier;
31
import net.sf.json.util.PropertyFilter;
32

    
33
/**
34
 * @author a.kohlbecker
35
 * @since 30.03.2009
36
 *
37
 */
38
public abstract class AbstractBeanProcessor<T extends Object> implements JsonBeanProcessor{
39

    
40
    public static final Logger logger = Logger.getLogger(AbstractBeanProcessor.class);
41

    
42
    private Set<String> excludes = new HashSet<String>();
43

    
44
    private Set<String> mergedExcludes = null;
45

    
46
    public Set<String> getExcludes() {
47
        return excludes;
48
    }
49

    
50
    /**
51
     * This method allows supplying a List of property names to be ignored
52
     * during the serialization to JSON. The <code>excludes</code> will be
53
     * merged with the property names configured by subclasses which override
54
     * {@link {@link #getIgnorePropNames()}.
55
     *
56
     * @param excludes
57
     */
58
    public void setExcludes(Set<String> excludes) {
59
        this.excludes = excludes;
60
    }
61

    
62
    /**
63
     * Implementations of this abstract class may override this method in order
64
     * to supply a List of property names to be ignored in
65
     * {@link #processBean(Object, JsonConfig)}. This feature generally is used
66
     * when {@link #processBeanSecondStep(CdmBase, JSONObject, JsonConfig)} is
67
     * implemented. such that this method is responsible of serializing this
68
     * property.
69
     *
70
     * @return a List of property names.
71
     */
72
    public abstract List<String> getIgnorePropNames();
73

    
74
    /**
75
     * merges and returns {@link {@link #getIgnorePropNames()} with
76
     * {@link #excludes}
77
     *
78
     * @return
79
     */
80
    protected Set<String> getMergedExcludes(){
81
        if(mergedExcludes  == null) {
82
            mergedExcludes = new HashSet<String>(excludes);
83
            if(getIgnorePropNames() != null){
84
                mergedExcludes.addAll(getIgnorePropNames());
85
            }
86
        }
87

    
88
        return mergedExcludes;
89
    }
90

    
91
    /**
92
     *
93
     * @param json
94
     * @param jsonConfig
95
     * @param fieldName
96
     * @param fieldObject
97
     */
98
    protected void addJsonElement(JSONObject json, JsonConfig jsonConfig, String fieldName,	Object fieldObject) {
99
        if(Hibernate.isInitialized(fieldObject)){
100
            json.element(fieldName, fieldObject, jsonConfig);
101
        }
102
    }
103

    
104

    
105

    
106
    /**
107
     * {@inheritDoc}
108
     */
109
    @Override
110
    public final JSONObject processBean(Object bean, JsonConfig jsonConfig) {
111

    
112
        if(logger.isDebugEnabled()){
113
            logger.debug("processing " + bean.getClass());
114
        }
115

    
116
        JSONObject json =  new JSONObject();
117
        Collection<?> exclusions = jsonConfig.getMergedExcludes( bean.getClass() );
118
        Set<PropertyDescriptor> props = AbstractBeanInitializer.getProperties(bean, null);
119
        PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
120
        for(PropertyDescriptor prop: props){
121
            String key = prop.getName();
122
            if(getMergedExcludes().contains(key) || exclusions.contains(key)){
123
                if(logger.isDebugEnabled()){
124
                    logger.debug("skipping excluded property " + key);
125
                }
126
                continue;
127
            }
128

    
129
            try {
130
                // ------ reusing snippet from JSONOnbject._fromBean()
131
                Class<?> type = prop.getPropertyType();
132
                Object value = PropertyUtils.getProperty( bean, key );
133

    
134
                if( jsonPropertyFilter != null && jsonPropertyFilter.apply( bean, key, value ) ){
135
                   continue;
136
                }
137
                JsonValueProcessor jsonValueProcessor = jsonConfig.findJsonValueProcessor(bean.getClass(), type, key );
138
                if( jsonValueProcessor != null ){
139
                   value = jsonValueProcessor.processObjectValue( key, value, jsonConfig );
140
                   if( !JsonVerifier.isValidJsonValue( value ) ){
141
                      throw new JSONException( "Value is not a valid JSON value. " + value );
142
                   }
143
                }
144
                // ----- END of snipped
145
                if(logger.isDebugEnabled()){
146
                    logger.debug("processing " + key + " of " + bean.getClass());
147
                }
148
                if(CdmBase.class.isAssignableFrom(type)){
149
                    json.element(key, value, jsonConfig);
150
                } else if(Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)){
151
                    json.element(key, value, jsonConfig);
152
                }  else if(Object.class.isAssignableFrom(type)){
153
                    json.element(key, value, jsonConfig);
154
                } else {
155
                    json.element(key, value);
156
                }
157

    
158
            } catch (IllegalAccessException e) {
159
                logger.error(e.getMessage(), e);
160
            } catch (InvocationTargetException e) {
161
                logger.error(e.getMessage(), e);
162
            } catch (NoSuchMethodException e) {
163
                logger.error(e.getMessage(), e);
164
            }
165
        }
166

    
167
        json = processBeanSecondStep((T) bean, json, jsonConfig);
168

    
169
        return json;
170
    }
171

    
172
    /**
173
     * This method is called at the end of {@link #processBean(Object, JsonConfig)} just before the JSONObject is returned.
174
     * By overriding this method it is possible to do further processing.
175
     * <p>
176
     * <b>See also {@link #getIgnorePropNames()}!</b>
177
     *
178
     * @param bean
179
     * @param json
180
     * @param jsonConfig
181
     * @return
182
     */
183
    public abstract JSONObject processBeanSecondStep(T bean, JSONObject json, JsonConfig jsonConfig) ;
184

    
185

    
186
}
(1-1/25)