Project

General

Profile

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

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

    
21
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
22
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25

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

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

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

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

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

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

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

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

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

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

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

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

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

    
126
        return result;
127
    }
128

    
129

    
130

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

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

    
141
}
(8-8/12)