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;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
21
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
22
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
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 implements ICdmIO<BfnXmlImportState> {
|
41
|
|
42
|
private static final Logger logger = Logger.getLogger(BfnXmlImportAddtionalTerms.class);
|
43
|
|
44
|
public BfnXmlImportAddtionalTerms(){
|
45
|
super();
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public boolean doCheck(BfnXmlImportState state){
|
50
|
boolean result = true;
|
51
|
//TODO needs to be implemented
|
52
|
return result;
|
53
|
}
|
54
|
|
55
|
public enum Vocabulary{
|
56
|
GERMAN_FEDERAL_STATES("Bundesländer"),
|
57
|
GERMAN_PRESENCE_TERMS("Vorkommensstatus"),
|
58
|
GERMAN_ESTABLISHMENT_TERMS("Etablierungsstatus");
|
59
|
|
60
|
private final String vocabulary;
|
61
|
|
62
|
private Vocabulary(final String vocabulary) {
|
63
|
this.vocabulary = vocabulary;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public String toString() {
|
68
|
return vocabulary;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
74
|
public void doInvoke(BfnXmlImportState state){
|
75
|
logger.info("create german terms ...");
|
76
|
IVocabularyService vocabularyService = getVocabularyService();
|
77
|
ITermService termService = getTermService();
|
78
|
TransactionStatus tx = startTransaction();
|
79
|
// createGermanNamedAreas(state, vocabularyService, termService);
|
80
|
createGermanTerms(vocabularyService, termService, GERMAN_FEDERAL_STATES, Vocabulary.GERMAN_FEDERAL_STATES);
|
81
|
createGermanTerms(vocabularyService, termService, GERMAN_PRESENCE_ABSENCE_TERMS, Vocabulary.GERMAN_PRESENCE_TERMS);
|
82
|
createGermanTerms(vocabularyService, termService, GERMAN_ESTABLISHMENT_STATUS_TERMS, Vocabulary.GERMAN_ESTABLISHMENT_TERMS);
|
83
|
commitTransaction(tx);
|
84
|
logger.info("end create german terms.");
|
85
|
return;
|
86
|
|
87
|
}
|
88
|
//
|
89
|
//
|
90
|
// private void createGermanNamedAreas(BfnXmlImportState state ,IVocabularyService vocabularyService, ITermService termService) {
|
91
|
// int id = 0;
|
92
|
// NamedArea parentGermany = null;
|
93
|
// for(String strGermanState:GERMAN_FEDERAL_STATES){
|
94
|
// UUID germanStateUUID;
|
95
|
// try {
|
96
|
// germanStateUUID = BfnXmlTransformer.getGermanStateUUID(strGermanState);
|
97
|
// } catch (UnknownCdmTypeException e) {
|
98
|
// logger.warn("Could not match german state to uuid: "+e.toString());
|
99
|
// germanStateUUID = UUID.randomUUID();
|
100
|
// }
|
101
|
// NamedArea germanState = (NamedArea)termService.load(germanStateUUID);
|
102
|
// if(germanState != null){
|
103
|
// //already in the db, so no need to step through the whole process again.
|
104
|
// return;
|
105
|
// }else{
|
106
|
// germanState = NamedArea.NewInstance(strGermanState, strGermanState, strGermanState);
|
107
|
// germanState.setUuid(germanStateUUID);
|
108
|
// germanState.setType(NamedAreaType.ADMINISTRATION_AREA());
|
109
|
// germanState.setIdInVocabulary(Integer.toString(id));;
|
110
|
// if(strGermanState.equalsIgnoreCase("Deutschland")){
|
111
|
// germanState.setLevel(NamedAreaLevel.COUNTRY());
|
112
|
// parentGermany = germanState;
|
113
|
// }else{
|
114
|
// germanState.setLevel(NamedAreaLevel.STATE());
|
115
|
// germanState.setPartOf(parentGermany);
|
116
|
// }
|
117
|
// }
|
118
|
// createOrUpdateTermVocabulary(TermType.NamedArea, vocabularyService, germanState, "German Federal States");
|
119
|
// id++;
|
120
|
// }
|
121
|
// }
|
122
|
|
123
|
private void createGermanTerms(IVocabularyService vocabularyService,ITermService termService , List<String> termList, Vocabulary vocabulary){
|
124
|
NamedArea parentGermany = null;
|
125
|
int id = 0;
|
126
|
for(String strGermanTerm:termList){
|
127
|
//get UUID and load existing term
|
128
|
UUID termUuuid = null;
|
129
|
try {
|
130
|
if(vocabulary.equals(Vocabulary.GERMAN_PRESENCE_TERMS)){
|
131
|
termUuuid = BfnXmlTransformer.getGermanAbsenceTermUUID(strGermanTerm);
|
132
|
}else if(vocabulary.equals(Vocabulary.GERMAN_ESTABLISHMENT_TERMS)){
|
133
|
termUuuid = BfnXmlTransformer.getGermanEstablishmentTermUUID(strGermanTerm);
|
134
|
}else if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
135
|
termUuuid = BfnXmlTransformer.getGermanStateUUID(strGermanTerm);
|
136
|
}
|
137
|
} catch (UnknownCdmTypeException e) {
|
138
|
logger.warn("Could not match term to uuid: "+e.toString());
|
139
|
termUuuid = UUID.randomUUID();
|
140
|
}
|
141
|
DefinedTermBase term = termService.load(termUuuid);
|
142
|
if(term != null){
|
143
|
//already in the db, so no need to step through the whole process again.
|
144
|
return;
|
145
|
}else{
|
146
|
if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
147
|
//Namedareas
|
148
|
term = NamedArea.NewInstance(strGermanTerm, strGermanTerm, strGermanTerm);
|
149
|
term.setUuid(termUuuid);
|
150
|
((NamedArea) term).setType(NamedAreaType.ADMINISTRATION_AREA());
|
151
|
term.setIdInVocabulary(Integer.toString(id));;
|
152
|
if(strGermanTerm.equalsIgnoreCase("Deutschland")){
|
153
|
((NamedArea) term).setLevel(NamedAreaLevel.COUNTRY());
|
154
|
parentGermany = (NamedArea) term;
|
155
|
}else{
|
156
|
((NamedArea) term).setLevel(NamedAreaLevel.STATE());
|
157
|
term.setPartOf(parentGermany);
|
158
|
}
|
159
|
}else{
|
160
|
term = PresenceAbsenceTerm.NewPresenceInstance(strGermanTerm, strGermanTerm, strGermanTerm);
|
161
|
//TODO create hierarchy
|
162
|
// ((PresenceAbsenceTerm)term).setPartOf(partOf);
|
163
|
term.setUuid(termUuuid);
|
164
|
}
|
165
|
}
|
166
|
if(vocabulary.equals(Vocabulary.GERMAN_FEDERAL_STATES)){
|
167
|
createOrUpdateTermVocabulary(TermType.NamedArea, vocabularyService, term, vocabulary.toString());
|
168
|
id++;
|
169
|
}else{
|
170
|
createOrUpdateTermVocabulary(TermType.PresenceAbsenceTerm, vocabularyService, term, vocabulary.toString());
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
|
175
|
|
176
|
/**
|
177
|
* @param vocabularyService
|
178
|
* @param term
|
179
|
* @param vocUUID
|
180
|
*/
|
181
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
182
|
private OrderedTermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
|
183
|
OrderedTermVocabulary termVocabulary = null;
|
184
|
UUID vocUUID = null;
|
185
|
try {
|
186
|
vocUUID=BfnXmlTransformer.getRedlistVocabularyUUID(strTermVocabulary);
|
187
|
} catch (UnknownCdmTypeException e) {
|
188
|
// TODO Auto-generated catch block
|
189
|
e.printStackTrace();
|
190
|
}
|
191
|
if(vocUUID != null){
|
192
|
termVocabulary = (OrderedTermVocabulary) vocabularyService.load(vocUUID);
|
193
|
}
|
194
|
//lookup via String in case uuid lookup does not work
|
195
|
if(termVocabulary == null && vocUUID == null){
|
196
|
List<TermVocabulary> vocList = vocabularyService.list(TermVocabulary.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY);
|
197
|
for(TermVocabulary tv : vocList){
|
198
|
if(tv.getTitleCache().equalsIgnoreCase(strTermVocabulary)){
|
199
|
termVocabulary = (OrderedTermVocabulary) tv;
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
//create termvocabulary
|
204
|
if(termVocabulary == null){
|
205
|
termVocabulary = OrderedTermVocabulary.NewInstance(termType, strTermVocabulary, strTermVocabulary, strTermVocabulary, null);
|
206
|
if(vocUUID != null){
|
207
|
termVocabulary.setUuid(vocUUID);
|
208
|
}
|
209
|
}
|
210
|
termVocabulary.addTerm((OrderedTermBase) term);
|
211
|
vocabularyService.saveOrUpdate(termVocabulary);
|
212
|
|
213
|
return termVocabulary;
|
214
|
}
|
215
|
|
216
|
|
217
|
/* (non-Javadoc)
|
218
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
219
|
*/
|
220
|
@Override
|
221
|
protected boolean isIgnore(BfnXmlImportState state){
|
222
|
return ! state.getConfig().isDoTaxonNames();
|
223
|
}
|
224
|
|
225
|
|
226
|
|
227
|
private static final List<String> GERMAN_PRESENCE_ABSENCE_TERMS = Arrays.asList(new String[] {
|
228
|
"abwesend",
|
229
|
"abwesend - ausgestorben",
|
230
|
"abwesend - frühere Fehleingabe",
|
231
|
"vorkommend",
|
232
|
"vorkommend - in Einbürgerung befindlich",
|
233
|
"vorkommend - etabliert",
|
234
|
"vorkommend - kultiviert, domestiziert",
|
235
|
"vorkommend - unbeständig",
|
236
|
"vorkommend - Vorkommen unsicher",
|
237
|
"vorkommend - unsicher"
|
238
|
|
239
|
});
|
240
|
|
241
|
private static final List<String> GERMAN_ESTABLISHMENT_STATUS_TERMS = Arrays.asList(new String[] {
|
242
|
"Archaeophyt",
|
243
|
"Indigen",
|
244
|
"Kulturpflanze / domestiziertes Tier",
|
245
|
"Neophyt",
|
246
|
"Kultuflüchtling"
|
247
|
});
|
248
|
|
249
|
private static final List<String> GERMAN_FEDERAL_STATES = Arrays.asList(new String[] {
|
250
|
"Deutschland",
|
251
|
"Baden-Württemberg",
|
252
|
"Bayern",
|
253
|
"Berlin",
|
254
|
"Brandenburg",
|
255
|
"Bremen",
|
256
|
"Hamburg",
|
257
|
"Hessen",
|
258
|
"Mecklenburg-Vorpommern",
|
259
|
"Niedersachsen",
|
260
|
"Nordrhein-Westfalen",
|
261
|
"Rheinland-Pfalz",
|
262
|
"Saarland",
|
263
|
"Sachsen",
|
264
|
"Sachsen-Anhalt",
|
265
|
"Schleswig-Holstein",
|
266
|
"Thüringen"
|
267
|
});
|
268
|
|
269
|
|
270
|
/** Hibernate classification vocabulary initialisation strategy */
|
271
|
private static final List<String> VOC_CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[] {
|
272
|
"classification.$",
|
273
|
"classification.rootNodes",
|
274
|
"childNodes",
|
275
|
"childNodes.taxon",
|
276
|
"childNodes.taxon.name",
|
277
|
"taxonNodes",
|
278
|
"taxonNodes.taxon",
|
279
|
"synonymRelations",
|
280
|
"taxon.*",
|
281
|
"taxon.sec",
|
282
|
"taxon.name.*",
|
283
|
"taxon.synonymRelations",
|
284
|
"termVocabulary.*",
|
285
|
"terms",
|
286
|
"namedArea"
|
287
|
|
288
|
});
|
289
|
|
290
|
|
291
|
}
|