Project

General

Profile

Download (3.22 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.common.CdmUtils;
19
import eu.etaxonomy.cdm.io.csv.in.CsvImportBase;
20
import eu.etaxonomy.cdm.model.common.Annotation;
21
import eu.etaxonomy.cdm.model.common.AnnotationType;
22
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.description.State;
25
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
26
import eu.etaxonomy.cdm.model.term.TermVocabulary;
27

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

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

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

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

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

    
57
        State stateTerm = State.NewInstance(currentRecord.get(HEADER_DEFINITION), termLabel, null);
58
        stateTerm.setIdInVocabulary(termLabel);
59
        stateTerm.setUri(URI.create(currentRecord.get(HEADER_URI)));
60
        if(CdmUtils.isNotBlank(currentRecord.get(HEADER_NOTES))){
61
            stateTerm.addAnnotation(Annotation.NewInstance(currentRecord.get(HEADER_NOTES), AnnotationType.EDITORIAL(), Language.ENGLISH()));
62
        }
63

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

    
77
        IdentifiableSource source = IdentifiableSource.NewInstance(OriginalSourceType.Import, importState.getCitation().getTitle(), null, importState.getCitation(), null);
78
        source.setIdInSource(termLabel);
79
        stateTerm.addSource(source);
80

    
81
        getVocabularyService().saveOrUpdate(vocabulary);
82
        getTermService().saveOrUpdate(stateTerm);
83
    }
84

    
85
}
(6-6/6)