Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Revision:
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.IVocabularyService;
22
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
23
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
24
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
25
import eu.etaxonomy.cdm.model.common.TermType;
26
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
28
import eu.etaxonomy.cdm.model.location.NamedArea;
29
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
30
import eu.etaxonomy.cdm.model.location.NamedAreaType;
31
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
32
/**
33
 *
34
 * This class creates all area vocabularies and all distribution status vocabularies
35
 * for the German redlists.
36
 *
37
 * @author a.oppermann
38
 * @author a.mueller
39
 * @date 04.07.2013
40
 *
41
 */
42
@Component
43
public class BfnXmlImportAddtionalTerms extends BfnXmlImportBase {
44

    
45
    private static final long serialVersionUID = 8997499338798245806L;
46

    
47
    private static final Logger logger = Logger.getLogger(BfnXmlImportAddtionalTerms.class);
48

    
49
    public enum Vocabulary{
50
        GERMAN_FEDERAL_STATES("Bundesländer"),
51
        GERMAN_COMBINED_STATES("Kombinierte Bundesländer"),
52
        GERMAN_MARINE_ALGAE_AREAS("Marine Algen Gebiete"),
53
        GERMAN_MARINE_INVERTEBRATE_AREAS("Marine Invertebraten Gebiete"),
54

    
55
        GERMAN_PRESENCE_TERMS("Vorkommensstatus"),
56
        GERMAN_ESTABLISHMENT_TERMS("Etablierungsstatus");
57

    
58
        private final String vocabulary;
59

    
60
        private Vocabulary(final String vocabulary) {
61
            this.vocabulary = vocabulary;
62
        }
63

    
64
        @Override
65
        public String toString() {
66
            return vocabulary;
67
        }
68
    }
69

    
70

    
71
    private static final List<String> GERMAN_PRESENCE_ABSENCE_TERMS = Arrays.asList(new String[] {
72
            "x:" + BfnXmlTransformer.VORHANDEN ,
73
            "?:" + BfnXmlTransformer.VORHANDEN_UNSICHER,
74
            "#:" + BfnXmlTransformer.ABWESEND_ABGELEHNT,
75
            "-:" + BfnXmlTransformer.ABWESEND_KEIN_NACHWEIS,
76

    
77
            "a:" + BfnXmlTransformer.ABWESEND,
78
            "aa:"  + BfnXmlTransformer.ABWESEND_AUSGESTORBEN,
79
            "an:"  + BfnXmlTransformer.ABWESEND_SEIT1980,
80
            "af:" + BfnXmlTransformer.ABWESEND_FEHLEINGABE,
81

    
82
            "v+:" + BfnXmlTransformer.VORHANDEN_EINBUERGERUNG,
83
            "ve:" + BfnXmlTransformer.VORHANDEN_ETABLIERT,
84
            "vk:" + BfnXmlTransformer.VORHANDEN_KULTIVIERT_DOMESTIZIERT,
85
            "vu:" + BfnXmlTransformer.VORHANDEN_UNBESTAENDIG,
86
            "v?:" + BfnXmlTransformer.VORHANDEN_VORKOMMEN_UNSICHER,
87

    
88
    });
89

    
90
    private static final List<String> GERMAN_ESTABLISHMENT_STATUS_TERMS = Arrays.asList(new String[] {
91
            "A:Archaeophyt",
92
            "I:Indigen",
93
            "K:Kulturpflanze / domestiziertes Tier",
94
            "N:Neophyt",
95
            "KF:Kultuflüchtling"
96
    });
97

    
98
    private static final List<String> GERMAN_FEDERAL_STATES = Arrays.asList(new String[] {
99
            "DE:Deutschland",
100
            "BW:Baden-Württemberg",
101
            "BY:Bayern",
102
            "BE:Berlin",
103
            "BB:Brandenburg",
104
            "HB:Bremen",
105
            "HH:Hamburg",
106
            "HE:Hessen",
107
            "MV:Mecklenburg-Vorpommern",
108
            "NI:Niedersachsen",
109
            "NW:Nordrhein-Westfalen",
110
            "RP:Rheinland-Pfalz",
111
            "SL:Saarland",
112
            "SN:Sachsen",
113
            "ST:Sachsen-Anhalt",
114
            "SH:Schleswig-Holstein",
115
            "TH:Thüringen"
116
    });
117

    
118
    private static final List<String> GERMAN_COMBINED_STATES = Arrays.asList(new String[] {
119
            "BB+BE:Brandenburg und Berlin",
120
            "SH+HH:Schleswig-Holstein und Hamburg"
121
    });
122

    
123
    private static final List<String> GERMAN_MARINE_ALGAE_AREAS = Arrays.asList(new String[] {
124
            "HGL:Helgoland",
125
            "NIW:Niedersächsisches Wattenmeer",
126
            "SHW:Schleswig-Holsteinisches Wattenmeer",
127
            "SHO:Schleswig-Holsteinische Ostsee",
128
            "MVO:Mecklenburg-Vorpommerische Ostsee"
129
    });
130

    
131
    private static final List<String> GERMAN_MARINE_INVERTEBRATE_AREAS = Arrays.asList(new String[] {
132
            "ÄWN:Ästuarien und Watt Nordsee",
133
            "SuN:Sublitoral Nordsee",
134
            "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
135
            "Dog:Doggerbank",
136
            "Ost:Ostsee"
137
    });
138

    
139

    
140
    /** Hibernate classification vocabulary initialization strategy */
141
    private static final List<String> VOC_CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[] {
142
            "classification.$",
143
            "classification.rootNodes",
144
            "childNodes",
145
            "childNodes.taxon",
146
            "childNodes.taxon.name",
147
            "taxonNodes",
148
            "taxonNodes.taxon",
149
            "synonyms",
150
            "taxon.*",
151
            "taxon.sec",
152
            "taxon.name.*",
153
            "taxon.synonyms",
154
            "termVocabulary.*",
155
            "terms",
156
            "namedArea"
157
    });
