Project

General

Profile

Download (8.67 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.commons.lang3.StringUtils;
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.common.DOI;
23
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24
import eu.etaxonomy.cdm.io.common.ImportHelper;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.TimePeriod;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30

    
31
/**
32
 * @author a.mueller
33
 * @date 18.12.2015
34
 *
35
 */
36
@Component
37
public class EdaphobaseReferenceImport extends EdaphobaseImportBase {
38
    private static final long serialVersionUID = 6895687693249076160L;
39

    
40
    private static final Logger logger = Logger.getLogger(EdaphobaseReferenceImport.class);
41

    
42
    private static final String tableName = "lit_document";
43

    
44
    private static final String pluralString = "documents";
45

    
46

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

    
55
    @Override
56
    protected String getIdQuery(EdaphobaseImportState state) {
57
        return    " SELECT DISTINCT document_id "
58
                + " FROM lit_document ld INNER JOIN tax_taxon t ON t.tax_document = ld.document_id "
59
                + " UNION "
60
                + " SELECT DISTINCT pd.document_id "
61
                + " FROM lit_document ld INNER JOIN tax_taxon t ON t.tax_document = ld.document_id "
62
                + " INNER JOIN lit_document pd ON pd.document_id = ld.parent_document_fk_document_id "
63
                + " ORDER BY document_id ";
64
    }
65

    
66
    @Override
67
    protected String getRecordQuery(EdaphobaseImportConfigurator config) {
68
        String result = " SELECT * "
69
                + " FROM lit_document ld "
70
                + " WHERE ld.document_id IN (@IDSET)";
71
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
72
        return result;
73
    }
74

    
75
    @Override
76
    public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
77
        ResultSet rs = partitioner.getResultSet();
78
        Set<Reference> referencesToSave = new HashSet<>();
79
        try {
80
            while (rs.next()){
81
                handleSingleReference(state, rs, referencesToSave);
82
            }
83
        } catch (SQLException e) {
84
            e.printStackTrace();
85
        }
86

    
87
        getReferenceService().saveOrUpdate(referencesToSave);
88

    
89
        return true;
90
    }
91

    
92
    /**
93
     * @param state
94
     * @param rs
95
     * @param referencesToSave
96
     * @throws SQLException
97
     */
98
    private void handleSingleReference(EdaphobaseImportState state, ResultSet rs, Set<Reference> referencesToSave) throws SQLException {
99
        Integer id = nullSafeInt(rs, "document_id");
100
        String dtype = rs.getString("dtype");
101
        String issue = rs.getString("issue");
102
        String orderer = rs.getString("orderer");
103
        String place = rs.getString("place");
104
        Integer pageFrom = nullSafeInt(rs, "page_from");
105
        Integer pageTo = nullSafeInt(rs, "page_to");
106
        String subtitle = rs.getString("subtitle");
107
        Integer year = nullSafeInt(rs, "year");
108
        String isbn = rs.getString("isbn");
109
        //refers_to_literature
110
        //refers_to_collection
111
        //refers_to_observation
112
        String remark = rs.getString("remark");
113
        String volume = rs.getString("volume");
114
        //abbreviation (no record)
115
        String title = rs.getString("title");
116
        String issn = rs.getString("issn");
117
        //circulation //2 records
118
        String keywords = rs.getString("keywords");
119
        String abstractt = rs.getString("abstract");
120
        String parallel_title = rs.getString("parallel_title");
121
        //language_fk_language_id
122
        //document_type_fk_document_type_id
123
        //editor_fk_person_id
124
        Integer editorFk = nullSafeInt(rs, "editor_fk_person_id");
125

    
126
//        Integer parentFk = nullSafeInt(rs, "parent_document_fk_document_id");
127
        //publisher_fk_publisher_id
128
        //deleted
129
        //chapter_no
130
        //versionfield
131
        String doi = rs.getString("doi");
132
        String displayString = rs.getString("display_string");
133
        //aquisistion_date, aquisition_type, adoption_date, ex_colletion, barcode_prefix, barcode_org_prefix
134
        //barcode_type, collection_status, barcode, typus_form,
135

    
136
        //taxon_for_scope, taxon_is_scope
137
        //language_fk, document_type_backup
138

    
139
        Integer documentType = nullSafeInt(rs, "document_type");
140
        //normalized_title, normalized_abk_official_remark
141

    
142
        Reference ref = makeReferenceType(documentType, dtype);
143
        ref.setTitle(title);
144
        ref.setPlacePublished(place);
145
        ref.setIssn(issn);
146
        ref.setIsbn(isbn);
147
        if (pageFrom != null || pageTo != null){
148
            String pageStr = pageFrom == null ? "" : String.valueOf(pageFrom);
149
            pageStr = pageTo == null ? pageStr : "-" + pageTo;
150
            ref.setPages(pageStr);
151
        }
152
        if (year != null){
153
            ref.setDatePublished(TimePeriod.NewInstance(year));
154
        }
155
        ref.setVolume(volume);
156
        ref.setReferenceAbstract(abstractt);
157
        if (StringUtils.isNotBlank(doi)){
158
            try {
159
                String doiStr = doi;
160
                if (doiStr.startsWith("dx.doi.org/")){
161
                    doiStr = doiStr.substring(11);
162
                }
163
                ref.setDoi(DOI.fromString(doiStr));
164
            } catch (IllegalArgumentException e) {
165
                logger.warn("DOI could not be parsed: " + doi);
166
            }
167
        }
168
        ref.setEdition(issue);
169

    
170
        //id
171
        ImportHelper.setOriginalSource(ref, state.getTransactionalSourceReference(), id, REFERENCE_NAMESPACE);
172

    
173
        referencesToSave.add(ref);
174
    }
175

    
176

    
177
    /**
178
     * @param documentType
179
     * @return
180
     */
181
    private Reference makeReferenceType(Integer documentType, String dtype) {
182
        if (documentType == 11914){
183
            return ReferenceFactory.newArticle();
184
        } else if (documentType == 11916){
185
            return ReferenceFactory.newBook();
186
        } else if (documentType == 11915){
187
            return ReferenceFactory.newPrintSeries();
188
        } else if (documentType == 11913){
189
            return ReferenceFactory.newJournal();
190
        } else if (documentType == 11917){
191
            return ReferenceFactory.newBookSection();
192
        } else if (documentType == 11912 || documentType == 11919 || documentType == 11924 ){
193
            Reference ref = ReferenceFactory.newGeneric();
194
            return ref;
195
        } else {
196
            throw new RuntimeException("DocumentType not yet supported: " + documentType + ", " + dtype);
197
        }
198
    }
199

    
200
    @Override
201
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
202
            EdaphobaseImportState state) {
203
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
204
//        Map<String, TeamOrPersonBase<?>> authorMap = new HashMap<>();
205
//        Set<String> authorSet = new HashSet<>();
206
//        try {
207
//            while (rs.next()){
208
//                String authorStr = rs.getString("tax_author_name");
209
//                authorSet.add(authorStr);
210
//            }
211
//        } catch (SQLException e) {
212
//            e.printStackTrace();
213
//        }
214
//
215
//        //Authors
216
//        Set<UUID> uuidSet = new HashSet<>();
217
//        for (String authorStr : authorSet){
218
//            UUID uuid = state.getAuthorUuid(authorStr);
219
//            uuidSet.add(uuid);
220
//        }
221
//        List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
222
//        Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
223
//        for (TeamOrPersonBase<?> author : authors){
224
//            authorUuidMap.put(author.getUuid(), author);
225
//        }
226
//
227
//        for (String authorStr : authorSet){
228
//            UUID uuid = state.getAuthorUuid(authorStr);
229
//            TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
230
//            authorMap.put(authorStr, author);
231
//        }
232
//        result.put(AUTHOR_NAMESPACE, authorMap);
233

    
234
        return result;
235
    }
236

    
237

    
238

    
239
    @Override
240
    protected boolean doCheck(EdaphobaseImportState state) {
241
        return false;
242
    }
243

    
244
    @Override
245
    protected boolean isIgnore(EdaphobaseImportState state) {
246
        return ! state.getConfig().isDoReferences();
247
    }
248

    
249
}
(8-8/11)