1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.redlist.bfnXml.in;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
22
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
23
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
24
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
25
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
26
|
import eu.etaxonomy.cdm.model.common.TermType;
|
27
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
28
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
29
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
30
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
31
|
import eu.etaxonomy.cdm.model.location.NamedAreaType;
|
32
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
33
|
/**
|
34
|
*
|
35
|
* @author a.oppermann
|
36
|
* @date 04.07.2013
|
37
|
*
|
38
|
*/
|
39
|
@Component
|
40
|
public class BfnXmlImportAddtionalTerms extends BfnXmlImportBase {
|
41
|
|
42
|
private static final Logger logger = Logger.getLogger(BfnXmlImportAddtionalTerms.class);
|
43
|
|
44
|
public enum Vocabulary{
|
45
|
GERMAN_FEDERAL_STATES("Bundesländer"),
|
46
|
GERMAN_PRESENCE_TERMS("Vorkommensstatus"),
|
47
|
GERMAN_ESTABLISHMENT_TERMS("Etablierungsstatus");
|
48
|
|
49
|
private final String vocabulary;
|
50
|
|
51
|
private Vocabulary(final String vocabulary) {
|
52
|
this.vocabulary = vocabulary;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public String toString() {
|
57
|
return vocabulary;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
|
62
|
private static final List<String> GERMAN_PRESENCE_ABSENCE_TERMS = Arrays.asList(new String[] {
|
63
|
"a:abwesend",
|
64
|
"aa:abwesend - ausgestorben",
|
65
|
"af:abwesend - frühere Fehleingabe",
|
66
|
"v:vorkommend",
|
67
|
"v+:vorkommend - in Einbürgerung befindlich",
|
68
|
"ve:vorkommend - etabliert",
|
69
|
"vk:vorkommend - kultiviert, domestiziert",
|
70
|
"vu:vorkommend - unbeständig",
|
71
|
"vs:vorkommend - Vorkommen unsicher",
|
72
|
"s:vorkommend - unsicher"
|
73
|
|
74
|
});
|
75
|
|
76
|
private static final List<String> GERMAN_ESTABLISHMENT_STATUS_TERMS = Arrays.asList(new String[] {
|
77
|
"A:Archaeophyt",
|
78
|
"I:Indigen",
|
79
|
"K:Kulturpflanze / domestiziertes Tier",
|
80
|
"N:Neophyt",
|
81
|
"KF:Kultuflüchtling"
|
82
|
});
|
83
|
|
84
|
private static final List<String> GERMAN_FEDERAL_STATES = Arrays.asList(new String[] {
|
85
|
"DE:Deutschland",
|
86
|
"BW:Baden-Württemberg",
|
87
|
"BY:Bayern",
|
88
|
"BE:Berlin",
|
89
|
"BB:Brandenburg",
|
90
|
"HB:Bremen",
|
91
|
"HH:Hamburg",
|
92
|
"HE:Hessen",
|
93
|
"MV:Mecklenburg-Vorpommern",
|
94
|
"NI:Niedersachsen",
|
95
|
"NW:Nordrhein-Westfalen",
|
96
|
"RP:Rheinland-Pfalz",
|
97
|
"SL:Saarland",
|
98
|
"SN:Sachsen",
|
99
|
"ST:Sachsen-Anhalt",
|
100
|
"SH:Schleswig-Holstein",
|
101
|
"TH:Thüringen"
|
102
|
});
|
103
|
|
104
|
|
105
|
/** Hibernate classification vocabulary initialization strategy */
|
106
|
private static final List<String> VOC_CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[] {
|
107
|
"classification.$",
|
108
|
"classification.rootNodes",
|
109
|
"childNodes",
|
110
|
"childNodes.taxon",
|
111
|
"childNodes.taxon.name",
|
112
|
"taxonNodes",
|
113
|
"taxonNodes.taxon",
|
114
|
"synonymRelations",
|
115
|
"taxon.*",
|
116
|
"taxon.sec",
|
117
|
"taxon.name.*",
|
118
|
"taxon.synonymRelations",
|
119
|
"termVocabulary.*",
|
120
|
"terms",
|
121
|
"namedArea"
|
122
|
|
123
|
});
|
124
|
|
125
|
|
126
|
public BfnXmlImportAddtionalTerms(){
|
127
|
super();
|
128
|
}
|
129
|
|
130
|
|
131
|
@Override
|
132
|
public void doInvoke(BfnXmlImportState state){
|
133
|
logger.info("create german terms ...");
|
134
|
IVocabularyService vocabularyService = getVocabularyService();
|
135
|
ITermService termService = getTermService();
|
136
|
TransactionStatus tx = startTransaction();
|
137
|
// createGermanNamedAreas(state, vocabularyService, termService);
|
138
|
createGermanTerms(vocabularyService, termService, GERMAN_FEDERAL_STATES, Vocabulary.GERMAN_FEDERAL_STATES);
|
139
|
createGermanTerms(vocabularyService, termService, GERMAN_PRESENCE_ABSENCE_TERMS, Vocabulary.GERMAN_PRESENCE_TERMS);
|
140
|
createGermanTerms(vocabularyService, termService, GERMAN_ESTABLISHMENT_STATUS_TERMS, Vocabulary.GERMAN_ESTABLISHMENT_TERMS);
|
141
|
commitTransaction(tx);
|
142
|
logger.info("end create german terms.");
|
143
|
return;
|
144
|
|
145
|
}
|
146
|
//
|
147
|
//
|
148
|
// private void createGermanNamedAreas(BfnXmlImportState state ,IVocabularyService vocabularyService, ITermService termService) {
|
149
|
// int id = 0;
|
150
|
// NamedArea parentGermany = null;
|
151
|
// for(String strGermanState:GERMAN_FEDERAL_STATES){
|
152
|
// UUID germanStateUUID;
|
153
|
// try {
|
154
|
// germanStateUUID = BfnXmlTransformer.getGermanStateUUID(strGermanState);
|
155
|
// } catch (UnknownCdmTypeException e) {
|
156
|
// logger.warn("Could not match german state to uuid: "+e.toString());
|
157
|
// germanStateUUID = UUID.randomUUID();
|
158
|
// }
|
159
|
// NamedArea germanState = (NamedArea)termService.load(germanStateUUID);
|
160
|
// if(germanState != null){
|
161
|
// //already in the db, so no need to step through the whole process again.
|
162
|
// return;
|
163
|
// }else{
|
164
|
// germanState = NamedArea.NewInstance(strGermanState, strGermanState, strGermanState);
|
165
|
// germanState.setUuid(germanStateUUID);
|
166
|
// germanState.setType(NamedAreaType.ADMINISTRATION_AREA());
|
167
|
// germanState.setIdInVocabulary(Integer.toString(id));;
|
168
|
// if(strGermanState.equalsIgnoreCase("Deutschland")){
|
169
|
// germanState.setLevel(NamedAreaLevel.COUNTRY());
|
170
|
// parentGermany = germanState;
|
171
|
// }else{
|
172
|
// germanState.setLevel(NamedAreaLevel.STATE());
|
173
|
// germanState.setPartOf(parentGermany);
|
174
|
// }
|
175
|
// }
|
176
|
// createOrUpdateTermVocabulary(TermType.NamedArea, vocabularyService, germanState, "German Federal States");
|
177
|
// id++;
|
178
|
// }
|
179
|
// }
|
180
|
|
181
|
private void createGermanTerms(IVocabularyService vocabularyService,ITermService termService , List<String> termList, Vocabulary vocabulary){
|
182
|
NamedArea parentGermany = null;
|
183
|
PresenceAbsenceTerm parent = null;
|
184
|
int id = 0;
|
185
|
for(String strGermanTerm:termList){
|
186
|
//Split string into label and abbrevated label
|
187
|
String[] splittedStrings = StringUtils.splitByWholeSeparator(strGermanTerm, ":");
|
188
|
String abbrevatedLabel = splittedStrings[0];
|
189
|
String label = splittedStrings[1];
|
190
|
//get UUID and load existing term
|
191
|
UUID termUuuid = null;
|
192
|
try {
|
193
|
if(vocabulary.equals(Vocabulary.GERMAN_PRESENCE_TERMS)){
|
194
|
termUuuid = BfnXmlTransformer.getGermanAbsenceTermUUID(label);
|
195
|
}else if(vocabulary.equals(Vocabulary.GERMAN_ESTABLISHMENT_TERMS)){
|
196
|
termUuuid = BfnXmlTransformer.getGermanEstablishmentTermUUID(label);
|
197
|
}else if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
198
|
termUuuid = BfnXmlTransformer.getGermanStateUUID(label);
|
199
|
}
|
200
|
} catch (UnknownCdmTypeException e) {
|
201
|
logger.warn("Could not match term to uuid: "+e.toString());
|
202
|
termUuuid = UUID.randomUUID();
|
203
|
}
|
204
|
DefinedTermBase term = termService.load(termUuuid);
|
205
|
if(term != null){
|
206
|
//already in the db, so no need to step through the whole process again.
|
207
|
return;
|
208
|
}else{
|
209
|
if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
210
|
//Namedareas
|
211
|
term = NamedArea.NewInstance(label, label, abbrevatedLabel);
|
212
|
((NamedArea) term).setType(NamedAreaType.ADMINISTRATION_AREA());
|
213
|
term.setIdInVocabulary(Integer.toString(id));
|
214
|
if(label.equalsIgnoreCase("Deutschland")){
|
215
|
((NamedArea) term).setLevel(NamedAreaLevel.COUNTRY());
|
216
|
parentGermany = (NamedArea) term;
|
217
|
}else{
|
218
|
((NamedArea) term).setLevel(NamedAreaLevel.STATE());
|
219
|
term.setPartOf(parentGermany);
|
220
|
}
|
221
|
}else{
|
222
|
term = PresenceAbsenceTerm.NewPresenceInstance(label, label, abbrevatedLabel);
|
223
|
term.setIdInVocabulary(abbrevatedLabel);
|
224
|
if(vocabulary.equals(Vocabulary.GERMAN_PRESENCE_TERMS)){
|
225
|
//create hierarchy of terms
|
226
|
if(label.equalsIgnoreCase("abwesend")){
|
227
|
parent = (PresenceAbsenceTerm) term;
|
228
|
}else if(label.equalsIgnoreCase("vorkommend")){
|
229
|
parent = (PresenceAbsenceTerm) term;
|
230
|
}else{
|
231
|
((PresenceAbsenceTerm)term).setPartOf(parent);
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
term.setUuid(termUuuid);
|
236
|
}
|
237
|
if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
238
|
createOrUpdateTermVocabulary(TermType.NamedArea, vocabularyService, term, vocabulary.toString());
|
239
|
id++;
|
240
|
}else{
|
241
|
createOrUpdateTermVocabulary(TermType.PresenceAbsenceTerm, vocabularyService, term, vocabulary.toString());
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
|
246
|
|
247
|
/**
|
248
|
* @param vocabularyService
|
249
|
* @param term
|
250
|
* @param vocUUID
|
251
|
*/
|
252
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
253
|
private OrderedTermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
|
254
|
OrderedTermVocabulary termVocabulary = null;
|
255
|
UUID vocUUID = null;
|
256
|
try {
|
257
|
vocUUID=BfnXmlTransformer.getRedlistVocabularyUUID(strTermVocabulary);
|
258
|
} catch (UnknownCdmTypeException e) {
|
259
|
// TODO Auto-generated catch block
|
260
|
e.printStackTrace();
|
261
|
}
|
262
|
if(vocUUID != null){
|
263
|
termVocabulary = (OrderedTermVocabulary) vocabularyService.load(vocUUID);
|
264
|
}
|
265
|
//lookup via String in case uuid lookup does not work
|
266
|
if(termVocabulary == null && vocUUID == null){
|
267
|
List<TermVocabulary> vocList = vocabularyService.list(TermVocabulary.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY);
|
268
|
for(TermVocabulary tv : vocList){
|
269
|
if(tv.getTitleCache().equalsIgnoreCase(strTermVocabulary)){
|
270
|
termVocabulary = (OrderedTermVocabulary) tv;
|
271
|
}
|
272
|
}
|
273
|
}
|
274
|
//create termvocabulary
|
275
|
if(termVocabulary == null){
|
276
|
termVocabulary = OrderedTermVocabulary.NewInstance(termType, strTermVocabulary, strTermVocabulary, strTermVocabulary, null);
|
277
|
if(vocUUID != null){
|
278
|
termVocabulary.setUuid(vocUUID);
|
279
|
}
|
280
|
}
|
281
|
termVocabulary.addTerm((OrderedTermBase) term);
|
282
|
vocabularyService.saveOrUpdate(termVocabulary);
|
283
|
|
284
|
return termVocabulary;
|
285
|
}
|
286
|
|
287
|
@Override
|
288
|
protected boolean isIgnore(BfnXmlImportState state){
|
289
|
return ! state.getConfig().isDoAdditionalTerms();
|
290
|
}
|
291
|
|
292
|
|
293
|
|
294
|
|
295
|
@Override
|
296
|
public boolean doCheck(BfnXmlImportState state){
|
297
|
boolean result = true;
|
298
|
//TODO needs to be implemented
|
299
|
return result;
|
300
|
}
|
301
|
|
302
|
|
303
|
}
|