3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.edaphobase
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
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
;
33 public class EdaphobaseInReferenceImport
extends EdaphobaseImportBase
{
34 private static final long serialVersionUID
= 6895687693249076160L;
36 private static final Logger logger
= Logger
.getLogger(EdaphobaseInReferenceImport
.class);
38 private static final String tableName
= "lit_document";
40 private static final String pluralString
= "documents";
46 public EdaphobaseInReferenceImport() {
47 super(tableName
, pluralString
);
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 ";
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
);
68 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
69 ResultSet rs
= partitioner
.getResultSet();
70 Set
<Reference
> referencesToSave
= new HashSet
<>();
73 handleSingleReference(state
, rs
, referencesToSave
);
75 } catch (SQLException e
) {
78 getReferenceService().saveOrUpdate(referencesToSave
);
85 * @param referencesToSave
86 * @throws SQLException
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");
93 Reference
<?
> child
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(id
), Reference
.class);
94 Reference
<?
> parent
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(parentId
), Reference
.class);
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" );
100 child
.setInReference(parent
);
101 referencesToSave
.add(child
);
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
>();
113 handleForeignKey(rs
, referenceIdSet
, "document_id");
114 handleForeignKey(rs
, referenceIdSet
, "parent_document_fk_document_id");
116 } catch (SQLException e
) {
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
);
133 protected boolean doCheck(EdaphobaseImportState state
) {
138 protected boolean isIgnore(EdaphobaseImportState state
) {
139 return ! state
.getConfig().isDoReferences();