Project

General

Profile

Download (7.28 KB) Statistics
| Branch: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.edaphobase;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32

    
33
/**
34
 * @author a.mueller
35
 * @date 21.12.2015
36
 *
37
 */
38
@Component
39
public class EdaphobaseClassificationImport extends EdaphobaseImportBase {
40
    private static final long serialVersionUID = -9138378836474086070L;
41

    
42
    private static final Logger logger = Logger.getLogger(EdaphobaseClassificationImport.class);
43

    
44
    private static final String tableName = "tax_taxon";
45

    
46
    private static final String pluralString = "taxon relationships";
47

    
48

    
49
    /**
50
     * @param tableName
51
     * @param pluralString
52
     */
53
    public EdaphobaseClassificationImport() {
54
        super(tableName, pluralString);
55
    }
56

    
57
    @Override
58
    protected String getIdQuery(EdaphobaseImportState state) {
59
        return "SELECT taxon_id FROM "
60
                + " (SELECT DISTINCT taxon_id, length(path_to_root) FROM tax_taxon t "
61
                + " ORDER BY length(path_to_root), taxon_id) as drvTbl ";
62
    }
63

    
64
    @Override
65
    protected String getRecordQuery(EdaphobaseImportConfigurator config) {
66
        String result = "SELECT DISTINCT t.* "
67
                + " FROM tax_taxon t"
68
                + " WHERE taxon_id IN (@IDSET)";
69
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
70
        return result;
71
    }
72

    
73
    @Override
74
    protected void doInvoke(EdaphobaseImportState state) {
75
        makeClassification(state);
76
        super.doInvoke(state);
77
    }
78

    
79

    
80
    @Override
81
    public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
82
        ResultSet rs = partitioner.getResultSet();
83
        Map<String, Classification> map = partitioner.getObjectMap(CLASSIFICATION_NAMESPACE);
84
        Classification classification = map.get(state.getConfig().getClassificationUuid().toString());
85
        Reference<?> sourceReference = state.getTransactionalSourceReference();
86

    
87
        Set<TaxonBase> taxaToSave = new HashSet<>();
88
        try {
89
            while (rs.next()){
90
                Integer id = rs.getInt("taxon_id");
91
                 //parentTaxonFk
92
                boolean isValid = rs.getBoolean("valid");
93
//                boolean idDeleted = rs.getBoolean("deleted");
94
//                String treeIndex = rs.getString("path_to_root");
95
//                Integer rankFk = rs.getInt("tax_rank_fk");
96
//                String officialRemark = rs.getString("official_remark");
97
//                boolean isGroup = rs.getBoolean("taxonomic_group");
98
                Integer parentTaxonFk = nullSafeInt(rs, "parent_taxon_fk");
99

    
100
                if (id != null && parentTaxonFk != null){
101
                    if (isValid){
102
                        Taxon child = state.getRelatedObject(TAXON_NAMESPACE, id.toString(), Taxon.class);
103
                        Taxon parent = state.getRelatedObject(TAXON_NAMESPACE, parentTaxonFk.toString(), Taxon.class);
104
                        if (parent != null){
105
                            classification.addParentChild(parent, child, sourceReference, null);
106
                        }else{
107
                            logger.warn("Parent taxon " + parentTaxonFk + " not found for taxon " + id );
108
                        }
109
                    }else{
110
                        Synonym synonym = state.getRelatedObject(TAXON_NAMESPACE, id.toString(), Synonym.class);
111
                        if (synonym == null){
112
                            logger.warn("Synonym " + id + " not found for taxon ");
113
                        }
114
                        Taxon accepted = state.getRelatedObject(TAXON_NAMESPACE, parentTaxonFk.toString(), Taxon.class);
115
                        if (accepted != null){
116
                            accepted.addSynonym(synonym, SynonymRelationshipType.SYNONYM_OF());
117
                        }else{
118
                            logger.warn("Accepted(parent) taxon " + parentTaxonFk + " not found for taxon " + id );
119
                        }
120
                    }
121

    
122
                }
123

    
124
//              //id
125
//              String nameSpace = "tax_taxon";
126
//              ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
127
//              ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
128

    
129

    
130
            }
131
        } catch (SQLException e) {
132
            // TODO Auto-generated catch block
133
            e.printStackTrace();
134
        }
135

    
136
        getTaxonService().saveOrUpdate(taxaToSave);
137
        return true;
138
    }
139

    
140
    @Override
141
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
142
            EdaphobaseImportState state) {
143
        String nameSpace;
144
        Class<?> cdmClass;
145
        Set<String> idSet;
146
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
147

    
148
        try{
149
            Set<String> taxonIdSet = new HashSet<String>();
150
            while (rs.next()){
151
                handleForeignKey(rs, taxonIdSet, "taxon_id");
152
                handleForeignKey(rs, taxonIdSet, "parent_taxon_fk");
153
            }
154

    
155
            //name map
156
            nameSpace = TAXON_NAMESPACE;
157
            cdmClass = TaxonBase.class;
158
            idSet = taxonIdSet;
159
            Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
160
            result.put(nameSpace, taxonMap);
161

    
162
            //Classification
163
            Map<String, Classification> classificationMap = new HashMap<>();
164
            UUID classificationUuid = state.getConfig().getClassificationUuid();
165
            Classification classification = getClassificationService().find(state.getConfig().getClassificationUuid());
166
            classificationMap.put(classificationUuid.toString(), classification);
167
            result.put(CLASSIFICATION_NAMESPACE, classificationMap);
168

    
169
        } catch (SQLException e) {
170
            throw new RuntimeException(e);
171
        }
172

    
173
        return result;
174
    }
175

    
176

    
177
    /**
178
     * @param state
179
     */
180
    private void makeClassification(EdaphobaseImportState state) {
181
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
182
        classification.setUuid(state.getConfig().getClassificationUuid());
183
        getClassificationService().save(classification);
184
    }
185

    
186

    
187
    @Override
188
    protected boolean doCheck(EdaphobaseImportState state) {
189
        return false;
190
    }
191

    
192
    @Override
193
    protected boolean isIgnore(EdaphobaseImportState state) {
194
        return ! state.getConfig().isDoTaxa();
195
    }
196

    
197
}
(2-2/8)