1
|
/**
|
2
|
* Copyright (C) 2007 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.util;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Map;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import net.sf.json.util.PropertyFilter;
|
20
|
|
21
|
public class CardinalityPropertyFilter implements PropertyFilter {
|
22
|
|
23
|
@SuppressWarnings("unused")
|
24
|
private static final Logger logger = Logger.getLogger(CardinalityPropertyFilter.class);
|
25
|
|
26
|
boolean includeToOneRelations = true;
|
27
|
|
28
|
boolean includeToManyRelations = true;
|
29
|
|
30
|
Set<String> exceptions = new HashSet<>();
|
31
|
|
32
|
public void setExceptions(Set<String> exceptions) {
|
33
|
this.exceptions = exceptions;
|
34
|
}
|
35
|
|
36
|
public void setIncludeToOneRelations(boolean includeToOneRelations) {
|
37
|
this.includeToOneRelations = includeToOneRelations;
|
38
|
}
|
39
|
|
40
|
public void setIncludeToManyRelations(boolean includeToManyRelations) {
|
41
|
this.includeToManyRelations = includeToManyRelations;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public boolean apply(Object source, String name, Object value) {
|
46
|
Class<?> valueType;
|
47
|
|
48
|
if(value == null){
|
49
|
valueType = org.springframework.beans.BeanUtils.findPropertyType(name, new Class[] {source.getClass()});
|
50
|
if(valueType == null){
|
51
|
// something went wrong, so better include the property
|
52
|
return false;
|
53
|
}
|
54
|
} else {
|
55
|
valueType = value.getClass();
|
56
|
}
|
57
|
|
58
|
if(CdmBase.class.isAssignableFrom(valueType)){
|
59
|
if(!includeToOneRelations
|
60
|
&& !exceptions.contains(source.getClass().getSimpleName() + "." + name)){
|
61
|
return true;
|
62
|
}
|
63
|
} else if(Collection.class.isAssignableFrom(valueType) || Map.class.isAssignableFrom(valueType)){
|
64
|
if(!includeToManyRelations
|
65
|
&& !exceptions.contains(source.getClass().getSimpleName() + "." + name)
|
66
|
&& CdmBase.class.isAssignableFrom(source.getClass())){
|
67
|
return true;
|
68
|
}
|
69
|
}
|
70
|
return false;
|
71
|
}
|
72
|
}
|