Project

General

Profile

Download (4.71 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

    
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.reference.Reference;
26

    
27
/**
28
 * @author a.mueller
29
 * @date 18.12.2015
30
 *
31
 */
32
@Component
33
public class EdaphobaseInReferenceImport extends EdaphobaseImportBase {
34
    private static final long serialVersionUID = 6895687693249076160L;
35

    
36
    private static final Logger logger = Logger.getLogger(EdaphobaseInReferenceImport.class);
37

    
38
    private static final String tableName = "lit_document";
39

    
40
    private static final String pluralString = "documents";
41

    
42
    /**
43
     * @param tableName
44
     * @param pluralString
45
     */
46
    public EdaphobaseInReferenceImport() {
47
        super(tableName, pluralString);
48
    }
49

    
50
    @Override
51
    protected String getIdQuery(EdaphobaseImportState state) {
52
        return    " SELECT DISTINCT document_id "
53
                + " FROM lit_document ld INNER JOIN tax_taxon t ON t.tax_document = ld.document_id "
54
                + " WHERE ld.parent_document_fk_document_id IS NOT NULL "
55
                + " ORDER BY document_id ";
56
    }
57

    
58
    @Override
59
    protected String getRecordQuery(EdaphobaseImportConfigurator config) {
60
        String result = " SELECT document_id, parent_document_fk_document_id "
61
                + " FROM lit_document ld "
62
                + " WHERE ld.document_id IN (@IDSET)";
63
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
64
        return result;
65
    }
66

    
67
    @Override
68
    public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
69
        ResultSet rs = partitioner.getResultSet();
70
        Set<Reference> referencesToSave = new HashSet<>();
71
        try {
72
            while (rs.next()){
73
                handleSingleReference(state, rs, referencesToSave);
74
            }
75
        } catch (SQLException e) {
76
            e.printStackTrace();
77
        }
78
        getReferenceService().saveOrUpdate(referencesToSave);
79
        return true;
80
    }
81

    
82
    /**
83
     * @param state
84
     * @param rs
85
     * @param referencesToSave
86
     * @throws SQLException
87
     */
88
    private void handleSingleReference(EdaphobaseImportState state, ResultSet rs, Set<Reference> referencesToSave) throws SQLException {
89
        Integer id = rs.getInt("document_id");
90
        Integer parentId = rs.getInt("parent_document_fk_document_id");
91
//        Integer documentType = nullSafeInt(rs, "document_type");
92

    
93
        Reference<?> child = state.getRelatedObject(REFERENCE_NAMESPACE, String.valueOf(id), Reference.class);
94
        Reference<?> parent = state.getRelatedObject(REFERENCE_NAMESPACE, String.valueOf(parentId), Reference.class);
95
        if (child == null){
96
            logger.warn("Child reference for document_id " + id + " is NULL" );
97
        }else if (parent == null){
98
            logger.warn("Parent reference for document_type_fk_document_type_id " + parentId + " is NULL" );
99
        }else{
100
            child.setInReference(parent);
101
            referencesToSave.add(child);
102
        }
103
    }
104

    
105
    @Override
106
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
107
            EdaphobaseImportState state) {
108
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
109
        Set<String> referenceIdSet = new HashSet<String>();
110

    
111
        try {
112
            while (rs.next()){
113
                handleForeignKey(rs, referenceIdSet, "document_id");
114
                handleForeignKey(rs, referenceIdSet, "parent_document_fk_document_id");
115
            }
116
        } catch (SQLException e) {
117
            e.printStackTrace();
118
        }
119

    
120
        //reference map
121
        String nameSpace = REFERENCE_NAMESPACE;
122
        Class<?> cdmClass = Reference.class;
123
        Set<String> idSet = referenceIdSet;
124
        Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
125
        result.put(nameSpace, referenceMap);
126

    
127
        return result;
128
    }
129

    
130

    
131

    
132
    @Override
133
    protected boolean doCheck(EdaphobaseImportState state) {
134
        return false;
135
    }
136

    
137
    @Override
138
    protected boolean isIgnore(EdaphobaseImportState state) {
139
        return ! state.getConfig().isDoReferences();
140
    }
141

    
142
}
(7-7/10)