Project

General

Profile

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

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

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

    
22
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
23
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
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.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31

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

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

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

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

    
47

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

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

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

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

    
78

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

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

    
99
                if (parentTaxonFk != null){
100
                    TaxonBase<?> parent = state.getRelatedObject(TAXON_NAMESPACE, parentTaxonFk.toString(), TaxonBase.class);
101
                    if (parent == null){
102
                        logger.warn("Parent taxon " + parentTaxonFk + " not found for taxon " + id );
103
                    }else{
104

    
105
                        TaxonName parentName = parent.getName();
106

    
107
                        TaxonBase<?> child = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(id), TaxonBase.class);
108
//                        TaxonName childName = child.getName();
109

    
110
//                        handleMissingNameParts(CdmBase.deproxy(childName, TaxonName.class), CdmBase.deproxy(parentName, NonViralName.class));
111

    
112
                        if (isValid){
113
                            if (parent.isInstanceOf(Synonym.class)){
114
                                logger.warn("Parent taxon (" + parentTaxonFk + " is not valid for valid child " + id);
115
                            }else{
116
                                Taxon accParent = CdmBase.deproxy(parent, Taxon.class);
117
                                classification.addParentChild(accParent, (Taxon)child, sourceReference, null);
118
                                taxaToSave.add(accParent);
119
                            }
120
                        }else{
121
//                            Synonym synonym = CdmBase.deproxy(child, Synonym.class);
122
//                            if (synonym == null){
123
//                                logger.warn("Synonym " + id + " not found for taxon ");
124
//                            }
125
//                            if(parent.isInstanceOf(Synonym.class)){
126
//                                String message = "Taxon ("+parentTaxonFk+") is not accepted but synonym. Can't add synonym ("+id+")";
127
//                                logger.warn(message);
128
//                            }else{
129
//                                Taxon accepted = CdmBase.deproxy(parent, Taxon.class);
130
////                                accepted.addSynonym(synonym, SynonymType.SYNONYM_OF());
131
//                                taxaToSave.add(accepted);
132
//                            }
133
                        }
134
                    }
135
                }
136

    
137
//              //id
138
//              String nameSpace = "tax_taxon";
139
//              ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
140
//              ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
141

    
142

    
143
            }
144
        } catch (SQLException e) {
145
            e.printStackTrace();
146
        }
147

    
148
        getTaxonService().saveOrUpdate(taxaToSave);
149
        return true;
150
    }
151

    
152
//    /**
153
//     * @param childName
154
//     * @param parentName
155
//     */
156
//    private void handleMissingNameParts(INonViralName childName, INonViralName parentName) {
157
//        if (childName.getGenusOrUninomial())
158
//    }
159

    
160
    @Override
161
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
162
            EdaphobaseImportState state) {
163
        String nameSpace;
164
        Class<?> cdmClass;
165
        Set<String> idSet;
166
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
167

    
168
        try{
169
            Set<String> taxonIdSet = new HashSet<String>();
170
            while (rs.next()){
171
                handleForeignKey(rs, taxonIdSet, "taxon_id");
172
                handleForeignKey(rs, taxonIdSet, "parent_taxon_fk");
173
            }
174

    
175
            //name map
176
            nameSpace = TAXON_NAMESPACE;
177
            cdmClass = TaxonBase.class;
178
            idSet = taxonIdSet;
179
            Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
180
            result.put(nameSpace, taxonMap);
181

    
182
            //Classification
183
            Map<String, Classification> classificationMap = new HashMap<>();
184
            UUID classificationUuid = state.getConfig().getClassificationUuid();
185
            Classification classification = getClassificationService().find(state.getConfig().getClassificationUuid());
186
            classificationMap.put(classificationUuid.toString(), classification);
187
            result.put(CLASSIFICATION_NAMESPACE, classificationMap);
188

    
189
        } catch (SQLException e) {
190
            throw new RuntimeException(e);
191
        }
192

    
193
        return result;
194
    }
195

    
196

    
197
    /**
198
     * @param state
199
     */
200
    private void makeClassification(EdaphobaseImportState state) {
201
        Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
202
        classification.setUuid(state.getConfig().getClassificationUuid());
203
        getClassificationService().save(classification);
204
    }
205

    
206

    
207
    @Override
208
    protected boolean doCheck(EdaphobaseImportState state) {
209
        return false;
210
    }
211

    
212
    @Override
213
    protected boolean isIgnore(EdaphobaseImportState state) {
214
        return ! state.getConfig().isDoTaxa();
215
    }
216

    
217
}
(2-2/11)