158

    
159

    
160
    public BfnXmlImportAddtionalTerms(){
161
        super();
162
    }
163

    
164

    
165
	@Override
166
	public void doInvoke(BfnXmlImportState state){
167
		logger.info("create german terms ...");
168

    
169
		TransactionStatus tx = startTransaction();
170
		//areas
171
		createTerms(GERMAN_FEDERAL_STATES, Vocabulary.GERMAN_FEDERAL_STATES, TermType.NamedArea);
172
		createTerms(GERMAN_MARINE_ALGAE_AREAS, Vocabulary.GERMAN_MARINE_ALGAE_AREAS, TermType.NamedArea);
173
		createTerms(GERMAN_MARINE_INVERTEBRATE_AREAS, Vocabulary.GERMAN_MARINE_INVERTEBRATE_AREAS, TermType.NamedArea);
174
		createTerms(GERMAN_COMBINED_STATES, Vocabulary.GERMAN_COMBINED_STATES, TermType.NamedArea);
175
		//distribution status
176
        createTerms(GERMAN_PRESENCE_ABSENCE_TERMS, Vocabulary.GERMAN_PRESENCE_TERMS, TermType.PresenceAbsenceTerm);
177
		createTerms(GERMAN_ESTABLISHMENT_STATUS_TERMS, Vocabulary.GERMAN_ESTABLISHMENT_TERMS, TermType.PresenceAbsenceTerm);
178

    
179
		commitTransaction(tx);
180
		logger.info("end create german terms.");
181
		return;
182

    
183
	}
