1
|
package eu.dnetlib.data.bulktag;
|
2
|
|
3
|
import com.google.common.reflect.TypeToken;
|
4
|
import com.google.gson.Gson;
|
5
|
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver;
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
|
9
|
|
10
|
import java.io.Serializable;
|
11
|
import java.lang.reflect.InvocationTargetException;
|
12
|
import java.lang.reflect.Type;
|
13
|
import java.util.Collection;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
/**
|
18
|
* Created by miriam on 02/08/2018.
|
19
|
*/
|
20
|
public class Constraints implements Serializable {
|
21
|
private static final Log log = LogFactory.getLog(Constraints.class);
|
22
|
//private ConstraintEncapsulator ce;
|
23
|
private List<Constraint> constraint;
|
24
|
|
25
|
|
26
|
public Constraints() {
|
27
|
}
|
28
|
public List<Constraint> getConstraint() {
|
29
|
return constraint;
|
30
|
}
|
31
|
|
32
|
public void setConstraint(List<Constraint> constraint) {
|
33
|
this.constraint = constraint;
|
34
|
}
|
35
|
|
36
|
public void setSc(String json){
|
37
|
Type collectionType = new TypeToken<Collection<Constraint>>(){}.getType();
|
38
|
constraint = new Gson().fromJson(json, collectionType);
|
39
|
|
40
|
}
|
41
|
|
42
|
void setSelection(VerbResolver resolver) {
|
43
|
for(Constraint st: constraint){
|
44
|
|
45
|
try {
|
46
|
st.setSelection(resolver);
|
47
|
} catch (NoSuchMethodException e) {
|
48
|
log.error(e.getMessage());
|
49
|
} catch (IllegalAccessException e) {
|
50
|
log.error(e.getMessage());
|
51
|
} catch (InvocationTargetException e) {
|
52
|
log.error(e.getMessage());
|
53
|
} catch (InstantiationException e) {
|
54
|
log.error(e.getMessage());
|
55
|
}
|
56
|
}
|
57
|
|
58
|
}
|
59
|
|
60
|
// //Constraint in and
|
61
|
// public boolean verifyCriteria(Map<String,String> param){
|
62
|
// for(Constraint constraint: constraint){
|
63
|
// if(!constraint.verifyCriteria(param.get(constraint.getField())))
|
64
|
// return false;
|
65
|
// }
|
66
|
// return true;
|
67
|
// }
|
68
|
|
69
|
//Constraint in and
|
70
|
public boolean verifyCriteria(Map<String,List<String>> param){
|
71
|
|
72
|
for(Constraint sc: constraint){
|
73
|
boolean verified = false;
|
74
|
for(String value : param.get(sc.getField())){
|
75
|
if (sc.verifyCriteria(value.trim())){
|
76
|
verified = true;
|
77
|
}
|
78
|
}
|
79
|
if(!verified)
|
80
|
return verified;
|
81
|
}
|
82
|
return true;
|
83
|
}
|
84
|
/* public ConstraintEncapsulator getCe() {
|
85
|
return ce;
|
86
|
}
|
87
|
|
88
|
public void setCe(ConstraintEncapsulator ce) {
|
89
|
this.ce = ce;
|
90
|
}
|
91
|
|
92
|
public void setConstraint(String json){
|
93
|
|
94
|
ce = new Gson().fromJson(json, ConstraintEncapsulator.class);
|
95
|
|
96
|
}
|
97
|
|
98
|
void setSelection(VerbResolver resolver) {
|
99
|
for(Constraint st: ce.getConstraint()){
|
100
|
|
101
|
try {
|
102
|
st.setSelection(resolver);
|
103
|
} catch (NoSuchMethodException e) {
|
104
|
log.error(e.getMessage());
|
105
|
} catch (IllegalAccessException e) {
|
106
|
log.error(e.getMessage());
|
107
|
} catch (InvocationTargetException e) {
|
108
|
log.error(e.getMessage());
|
109
|
} catch (InstantiationException e) {
|
110
|
log.error(e.getMessage());
|
111
|
}
|
112
|
}
|
113
|
|
114
|
}
|
115
|
|
116
|
// //Constraint in and
|
117
|
// public boolean verifyCriteria(Map<String,String> param){
|
118
|
// for(Constraint ce: ce){
|
119
|
// if(!ce.verifyCriteria(param.get(ce.getField())))
|
120
|
// return false;
|
121
|
// }
|
122
|
// return true;
|
123
|
// }
|
124
|
|
125
|
//Constraint in and
|
126
|
public boolean verifyCriteria(Map<String,List<String>> param) throws NullPointerException {
|
127
|
for(Constraint constraint: ce.getConstraint()){
|
128
|
boolean verified = false;
|
129
|
for(String value : param.get(constraint.getField())){
|
130
|
try {
|
131
|
if (constraint.verifyCriteria(value.trim())) {
|
132
|
verified = true;
|
133
|
}
|
134
|
}catch(NullPointerException npe){
|
135
|
log.info(new Gson().toJson(this));
|
136
|
throw new NullPointerException();
|
137
|
|
138
|
}
|
139
|
}
|
140
|
if(!verified)
|
141
|
return verified;
|
142
|
}
|
143
|
return true;
|
144
|
}*/
|
145
|
}
|