Project

General

Profile

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

    
61
    //Constraint in and
62
    public boolean verifyCriteria(final Map<String, List<String>> param) {
63

    
64
        for(Constraint sc : constraint) {
65
            boolean verified = false;
66
            for(String value : param.get(sc.getField())){
67
                if (sc.verifyCriteria(value.trim())){
68
                    verified = true;
69
                }
70
            }
71
            if(!verified)
72
                return verified;
73
        }
74
        return true;
75
    }
76

    
77
}
(5-5/10)