2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.redlist
.bfnXml
.in
;
11 import java
.util
.Arrays
;
12 import java
.util
.List
;
13 import java
.util
.UUID
;
15 import org
.apache
.commons
.lang3
.StringUtils
;
16 import org
.apache
.logging
.log4j
.LogManager
;
17 import org
.apache
.logging
.log4j
.Logger
;
18 import org
.springframework
.stereotype
.Component
;
19 import org
.springframework
.transaction
.TransactionStatus
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
22 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
23 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
24 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
25 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaType
;
26 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
27 import eu
.etaxonomy
.cdm
.model
.term
.OrderedTermVocabulary
;
28 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
29 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
30 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
32 * This class creates all area vocabularies and all distribution status vocabularies
33 * for the German redlists.
40 public class BfnXmlImportAddtionalTerms
extends BfnXmlImportBase
{
42 private static final long serialVersionUID
= 8997499338798245806L;
43 private static final Logger logger
= LogManager
.getLogger();
45 public enum Vocabulary
{
46 GERMAN_FEDERAL_STATES("Bundesländer"),
47 GERMAN_COMBINED_STATES("Kombinierte Bundesländer"),
48 GERMAN_MARINE_ALGAE_AREAS("Marine Algen Gebiete"),
49 GERMAN_MARINE_INVERTEBRATE_AREAS("Marine Invertebraten Gebiete"),
51 GERMAN_PRESENCE_TERMS("Vorkommensstatus"),
52 GERMAN_ESTABLISHMENT_TERMS("Etablierungsstatus");
54 private final String vocabulary
;
56 private Vocabulary(final String vocabulary
) {
57 this.vocabulary
= vocabulary
;
61 public String
toString() {
67 private static final List
<String
> GERMAN_PRESENCE_ABSENCE_TERMS
= Arrays
.asList(new String
[] {
68 "x:" + BfnXmlTransformer
.VORHANDEN
,
69 "?:" + BfnXmlTransformer
.VORHANDEN_UNSICHER
,
70 "#:" + BfnXmlTransformer
.ABWESEND_ABGELEHNT
,
71 "-:" + BfnXmlTransformer
.ABWESEND_KEIN_NACHWEIS
,
73 "a:" + BfnXmlTransformer
.ABWESEND
,
74 "aa:" + BfnXmlTransformer
.ABWESEND_AUSGESTORBEN
,
75 "an:" + BfnXmlTransformer
.ABWESEND_SEIT1980
,
76 "af:" + BfnXmlTransformer
.ABWESEND_FEHLEINGABE
,
78 "v+:" + BfnXmlTransformer
.VORHANDEN_EINBUERGERUNG
,
79 "ve:" + BfnXmlTransformer
.VORHANDEN_ETABLIERT
,
80 "vk:" + BfnXmlTransformer
.VORHANDEN_KULTIVIERT_DOMESTIZIERT
,
81 "vu:" + BfnXmlTransformer
.VORHANDEN_UNBESTAENDIG
,
82 "v?:" + BfnXmlTransformer
.VORHANDEN_VORKOMMEN_UNSICHER
,
86 private static final List
<String
> GERMAN_ESTABLISHMENT_STATUS_TERMS
= Arrays
.asList(new String
[] {
89 "K:Kulturpflanze / domestiziertes Tier",
94 private static final List
<String
> GERMAN_FEDERAL_STATES
= Arrays
.asList(new String
[] {
96 "BW:Baden-Württemberg",
103 "MV:Mecklenburg-Vorpommern",
105 "NW:Nordrhein-Westfalen",
106 "RP:Rheinland-Pfalz",
110 "SH:Schleswig-Holstein",
114 private static final List
<String
> GERMAN_COMBINED_STATES
= Arrays
.asList(new String
[] {
115 "BB+BE:Brandenburg und Berlin",
116 "SH+HH:Schleswig-Holstein und Hamburg"
119 private static final List
<String
> GERMAN_MARINE_ALGAE_AREAS
= Arrays
.asList(new String
[] {
121 "NIW:Niedersächsisches Wattenmeer",
122 "SHW:Schleswig-Holsteinisches Wattenmeer",
123 "SHO:Schleswig-Holsteinische Ostsee",
124 "MVO:Mecklenburg-Vorpommerische Ostsee"
127 private static final List
<String
> GERMAN_MARINE_INVERTEBRATE_AREAS
= Arrays
.asList(new String
[] {
128 "ÄWN:Ästuarien und Watt Nordsee",
129 "SuN:Sublitoral Nordsee",
130 "Hel:Helgoland2", //TODO: the 2 is a workaround to distinguish from Algae Helgoloand, still needs to be discussed with BfN if these are 2 different areas
136 /** Hibernate classification vocabulary initialization strategy */
137 private static final List
<String
> VOC_CLASSIFICATION_INIT_STRATEGY
= Arrays
.asList(new String
[] {
139 "classification.rootNodes",
142 "childNodes.taxon.name",
147 "taxon.secSource.citation",
156 public BfnXmlImportAddtionalTerms(){
162 public void doInvoke(BfnXmlImportState state
){
163 logger
.info("create german terms ...");
165 TransactionStatus tx
= startTransaction();
167 createTerms(GERMAN_FEDERAL_STATES
, Vocabulary
.GERMAN_FEDERAL_STATES
, TermType
.NamedArea
);
168 createTerms(GERMAN_MARINE_ALGAE_AREAS
, Vocabulary
.GERMAN_MARINE_ALGAE_AREAS
, TermType
.NamedArea
);
169 createTerms(GERMAN_MARINE_INVERTEBRATE_AREAS
, Vocabulary
.GERMAN_MARINE_INVERTEBRATE_AREAS
, TermType
.NamedArea
);
170 createTerms(GERMAN_COMBINED_STATES
, Vocabulary
.GERMAN_COMBINED_STATES
, TermType
.NamedArea
);
171 //distribution status
172 createTerms(GERMAN_PRESENCE_ABSENCE_TERMS
, Vocabulary
.GERMAN_PRESENCE_TERMS
, TermType
.PresenceAbsenceTerm
);
173 createTerms(GERMAN_ESTABLISHMENT_STATUS_TERMS
, Vocabulary
.GERMAN_ESTABLISHMENT_TERMS
, TermType
.PresenceAbsenceTerm
);
175 commitTransaction(tx
);
176 logger
.info("end create german terms.");
181 private void createTerms(List
<String
> termList
, Vocabulary vocabulary
, TermType termType
){
182 NamedArea parentGermany
= null;
183 PresenceAbsenceTerm lastParent
= null;
185 for(String strTerm
: termList
){
186 //Split string into label and abbrevated label
187 String
[] splittedStrings
= StringUtils
.splitByWholeSeparator(strTerm
, ":");
188 String abbrevatedLabel
= splittedStrings
[0];
189 String label
= splittedStrings
[1];
190 //get UUID and load existing term
191 UUID termUuuid
= null;
193 if(vocabulary
== Vocabulary
.GERMAN_PRESENCE_TERMS
){
194 termUuuid
= BfnXmlTransformer
.getGermanAbsenceTermUUID(label
);
195 }else if(vocabulary
== Vocabulary
.GERMAN_ESTABLISHMENT_TERMS
){
196 termUuuid
= BfnXmlTransformer
.getGermanEstablishmentTermUUID(label
);
197 }else if(termType
== TermType
.NamedArea
){
198 termUuuid
= BfnXmlTransformer
.getAreaUUID(label
);
200 } catch (UnknownCdmTypeException e
) {
201 logger
.warn("Could not match term to uuid: "+e
.toString());
202 termUuuid
= UUID
.randomUUID();
205 @SuppressWarnings("rawtypes")
206 DefinedTermBase term
= getTermService().load(termUuuid
);
208 //already in the db, so no need to step through the whole process again.
211 if(termType
== TermType
.NamedArea
){
213 NamedArea namedArea
= NamedArea
.NewInstance(label
, label
, abbrevatedLabel
);
215 term
.setIdInVocabulary(Integer
.toString(id
));
216 if (vocabulary
== Vocabulary
.GERMAN_FEDERAL_STATES
){
217 namedArea
.setType(NamedAreaType
.ADMINISTRATION_AREA());
218 if(label
.equalsIgnoreCase("Deutschland")){
219 namedArea
.setLevel(NamedAreaLevel
.COUNTRY());
220 parentGermany
= (NamedArea
) term
;
222 namedArea
.setLevel(NamedAreaLevel
.STATE());
223 term
.setPartOf(parentGermany
);
226 if (vocabulary
== Vocabulary
.GERMAN_COMBINED_STATES
){
227 namedArea
.setType(NamedAreaType
.ADMINISTRATION_AREA());
229 namedArea
.setType(NamedAreaType
.NATURAL_AREA());
233 term
= PresenceAbsenceTerm
.NewPresenceInstance(label
, label
, abbrevatedLabel
);
234 term
.setIdInVocabulary(abbrevatedLabel
);
235 if(vocabulary
.equals(Vocabulary
.GERMAN_PRESENCE_TERMS
)){
236 //create hierarchy of terms
237 if(abbrevatedLabel
.length() >1 ){
238 ((PresenceAbsenceTerm
)term
).setPartOf(lastParent
);
240 lastParent
= (PresenceAbsenceTerm
) term
;
244 term
.setUuid(termUuuid
);
246 createOrUpdateTermVocabulary(termType
, getVocabularyService(), term
, vocabulary
.toString());
251 @SuppressWarnings({ "unchecked", "rawtypes" })
252 private OrderedTermVocabulary
createOrUpdateTermVocabulary(TermType termType
, IVocabularyService vocabularyService
, DefinedTermBase term
, String strTermVocabulary
) {
253 OrderedTermVocabulary termVocabulary
= null;
256 vocUUID
=BfnXmlTransformer
.getRedlistVocabularyUUID(strTermVocabulary
);
257 } catch (UnknownCdmTypeException e
) {
258 // TODO Auto-generated catch block
262 termVocabulary
= (OrderedTermVocabulary
) vocabularyService
.load(vocUUID
);
264 //lookup via String in case uuid lookup does not work
265 if(termVocabulary
== null && vocUUID
== null){
266 List
<TermVocabulary
> vocList
= vocabularyService
.list(TermVocabulary
.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY
);
267 for(TermVocabulary tv
: vocList
){
268 if(tv
.getTitleCache().equalsIgnoreCase(strTermVocabulary
)){
269 termVocabulary
= (OrderedTermVocabulary
) tv
;
273 //create termvocabulary
274 if(termVocabulary
== null){
275 termVocabulary
= OrderedTermVocabulary
.NewOrderedInstance(termType
, null, strTermVocabulary
, strTermVocabulary
, strTermVocabulary
, null);
277 termVocabulary
.setUuid(vocUUID
);
280 termVocabulary
.addTerm(term
);
281 vocabularyService
.saveOrUpdate(termVocabulary
);
283 return termVocabulary
;
287 protected boolean isIgnore(BfnXmlImportState state
){
288 return ! state
.getConfig().isDoAdditionalTerms();
295 public boolean doCheck(BfnXmlImportState state
){
296 boolean result
= true;
297 //TODO needs to be implemented