1 |
36370
|
michele.ar
|
package eu.dnetlib.data.resultSet.cleaner;
|
2 |
|
|
|
3 |
|
|
import java.util.HashMap;
|
4 |
|
|
import java.util.Map;
|
5 |
|
|
import java.util.Set;
|
6 |
|
|
|
7 |
|
|
import org.apache.commons.logging.Log;
|
8 |
|
|
import org.apache.commons.logging.LogFactory;
|
9 |
|
|
|
10 |
|
|
import com.google.common.base.Joiner;
|
11 |
|
|
import com.google.common.collect.Maps;
|
12 |
|
|
import com.google.common.collect.Sets;
|
13 |
|
|
|
14 |
|
|
import eu.dnetlib.enabling.datastructures.Vocabulary;
|
15 |
|
|
import eu.dnetlib.enabling.datastructures.VocabularyTerms;
|
16 |
|
|
import eu.dnetlib.enabling.is.client.InformationServiceClient;
|
17 |
|
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
18 |
|
|
|
19 |
|
|
public class VocabularyRule extends XPathCleaningRule {
|
20 |
|
|
|
21 |
|
|
private Set<String> vocabularies;
|
22 |
|
|
|
23 |
|
|
private static final Log log = LogFactory.getLog(VocabularyRule.class); // NOPMD by marko on 11/24/08 5:02 PM
|
24 |
|
|
|
25 |
|
|
private Map<String, String> synonyms = Maps.newHashMap();
|
26 |
|
|
private Set<String> validTerms = Sets.newHashSet();
|
27 |
|
|
|
28 |
|
|
public VocabularyRule(final Set<String> vocabularies, final String xpath, final boolean strict, final InformationServiceClient isClient) {
|
29 |
|
|
super(xpath, strict);
|
30 |
|
|
|
31 |
|
|
this.vocabularies = vocabularies;
|
32 |
|
|
for (final String vocName : vocabularies) {
|
33 |
|
|
try {
|
34 |
36812
|
michele.ar
|
for (VocabularyTerms term : isClient.getResourceByCode(vocName, Vocabulary.class).getTerms()) {
|
35 |
36370
|
michele.ar
|
final String code = term.getCode();
|
36 |
|
|
validTerms.add(code.toLowerCase());
|
37 |
|
|
synonyms.put(code.toLowerCase(), code);
|
38 |
|
|
synonyms.put(term.getNativeName().toLowerCase(), code);
|
39 |
|
|
synonyms.put(term.getEnglishName().toLowerCase(), code);
|
40 |
|
|
for (String synonym : term.getSynonyms()) {
|
41 |
|
|
synonyms.put(synonym.toLowerCase(), code);
|
42 |
|
|
}
|
43 |
|
|
}
|
44 |
|
|
} catch (InformationServiceException e) {
|
45 |
|
|
log.warn("Invalid vocabulary " + vocName, e);
|
46 |
|
|
}
|
47 |
|
|
}
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
@Override
|
51 |
|
|
protected String calculateNewValue(final String oldValue) {
|
52 |
|
|
log.debug("calculating new value for: " + oldValue);
|
53 |
|
|
|
54 |
|
|
if (synonyms.isEmpty()) {
|
55 |
|
|
log.warn("Vocabulary terms is void, vocabularies: " + this.vocabularies);
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
String newValue = null;
|
59 |
|
|
|
60 |
|
|
if (synonyms.containsKey(oldValue.toLowerCase())) {
|
61 |
|
|
newValue = synonyms.get(oldValue.toLowerCase());
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
if (newValue == null) {
|
65 |
|
|
log.debug("Synonym " + oldValue + " not found in vocabulary");
|
66 |
|
|
return oldValue;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
return newValue;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
@Override
|
73 |
|
|
protected Map<String, String> verifyValue(final String value) {
|
74 |
|
|
if (synonyms.isEmpty()) {
|
75 |
|
|
log.warn("Vocabulary terms is void, vocabularies: " + this.vocabularies);
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
if (validTerms.contains(value.toLowerCase())) { return null; }
|
79 |
|
|
|
80 |
|
|
final Map<String, String> error = new HashMap<String, String>();
|
81 |
|
|
error.put("term", value);
|
82 |
|
|
error.put("vocabularies", this.vocabularies.toString().replaceAll("\\[", "").replaceAll("\\]", ""));
|
83 |
|
|
error.put("xpath", this.getXpath());
|
84 |
|
|
return error;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
public Map<String, String> getVocabularyTerms() {
|
88 |
|
|
return synonyms;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
@Override
|
92 |
|
|
public String toString() {
|
93 |
|
|
return "VOCABULARIES: [" + Joiner.on(", ").join(vocabularies) + "]";
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
}
|