Project

General

Profile

Download (5.97 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.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
22
import org.hibernate.Hibernate;
23

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

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

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

    
42
    private Set<String> excludes = new HashSet<>();
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<>(excludes);
83
            if(getIgnorePropNames() != null){
84
                mergedExcludes.addAll(getIgnorePropNames());
85
            }
86
        }
87

    
88
        return mergedExcludes;
89
    }
90

    
91
    protected void addJsonElement(JSONObject json, JsonConfig jsonConfig, String fieldName,	Object fieldObject) {
92
        if(Hibernate.isInitialized(fieldObject)){
93
            json.element(fieldName, fieldObject, jsonConfig);
94
        }
95
    }
96

    
97
    @Override
98
    public final JSONObject processBean(Object bean, JsonConfig jsonConfig) {
99

    
100
        if(logger.isDebugEnabled()){
101
            logger.debug("processing " + bean.getClass());
102
        }
103

    
104
        JSONObject json =  new JSONObject();
105
        Collection<?> exclusions = jsonConfig.getMergedExcludes( bean.getClass() );
106
        Set<PropertyDescriptor> props = AbstractBeanInitializer.getProperties(bean, null);
107
        PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
108
        for(PropertyDescriptor prop: props){
109
            String key = prop.getName();
110
            if(getMergedExcludes().contains(key) || exclusions.contains(key)){
111
                if(logger.isDebugEnabled()){
112
                    logger.debug("skipping excluded property " + key);
113
                }
114
                continue;
115
            }
116

    
117
            try {
118
                // ------ reusing snippet from JSONObject._fromBean()
119
                Class<?> type = prop.getPropertyType();
120
                Object value = PropertyUtils.getProperty( bean, key );
121

    
122
                if( jsonPropertyFilter != null && jsonPropertyFilter.apply( bean, key, value ) ){
123
                   continue;
124
                }
125
                JsonValueProcessor jsonValueProcessor = jsonConfig.findJsonValueProcessor(bean.getClass(), type, key );
126
                if( jsonValueProcessor != null ){
127
                   value = jsonValueProcessor.processObjectValue( key, value, jsonConfig );
128
                   if( !JsonVerifier.isValidJsonValue( value ) ){
129
                      throw new JSONException( "Value is not a valid JSON value. " + value );
130
                   }
131
                }
132
                // ----- END of snipped
133
                if(logger.isDebugEnabled()){
134
                    logger.debug("processing " + key + " of " + bean.getClass());
135
                }
136
                if(CdmBase.class.isAssignableFrom(type)){
137
                    json.element(key, value, jsonConfig);
138
                } else if(Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)){
139
                    json.element(key, value, jsonConfig);
140
                }  else if(Object.class.isAssignableFrom(type)){
141
                    json.element(key, value, jsonConfig);
142
                } else {
143
                    json.element(key, value);
144
                }
145

    
146
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
147
                logger.error(e.getMessage(), e);
148
            }
149
        }
150

    
151
        json = processBeanSecondStep((T) bean, json, jsonConfig);
152

    
153
        return json;
154
    }
155

    
156
    /**
157
     * This method is called at the end of {@link #processBean(Object, JsonConfig)} just before the JSONObject is returned.
158
     * By overriding this method it is possible to do further processing.
159
     * <p>
160
     * <b>See also {@link #getIgnorePropNames()}!</b>
161
     */
162
    public abstract JSONObject processBeanSecondStep(T bean, JSONObject json, JsonConfig jsonConfig) ;
163
}
(1-1/31)