2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.edaphobase
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.stereotype
.Component
;
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
;
32 public class EdaphobaseInReferenceImport
extends EdaphobaseImportBase
{
33 private static final long serialVersionUID
= 6895687693249076160L;
35 private static final Logger logger
= Logger
.getLogger(EdaphobaseInReferenceImport
.class);
37 private static final String tableName
= "lit_document";
39 private static final String pluralString
= "documents";
45 public EdaphobaseInReferenceImport() {
46 super(tableName
, pluralString
);
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 ";
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
);
67 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
68 ResultSet rs
= partitioner
.getResultSet();
69 Set
<Reference
> referencesToSave
= new HashSet
<>();
72 handleSingleReference(state
, rs
, referencesToSave
);
74 } catch (SQLException e
) {
77 getReferenceService().saveOrUpdate(referencesToSave
);
84 * @param referencesToSave
85 * @throws SQLException
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");
92 Reference child
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(id
), Reference
.class);
93 Reference parent
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(parentId
), Reference
.class);
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" );
99 child
.setInReference(parent
);
100 referencesToSave
.add(child
);
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
>();
112 handleForeignKey(rs
, referenceIdSet
, "document_id");
113 handleForeignKey(rs
, referenceIdSet
, "parent_document_fk_document_id");
115 } catch (SQLException e
) {
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
);
132 protected boolean doCheck(EdaphobaseImportState state
) {
137 protected boolean isIgnore(EdaphobaseImportState state
) {
138 return ! state
.getConfig().isDoReferences();