1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.util.List; |
---|
7 | import java.util.Map; |
---|
8 | import java.util.Map.Entry; |
---|
9 | |
---|
10 | import org.slf4j.Logger; |
---|
11 | import org.slf4j.LoggerFactory; |
---|
12 | |
---|
13 | import eu.clarin.cmdi.vlo.pojo.VariantsMap; |
---|
14 | import eu.clarin.cmdi.vlo.pojo.VocabularyEntry; |
---|
15 | import eu.clarin.cmdi.vlo.transformers.VariantsMapMarshaller; |
---|
16 | |
---|
17 | /* |
---|
18 | * abstract class that encapsulates common map creation from mapping files |
---|
19 | * for some postprocessors like LanguageCodePostProcessor* |
---|
20 | * |
---|
21 | * brings one more level in class hierarchy between interface PostPorcessor and concrete implementations |
---|
22 | * |
---|
23 | * @author dostojic |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | public abstract class PostProcessorsWithVocabularyMap implements PostProcessor{ |
---|
28 | |
---|
29 | private final static Logger _logger = LoggerFactory.getLogger(PostProcessorsWithVocabularyMap.class); |
---|
30 | |
---|
31 | private List<VocabularyEntry> map = null; |
---|
32 | |
---|
33 | |
---|
34 | /* |
---|
35 | * This method is used for normalization of facet values based on normalization maps. |
---|
36 | * In case that value is not in the map, the input value is returned. |
---|
37 | * |
---|
38 | * @param value - original value from record |
---|
39 | * @return normalized value if there is a match otherwise original value |
---|
40 | * |
---|
41 | */ |
---|
42 | public String normalize(String value){ |
---|
43 | return normalize(value, value); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | /* |
---|
48 | * This method is used for normalization of facet values based on normalization maps. |
---|
49 | * With second parameter user can specify what to return in case when value is not in normalization map (special case for NationalProject facet). |
---|
50 | * |
---|
51 | * @param value - original value from record |
---|
52 | * @param fallBackValue - value to be returned in case of no match |
---|
53 | * @return normalized value if there is a match otherwise returns what is specified with 2nd parameter |
---|
54 | * |
---|
55 | */ |
---|
56 | public String normalize(String value, String fallBackValue){ |
---|
57 | //init |
---|
58 | if(map == null) |
---|
59 | createMap(); |
---|
60 | |
---|
61 | int ind = map.indexOf(value); |
---|
62 | return (ind != -1)? map.get(ind).getNormalizedValue() : fallBackValue; |
---|
63 | } |
---|
64 | |
---|
65 | public Map<String, String> getCrossMappings(String value){ |
---|
66 | |
---|
67 | //init |
---|
68 | if(map == null) |
---|
69 | createMap(); |
---|
70 | |
---|
71 | int ind = map.indexOf(value); |
---|
72 | return (ind != -1)? map.get(ind).getCrossMap() : null; |
---|
73 | } |
---|
74 | |
---|
75 | public abstract String getNormalizationMapURL(); |
---|
76 | |
---|
77 | private void createMap(){ |
---|
78 | VariantsMap varinatsRawMap = getMappingFromFile(getNormalizationMapURL()); |
---|
79 | map = varinatsRawMap.getMap(); |
---|
80 | } |
---|
81 | |
---|
82 | protected VariantsMap getMappingFromFile(String mapUrl){ |
---|
83 | try { |
---|
84 | |
---|
85 | _logger.info("Reading vocabulary file from: {}", mapUrl); |
---|
86 | // load records from file |
---|
87 | // in the future this should be loaded from CLAVAS directly and the file only used as fallback |
---|
88 | |
---|
89 | //InputStream is = PostProcessorsWithVocabularyMap.class.getClassLoader().getResourceAsStream(mapUrl); |
---|
90 | InputStream is = new FileInputStream(new File(mapUrl)); |
---|
91 | |
---|
92 | |
---|
93 | return VariantsMapMarshaller.unmarshal(is); |
---|
94 | } catch (Exception e) { |
---|
95 | throw new RuntimeException("Cannot instantiate postProcessor:", e); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | // for debug |
---|
101 | public void printMap(){ |
---|
102 | _logger.info("map contains {} entries", map.size()); |
---|
103 | for(VocabularyEntry entry: map) |
---|
104 | _logger.info(entry.toString()); |
---|
105 | |
---|
106 | } |
---|
107 | } |
---|