dnet-hadoop/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/clean/CleaningRuleMap.java

88 lines
3.3 KiB
Java
Raw Normal View History

2020-06-09 17:20:40 +02:00
package eu.dnetlib.dhp.oa.graph.clean;
2020-06-13 13:06:04 +02:00
import java.io.Serializable;
import java.util.HashMap;
2022-08-05 16:56:09 +02:00
import java.util.concurrent.atomic.AtomicReference;
2020-06-12 12:03:25 +02:00
2022-08-05 16:56:09 +02:00
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
2020-06-13 13:06:04 +02:00
import eu.dnetlib.dhp.common.FunctionalInterfaceSupport.SerializableConsumer;
import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup;
import eu.dnetlib.dhp.schema.common.ModelConstants;
2022-08-05 12:32:08 +02:00
import eu.dnetlib.dhp.schema.oaf.*;
2020-06-12 10:45:18 +02:00
2021-08-11 12:13:22 +02:00
public class CleaningRuleMap extends HashMap<Class<?>, SerializableConsumer<Object>> implements Serializable {
2020-06-09 17:20:40 +02:00
2020-06-12 18:25:47 +02:00
/**
2020-06-13 13:06:04 +02:00
* Creates the mapping for the Oaf types subject to cleaning
*
2020-06-12 18:25:47 +02:00
* @param vocabularies
*/
2020-06-13 13:06:04 +02:00
public static CleaningRuleMap create(VocabularyGroup vocabularies) {
CleaningRuleMap mapping = new CleaningRuleMap();
mapping.put(Qualifier.class, o -> cleanQualifier(vocabularies, (Qualifier) o));
mapping.put(AccessRight.class, o -> cleanQualifier(vocabularies, (AccessRight) o));
mapping.put(Country.class, o -> cleanCountry(vocabularies, (Country) o));
mapping.put(Relation.class, o -> cleanRelation(vocabularies, (Relation) o));
2022-08-05 09:11:37 +02:00
mapping.put(Subject.class, o -> cleanSubject(vocabularies, (Subject) o));
2020-06-13 13:06:04 +02:00
return mapping;
2020-06-09 19:52:53 +02:00
}
2020-06-12 18:25:47 +02:00
2022-08-05 16:56:09 +02:00
private static void cleanSubject(VocabularyGroup vocabularies, Subject subject) {
if (cleanSubjectForVocabulary(ModelConstants.DNET_SUBJECT_FOS_CLASSID, vocabularies, subject)) {
return;
} else {
// TODO cleaning based on different subject vocabs can be added here
}
2022-08-05 09:11:37 +02:00
}
2022-08-05 16:56:09 +02:00
private static boolean cleanSubjectForVocabulary(String vocabularyId, VocabularyGroup vocabularies,
Subject subject) {
AtomicReference<Boolean> modified = new AtomicReference<>(false);
2022-08-05 09:11:37 +02:00
vocabularies.find(vocabularyId).ifPresent(vocabulary -> {
2022-08-05 16:56:09 +02:00
if (!ModelConstants.DNET_SUBJECT_KEYWORD.equalsIgnoreCase(subject.getQualifier().getClassid())) {
2022-08-05 09:11:37 +02:00
return;
}
2022-08-05 16:56:09 +02:00
Qualifier newValue = vocabulary.lookup(subject.getValue());
if (!ModelConstants.UNKNOWN.equals(newValue.getClassid())) {
2022-08-05 16:56:09 +02:00
subject.setValue(newValue.getClassid());
subject.getQualifier().setClassid(vocabularyId);
subject.getQualifier().setClassname(vocabulary.getName());
modified.set(true);
2022-08-05 09:11:37 +02:00
}
});
2022-08-05 16:56:09 +02:00
return modified.get();
2022-08-05 09:11:37 +02:00
}
private static void cleanRelation(VocabularyGroup vocabularies, Relation r) {
if (vocabularies.vocabularyExists(ModelConstants.DNET_RELATION_SUBRELTYPE)) {
Qualifier newValue = vocabularies.lookup(ModelConstants.DNET_RELATION_SUBRELTYPE, r.getSubRelType());
r.setSubRelType(newValue.getClassid());
}
if (vocabularies.vocabularyExists(ModelConstants.DNET_RELATION_RELCLASS)) {
Qualifier newValue = vocabularies.lookup(ModelConstants.DNET_RELATION_RELCLASS, r.getRelClass());
r.setRelClass(newValue.getClassid());
}
}
private static void cleanCountry(VocabularyGroup vocabularies, Country o) {
final Country c = o;
if (StringUtils.isBlank(c.getSchemeid())) {
c.setSchemeid(ModelConstants.DNET_COUNTRY_TYPE);
c.setSchemename(ModelConstants.DNET_COUNTRY_TYPE);
}
cleanQualifier(vocabularies, c);
}
private static <Q extends Qualifier> void cleanQualifier(VocabularyGroup vocabularies, Q q) {
if (vocabularies.vocabularyExists(q.getSchemeid())) {
Qualifier newValue = vocabularies.lookup(q.getSchemeid(), q.getClassid());
q.setClassid(newValue.getClassid());
q.setClassname(newValue.getClassname());
}
}
2020-06-09 17:20:40 +02:00
}