Project

General

Profile

Download (3.1 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2017 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
package eu.etaxonomy.cdm.io.plantglossary;
10

    
11
import java.io.File;
12
import java.net.URI;
13
import java.util.Map;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.stereotype.Component;
17

    
18
import eu.etaxonomy.cdm.io.csv.in.CsvImportBase;
19
import eu.etaxonomy.cdm.model.common.Annotation;
20
import eu.etaxonomy.cdm.model.common.AnnotationType;
21
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
22
import eu.etaxonomy.cdm.model.common.Language;
23
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
24
import eu.etaxonomy.cdm.model.common.TermVocabulary;
25
import eu.etaxonomy.cdm.model.description.State;
26

    
27
/**
28
 *
29
 * @author pplitzner
30
 * @since Dec 7, 2018
31
 *
32
 */
33
@Component
34
public class PlantGlossaryStateImport extends CsvImportBase<PlantGlossaryCsvImportConfigurator, PlantGlossaryCsvImportState, File>{
35
    private static final long serialVersionUID = -5600766240192189822L;
36
    private static Logger logger = Logger.getLogger(PlantGlossaryStateImport.class);
37

    
38
    final String HEADER_LABEL = "dcterms:identifier";
39
    final String HEADER_DEFINITION = "definition";
40
    final String HEADER_CATEGORY = "vann:termGroup";
41
    final String HEADER_NOTES = "skos:example";
42
    final String SOURCE_HEADER = "sourceDataset";
43
    final String HEADER_URI = "term_URI";
44

    
45
    @Override
46
    protected void handleSingleLine(PlantGlossaryCsvImportState importState) {
47

    
48
        Map<String, String> currentRecord = importState.getCurrentRecord();
49

    
50
        String termLabel = currentRecord.get(HEADER_LABEL);
51
        //check if already present
52
        if(importState.isTermPresent(termLabel, getTermService())){
53
            return;
54
        }
55

    
56
        State stateTerm = State.NewInstance(currentRecord.get(HEADER_DEFINITION), termLabel, null);
57
        stateTerm.setIdInVocabulary(termLabel);
58
        stateTerm.setUri(URI.create(currentRecord.get(HEADER_URI)));
59
        stateTerm.addAnnotation(Annotation.NewInstance(currentRecord.get(HEADER_NOTES), AnnotationType.EDITORIAL(), Language.ENGLISH()));
60

    
61
        String vocName = currentRecord.get(HEADER_CATEGORY);
62
        // TODO how should we handle multiple possible categories?
63
        // for now we just take the first one
64
        if(vocName.contains(",")){
65
            vocName = vocName.split(",")[0];
66
        }
67
        TermVocabulary vocabulary = importState.checkVocabularies(vocName, getVocabularyService());
68
        if(vocabulary==null){
69
            logger.error("No vocabulary found for term: "+stateTerm+" with vocName: "+vocName);
70
            return;
71
        }
72
        vocabulary.addTerm(stateTerm);
73

    
74
        IdentifiableSource source = IdentifiableSource.NewInstance(OriginalSourceType.Import, importState.getCitation().getTitle(), null, importState.getCitation(), null);
75
        source.setIdInSource(termLabel);
76
        stateTerm.addSource(source);
77

    
78
        getVocabularyService().saveOrUpdate(vocabulary);
79
        getTermService().saveOrUpdate(stateTerm);
80
    }
81

    
82
}
(5-5/5)