Project

General

Profile

Download (2.55 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id: InitializedHibernatePropertyFilter.java 5473 2009-03-25 13:42:07Z a.kohlbecker $
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.util;
12

    
13
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import net.sf.json.util.PropertyFilter;
19

    
20
import org.apache.log4j.Logger;
21

    
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23

    
24
public class CardinalityPropertyFilter implements PropertyFilter {
25

    
26
    private static final Logger logger = Logger.getLogger(CardinalityPropertyFilter.class);
27

    
28
    boolean includeToOneRelations = true;
29

    
30
    boolean includeToManyRelations = true;
31

    
32
    Set<String> exceptions = new HashSet<String>();
33

    
34
    public void setExceptions(Set<String> exceptions) {
35
        this.exceptions = exceptions;
36
    }
37

    
38
    public void setIncludeToOneRelations(boolean includeToOneRelations) {
39
        this.includeToOneRelations = includeToOneRelations;
40
    }
41

    
42
    public void setIncludeToManyRelations(boolean includeToManyRelations) {
43
        this.includeToManyRelations = includeToManyRelations;
44
    }
45

    
46
    /* (non-Javadoc)
47
     * @see net.sf.json.util.PropertyFilter#apply(java.lang.Object, java.lang.String, java.lang.Object)
48
     */
49
    public boolean apply(Object source, String name, Object value) {
50
        Class<?> valueType;
51

    
52
        if(value == null){
53
            valueType = org.springframework.beans.BeanUtils.findPropertyType(name, new Class[] {source.getClass()});
54
            if(valueType == null){
55
                // something went wrong, so better include the property
56
                return false;
57
            }
58
        } else {
59
            valueType = value.getClass();
60
        }
61

    
62
        if(CdmBase.class.isAssignableFrom(valueType)){
63
            if(!includeToOneRelations
64
                    && !exceptions.contains(source.getClass().getSimpleName() + "." + name)){
65
                return true;
66
            }
67
        } else if(Collection.class.isAssignableFrom(valueType) || Map.class.isAssignableFrom(valueType)){
68
            if(!includeToManyRelations
69
                    && !exceptions.contains(source.getClass().getSimpleName() + "." + name)
70
                    && CdmBase.class.isAssignableFrom(source.getClass())){
71
                return true;
72
            }
73
        }
74
        return false;
75
    }
76
}
(1-1/2)