184

    
185
	private void createTerms(List<String> termList, Vocabulary vocabulary, TermType termType){
186
	   NamedArea parentGermany = null;
187
	   PresenceAbsenceTerm lastParent = null;
188
	   int id = 0;
189
       for(String strTerm : termList){
190
           //Split string into label and abbrevated label
191
           String[] splittedStrings = StringUtils.splitByWholeSeparator(strTerm, ":");
192
           String abbrevatedLabel = splittedStrings[0];
193
           String label = splittedStrings[1];
194
           //get UUID and load existing term
195
           UUID termUuuid = null;
196
           try {
197
               if(vocabulary == Vocabulary.GERMAN_PRESENCE_TERMS){
198
                   termUuuid = BfnXmlTransformer.getGermanAbsenceTermUUID(label);
199
               }else if(vocabulary == Vocabulary.GERMAN_ESTABLISHMENT_TERMS){
200
                   termUuuid = BfnXmlTransformer.getGermanEstablishmentTermUUID(label);
201
               }else if(termType == TermType.NamedArea){
202
                   termUuuid = BfnXmlTransformer.getAreaUUID(label);
203
               }
204
           } catch (UnknownCdmTypeException e) {
205
               logger.warn("Could not match term to uuid: "+e.toString());
206
               termUuuid = UUID.randomUUID();
207
           }
208

    
209
           @SuppressWarnings("rawtypes")
210
           DefinedTermBase term = getTermService().load(termUuuid);
211
           if(term != null){
212
               //already in the db, so no need to step through the whole process again.
213
               return;
214
           }else{
215
               if(termType == TermType.NamedArea){
216
                   //Namedareas
217
                   NamedArea namedArea = NamedArea.NewInstance(label, label, abbrevatedLabel);
218
                   term = namedArea;
219
                   term.setIdInVocabulary(Integer.toString(id));
220
                   if (vocabulary == Vocabulary.GERMAN_FEDERAL_STATES){
221
                       namedArea.setType(NamedAreaType.ADMINISTRATION_AREA());
222
                       if(label.equalsIgnoreCase("Deutschland")){
223
                           namedArea.setLevel(NamedAreaLevel.COUNTRY());
224
                           parentGermany = (NamedArea) term;
225
                       }else{
226
                           namedArea.setLevel(NamedAreaLevel.STATE());
227
                           term.setPartOf(parentGermany);
228
                       }
229
                   }else{
230
                       if (vocabulary == Vocabulary.GERMAN_COMBINED_STATES){
231
                           namedArea.setType(NamedAreaType.ADMINISTRATION_AREA());
232
                       }else{
233
                           namedArea.setType(NamedAreaType.NATURAL_AREA());
234
                       }
235
                   }
236
               }else{
237
                   term = PresenceAbsenceTerm.NewPresenceInstance(label, label, abbrevatedLabel);
238
                   term.setIdInVocabulary(abbrevatedLabel);
239
                   if(vocabulary.equals(Vocabulary.GERMAN_PRESENCE_TERMS)){
240
                       //create hierarchy of terms
241
                       if(abbrevatedLabel.length() >1 ){
242
                           ((PresenceAbsenceTerm)term).setPartOf(lastParent);
243
                       }else {
244
                           lastParent = (PresenceAbsenceTerm) term;
245
                       }
246
                   }
247
               }
248
               term.setUuid(termUuuid);
249
           }
250
           createOrUpdateTermVocabulary(termType, getVocabularyService(), term, vocabulary.toString());
251
           id++;
252
       }
253
	}
254

    
255

    
256
	/**
257
	 * @param vocabularyService
258
	 * @param term
259
	 * @param vocUUID
260
	 */
261
	@SuppressWarnings({ "unchecked", "rawtypes" })
262
	private OrderedTermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
263
		OrderedTermVocabulary termVocabulary = null;
264
		UUID vocUUID = null;
265
		try {
266
			vocUUID=BfnXmlTransformer.getRedlistVocabularyUUID(strTermVocabulary);
267
		} catch (UnknownCdmTypeException e) {
268
			// TODO Auto-generated catch block
269
			e.printStackTrace();
270
		}
271
		if(vocUUID != null){
272
			termVocabulary = (OrderedTermVocabulary) vocabularyService.load(vocUUID);
273
		}
274
		//lookup via String in case uuid lookup does not work
275
		if(termVocabulary == null && vocUUID == null){
276
			List<TermVocabulary> vocList = vocabularyService.list(TermVocabulary.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY);
277
			for(TermVocabulary tv : vocList){
278
				if(tv.getTitleCache().equalsIgnoreCase(strTermVocabulary)){
279
					termVocabulary = (OrderedTermVocabulary) tv;
280
				}
281
			}
282
		}
283
		//create termvocabulary
284
		if(termVocabulary == null){
285
			termVocabulary = OrderedTermVocabulary.NewInstance(termType, strTermVocabulary, strTermVocabulary, strTermVocabulary, null);
286
			if(vocUUID != null){
287
				termVocabulary.setUuid(vocUUID);
288
			}
289
		}
290
		termVocabulary.addTerm((OrderedTermBase) term);
291
		vocabularyService.saveOrUpdate(termVocabulary);
292

    
293
		return termVocabulary;
294
	}
295

    
296
	@Override
297
    protected boolean isIgnore(BfnXmlImportState state){
298
		return ! state.getConfig().isDoAdditionalTerms();
299
	}
300

    
301

    
302

    
303

    
304
    @Override
305
    public boolean doCheck(BfnXmlImportState state){
306
        boolean result = true;
307
        //TODO needs to be implemented
308
        return result;
309
    }
310
}
(1-1/9)