Project

General

Profile

Download (3.55 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2016 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.greece;
10

    
11
import java.util.Arrays;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Set;
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.io.mexico.SimpleExcelTaxonImportState;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.description.Feature;
23
import eu.etaxonomy.cdm.model.description.TaxonDescription;
24
import eu.etaxonomy.cdm.model.description.TextData;
25
import eu.etaxonomy.cdm.model.taxon.Taxon;
26

    
27
/**
28
 * Import for the Flora Hellenica taxon comments.
29
 *
30
 * @author a.mueller
31
 * @date 14.12.2016
32
 */
33

    
34
@Component
35
public class FloraHellenicaCommentsImport<CONFIG extends FloraHellenicaImportConfigurator>
36
        extends FloraHellenicaImportBase<CONFIG>{
37

    
38
    private static final long serialVersionUID = -3565782012921316901L;
39
    private static final Logger logger = Logger.getLogger(FloraHellenicaCommentsImport.class);
40

    
41
    private static final String TAXON = "Taxon";
42
    private static final String UNIQUE_ID_ACCEPTED = "Unique ID of taxon name (Includes valid and excluded taxa IDs)";
43
    private static final String COMMENT = "Comment";
44

    
45

    
46
   private  static List<String> expectedKeys= Arrays.asList(new String[]{
47
            UNIQUE_ID_ACCEPTED,
48
            TAXON,
49
            COMMENT
50
    });
51

    
52
    @Override
53
    protected String getWorksheetName() {
54
        return "comments";
55
    }
56

    
57
    private boolean isFirst = true;
58
    private TransactionStatus tx = null;
59
    /**
60
     * {@inheritDoc}
61
     */
62
    @Override
63
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
64
        if (isFirst){
65
            tx = this.startTransaction();
66
            isFirst = false;
67
        }
68

    
69
        String line = state.getCurrentLine() + ": ";
70
        HashMap<String, String> record = state.getOriginalRecord();
71

    
72
        Set<String> keys = record.keySet();
73
        for (String key: keys) {
74
            if (! expectedKeys.contains(key)){
75
                logger.warn(line + "Unexpected Key: " + key);
76
            }
77
        }
78

    
79
        String noStr = getValue(record, "Unique ID");
80
        makeComment(state, line, record, noStr);
81
    }
82

    
83
    @Override
84
    protected void secondPass(SimpleExcelTaxonImportState<CONFIG> state) {
85
        if (tx != null){
86
            this.commitTransaction(tx);
87
            tx = null;
88
        }
89
    }
90

    
91

    
92
    /**
93
     * @param state
94
     * @param line
95
     * @param record
96
     * @param noStr
97
     * @return
98
     */
99
    private void makeComment(SimpleExcelTaxonImportState<CONFIG> state, String line,
100
            HashMap<String, String> record,
101
            String noStr) {
102

    
103
        Taxon acceptedTaxon = getAcceptedTaxon(record, state, UNIQUE_ID_ACCEPTED);
104
        if (acceptedTaxon == null){
105
            logger.warn(line + "Accepted not found: " + record.get(UNIQUE_ID_ACCEPTED));
106
            return;
107
        }
108

    
109
        String commentStr = getValue(record, COMMENT);
110

    
111
        TaxonDescription desc = getTaxonDescription(acceptedTaxon);
112
        TextData comment = TextData.NewInstance(Feature.NOTES(), commentStr, Language.ENGLISH(), null);
113
        desc.addElement(comment);
114
        comment.addImportSource(noStr, getWorksheetName(), getSourceCitation(state), null);
115
        getTaxonService().saveOrUpdate(acceptedTaxon);
116
        return ;
117
    }
118

    
119
}
(1-1/7)