Project

General

Profile

Revision 9bfdc83a

ID9bfdc83ac269d0245ce1d8ff77f9d78eda0a1164
Parent 815a9015
Child 6793f949

Added by Katja Luther over 2 years ago

simple taxon list import

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/ExcelListRow.java
1
/**
2
* Copyright (C) 2018 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.excel.taxa;
10

  
11
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase;
12

  
13
/**
14
 * @author k.luther
15
 * @date 21.02.2018
16
 *
17
 */
18
public class ExcelListRow extends ExcelRowBase {
19

  
20
    String name;
21
    String correctNameifSynonym;
22

  
23

  
24
    public ExcelListRow(){
25

  
26
    }
27

  
28

  
29
    /**
30
     * @return the name
31
     */
32
    public String getName() {
33
        return name;
34
    }
35

  
36

  
37
    /**
38
     * @param name the name to set
39
     */
40
    public void setName(String name) {
41
        this.name = name;
42
    }
43

  
44

  
45
    /**
46
     * @return the correctNameifSynonym
47
     */
48
    public String getCorrectNameifSynonym() {
49
        return correctNameifSynonym;
50
    }
51

  
52

  
53
    /**
54
     * @param correctNameifSynonym the correctNameifSynonym to set
55
     */
56
    public void setCorrectNameifSynonym(String correctNameifSynonym) {
57
        this.correctNameifSynonym = correctNameifSynonym;
58
    }
59

  
60
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/TaxonListImport.java
1
/**
2
* Copyright (C) 2018 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.excel.taxa;
10

  
11
import org.codehaus.plexus.util.StringUtils;
12

  
13
import eu.etaxonomy.cdm.model.name.TaxonName;
14
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
15
import eu.etaxonomy.cdm.model.reference.Reference;
16
import eu.etaxonomy.cdm.model.taxon.Classification;
17
import eu.etaxonomy.cdm.model.taxon.Synonym;
18
import eu.etaxonomy.cdm.model.taxon.Taxon;
19
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
20

  
21
/**
22
 * @author k.luther
23
 * @date 19.02.2018
24
 *
25
 */
26
public class TaxonListImport extends TaxonExcelImportBase {
27

  
28
    private static final long serialVersionUID = 515631363871257717L;
29

  
30
    final String nameColumn = "Name";
31
    final String correctNameColumn = "Correct name if synonym";
32

  
33
    /**
34
     * {@inheritDoc}
35
     */
36
    @Override
37
    protected ExcelListRow createDataHolderRow() {
38

  
39
        return new ExcelListRow();
40
    }
41

  
42
    /**
43
     * {@inheritDoc}
44
     */
45
    @Override
46
    protected void analyzeSingleValue(
47
            KeyValue keyValue,
48
            TaxonExcelImportState state) {
49
        ExcelListRow excelRow = (ExcelListRow)state.getCurrentRow();
50
        String key = keyValue.key;
51
        String value = keyValue.value;
52
        Integer index = keyValue.index;
53

  
54
        if (key.equalsIgnoreCase(nameColumn)) {
55
            excelRow.setName(value);
56
        } else if(key.equalsIgnoreCase(correctNameColumn) ) {
57
            excelRow.setCorrectNameifSynonym(value);
58
        }
59
    }
60

  
61
    /**
62
     * {@inheritDoc}
63
     */
64
    @Override
65
    protected void firstPass(TaxonExcelImportState state) {
66

  
67
        //first import all names
68
        ExcelListRow taxonDataHolder = (ExcelListRow)state.getCurrentRow();
69
        TaxonName name = TaxonNameFactory.PARSED_BOTANICAL(taxonDataHolder.getName());
70
        name = getNameService().save(name);
71

  
72
        taxonDataHolder.setCdmUuid(name.getUuid());
73
        state.putName(taxonDataHolder.getName(), name);
74

  
75
        //import all taxa -> column correct name if synonym is empty
76

  
77
        if (StringUtils.isBlank(taxonDataHolder.getCorrectNameifSynonym() )){
78
            Taxon taxon = Taxon.NewInstance(state.getNameMap().get(taxonDataHolder.name), null);
79
            taxon = (Taxon) getTaxonService().save(taxon);
80
            state.putTaxon(name.getNameCache(), taxon);
81

  
82
        }
83
        return;
84
    }
85

  
86
    /**
87
     * {@inheritDoc}
88
     */
89
    @Override
90
    protected void secondPass(TaxonExcelImportState state) {
91
       //import all synonyms -> column correct name if synonym contains name
92
        ExcelListRow taxonDataHolder = (ExcelListRow)state.getCurrentRow();
93

  
94
        if (!StringUtils.isBlank(taxonDataHolder.getCorrectNameifSynonym() )){
95
            Taxon taxon = (Taxon)state.getTaxonBase(taxonDataHolder.correctNameifSynonym);
96
            Synonym syn = taxon.addHeterotypicSynonymName(state.getNameMap().get(taxonDataHolder.name));
97
            getTaxonService().saveOrUpdate(taxon);
98
//            state.putTaxon(nameColumn, syn);
99
        }else {
100
            //create parent child relationship
101
            TaxonBase taxonBase = state.getTaxonMap().get(taxonDataHolder.name);
102
            if (taxonBase instanceof Taxon){
103
                Taxon taxon = (Taxon) taxonBase;
104
                if (taxon.getName().isGenus()){
105
                    makeParent(state, null, taxon);
106
                }else{
107
                    TaxonBase parent =  state.getTaxonMap().get(taxon.getName().getGenusOrUninomial());
108
                    if (parent instanceof Taxon){
109
                        Taxon parentTaxon = (Taxon)parent;
110
                        makeParent(state, parentTaxon, taxon);
111
                    }
112
                }
113
            }
114
        }
115

  
116

  
117

  
118

  
119

  
120

  
121
    }
122

  
123
    private void makeParent(TaxonExcelImportState state, Taxon parentTaxon, Taxon childTaxon){
124
        Reference sec = state.getConfig().getSourceReference();
125

  
126
        Classification tree = state.getClassification();
127
        if (tree == null){
128
            if (state.getConfig().getClassificationUuid() != null){
129
                tree = getClassificationService().load(state.getConfig().getClassificationUuid());
130
                state.setClassification(tree);
131
            }
132
            if (tree == null){
133
                tree = makeTree(state, sec);
134
                getClassificationService().save(tree);
135
                state.setClassification(tree);
136
            }
137
        }
138
        boolean success;
139
        if (parentTaxon == null){
140
            success =  (null !=  tree.addChildTaxon(childTaxon, null, null));
141
        }else{
142
            success =  (null !=  tree.addParentChild(parentTaxon, childTaxon, null, null));
143
        }
144
        if (success == false){
145
            state.setUnsuccessfull();
146
        }
147

  
148
        return;
149
    }
150

  
151
    /**
152
     * {@inheritDoc}
153
     */
154
    @Override
155
    protected boolean isIgnore(TaxonExcelImportState state) {
156
        // TODO Auto-generated method stub
157
        return false;
158
    }
159

  
160

  
161

  
162
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/TaxonListImportConfigurator.java
1
/**
2
* Copyright (C) 2018 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.excel.taxa;
10

  
11
import java.net.URI;
12

  
13
import eu.etaxonomy.cdm.database.ICdmDataSource;
14
import eu.etaxonomy.cdm.io.common.ImportStateBase;
15
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
16

  
17
/**
18
 * @author k.luther
19
 * @date 21.02.2018
20
 *
21
 */
22
public class TaxonListImportConfigurator extends ExcelImportConfiguratorBase {
23

  
24
    /**
25
     * @param uri
26
     * @param destination
27
     */
28
    protected TaxonListImportConfigurator(URI uri, ICdmDataSource destination) {
29
        super(uri, destination);
30

  
31
    }
32

  
33
    /**
34
     * {@inheritDoc}
35
     */
36
    @Override
37
    public <STATE extends ImportStateBase> STATE getNewState() {
38
        // TODO Auto-generated method stub
39
        return null;
40
    }
41

  
42
    /**
43
     * {@inheritDoc}
44
     */
45
    @Override
46
    protected void makeIoClassList() {
47
        // TODO Auto-generated method stub
48

  
49
    }
50

  
51
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)