dnet-hadoop/dhp-workflows/dhp-enrichment/src/main/java/eu/dnetlib/dhp/bulktag/community/Constraints.java

69 lines
1.6 KiB
Java
Raw Normal View History

2020-04-30 11:05:17 +02:00
package eu.dnetlib.dhp.bulktag.community;
2020-03-03 16:38:50 +01:00
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;
2020-04-30 11:05:17 +02:00
2020-05-11 17:38:08 +02:00
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import eu.dnetlib.dhp.bulktag.criteria.VerbResolver;
/** Created by miriam on 02/08/2018. */
2020-03-03 16:38:50 +01:00
public class Constraints implements Serializable {
2020-04-30 11:05:17 +02:00
private static final Log log = LogFactory.getLog(Constraints.class);
2020-03-03 16:38:50 +01:00
2021-08-11 12:13:22 +02:00
private List<Constraint> constraint;
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
public List<Constraint> getConstraint() {
return constraint;
}
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
public void setConstraint(List<Constraint> constraint) {
this.constraint = constraint;
}
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
public void setSc(String json) {
Type collectionType = new TypeToken<Collection<Constraint>>() {
}.getType();
constraint = new Gson().fromJson(json, collectionType);
}
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
void setSelection(VerbResolver resolver) {
for (Constraint st : constraint) {
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
try {
st.setSelection(resolver);
2021-08-11 12:13:22 +02:00
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException
| InstantiationException e) {
2020-04-30 11:05:17 +02:00
log.error(e.getMessage());
}
}
}
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
// Constraint in and
public boolean verifyCriteria(final Map<String, List<String>> param) {
2020-03-03 16:38:50 +01:00
2020-04-30 11:05:17 +02:00
for (Constraint sc : constraint) {
boolean verified = false;
if(!param.containsKey(sc.getField()))
return false;
2020-04-30 11:05:17 +02:00
for (String value : param.get(sc.getField())) {
if (sc.verifyCriteria(value.trim())) {
verified = true;
}
}
if (!verified)
return verified;
}
return true;
}
}