3 * Copyright (C) 2007 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.
11 package eu
.etaxonomy
.cdm
.io
.pesi
.erms
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
24 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportNameTypeDesignationMapper
;
25 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportSynonymMapper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportTaxIncludedInMapper
;
27 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.ICheckIgnoreMapper
;
28 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.IDbImportMapper
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
39 public class ErmsTaxonRelationImport
extends ErmsImportBase
<TaxonBase
<?
>> implements ICheckIgnoreMapper
{
40 @SuppressWarnings("unused")
41 private static final Logger logger
= Logger
.getLogger(ErmsTaxonRelationImport
.class);
43 private DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
> mapping
;
45 private static final String pluralString
= "taxon relations";
46 private static final String dbTableName
= "tu";
48 private static final Class
<?
> cdmTargetClass
= TaxonBase
.class;
50 private ErmsImportState state
; //ERMS import will never run in more then one instance
52 public ErmsTaxonRelationImport(){
53 super(pluralString
, dbTableName
, cdmTargetClass
);
58 protected DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
> getMapping() {
60 mapping
= new DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
>();
62 mapping
.addMapper(DbImportTaxIncludedInMapper
.NewInstance("id", TAXON_NAMESPACE
, "parentId", TAXON_NAMESPACE
, "accParentId", TAXON_NAMESPACE
, null));//there is only one tree
64 TaxonRelationshipType taxonRelationshipType
= getTaxonRelationshipType(state
, ErmsTransformer
.uuidTaxRelTypeIsTaxonSynonymOf
, "is taxon synonym of", "is synonym of relation used by synonym that are of class Taxon as they can not be handled differently", null, null);
65 mapping
.addMapper(DbImportSynonymMapper
.NewInstance("id", "tu_acctaxon", TAXON_NAMESPACE
, null, taxonRelationshipType
));
66 mapping
.addMapper(DbImportNameTypeDesignationMapper
.NewInstance("id", "tu_typetaxon", ErmsTaxonImport
.NAME_NAMESPACE
, "tu_typedesignationstatus"));
67 // mapping.addMapper(DbNotYetImplementedMapper.NewInstance("tu_acctaxon"));
74 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getRecordQuery(eu.etaxonomy.cdm.io.erms.ErmsImportConfigurator)
76 protected String
getRecordQuery(ErmsImportConfigurator config
) {
77 //TODO get automatic by second path mappers
78 String selectAttributes
= " myTaxon.id, myTaxon.tu_parent, myTaxon.tu_typetaxon, myTaxon.tu_typedesignation, " +
79 " myTaxon.tu_acctaxon, myTaxon.tu_status, parent.tu_status AS parentStatus, parent.id AS parentId, " +
80 " accParent.tu_status AS accParentStatus, accParent.id AS accParentId ";
81 String strRecordQuery
=
82 " SELECT " + selectAttributes
+
83 " FROM tu AS myTaxon LEFT OUTER JOIN " +
84 " tu AS accTaxon ON myTaxon.tu_acctaxon = accTaxon.id LEFT OUTER JOIN " +
85 " tu AS accParent RIGHT OUTER JOIN " +
86 " tu AS parent ON accParent.id = parent.tu_acctaxon ON myTaxon.tu_parent = parent.id " +
87 " WHERE ( myTaxon.id IN (" + ID_LIST_TOKEN
+ ") )";
88 return strRecordQuery
;
93 protected void doInvoke(ErmsImportState state
) {
95 super.doInvoke(state
);
99 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, ErmsImportState state
) {
103 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
106 Set
<String
> taxonIdSet
= new HashSet
<String
>();
107 Set
<String
> nameIdSet
= new HashSet
<String
>();
109 handleForeignKey(rs
, taxonIdSet
, "parentId");
110 handleForeignKey(rs
, taxonIdSet
, "accParentId");
111 handleForeignKey(rs
, taxonIdSet
, "tu_acctaxon");
112 handleForeignKey(rs
, taxonIdSet
, "id");
113 handleForeignKey(rs
, nameIdSet
, "tu_typetaxon");
114 handleForeignKey(rs
, nameIdSet
, "id");
118 nameSpace
= ErmsTaxonImport
.NAME_NAMESPACE
;
119 cdmClass
= TaxonNameBase
.class;
121 Map
<String
, TaxonNameBase
<?
,?
>> nameMap
= (Map
<String
, TaxonNameBase
<?
,?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
122 result
.put(nameSpace
, nameMap
);
126 nameSpace
= ErmsTaxonImport
.TAXON_NAMESPACE
;
127 cdmClass
= TaxonBase
.class;
129 Map
<String
, TaxonBase
<?
>> taxonMap
= (Map
<String
, TaxonBase
<?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
130 result
.put(nameSpace
, taxonMap
);
132 } catch (SQLException e
) {
133 throw new RuntimeException(e
);
140 * @see eu.etaxonomy.cdm.io.erms.ICheckIgnoreMapper#checkIgnoreMapper(eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper, java.sql.ResultSet)
142 public boolean checkIgnoreMapper(IDbImportMapper mapper
, ResultSet rs
) throws SQLException
{
143 boolean result
= false;
144 if (mapper
instanceof DbImportTaxIncludedInMapper
){
146 // int tu_status = rs.getInt("tu_status");
147 // if (tu_status != 1){
151 int id
= rs
.getInt("id");
152 if (state
.getAcceptedTaxaKeys().contains(id
)){
157 }else if (mapper
instanceof DbImportSynonymMapper
){
159 // int tu_status = rs.getInt("tu_status");
160 // if (tu_status == 1){
164 // }t.tu_acctaxon <> t.id
166 int id
= rs
.getInt("id");
167 Object accTaxonId
= rs
.getObject("tu_acctaxon");
168 if (accTaxonId
== null){
171 int accId
= Integer
.valueOf(String
.valueOf(accTaxonId
));
174 }else if (mapper
instanceof DbImportNameTypeDesignationMapper
){
175 Object tu_typeTaxon
= rs
.getObject("tu_typetaxon");
176 if (tu_typeTaxon
== null){
185 protected boolean doCheck(ErmsImportState state
){
186 // IOValidator<ErmsImportState> validator = new ErmsTaxonImportValidator();
187 // return validator.validate(state);
192 protected boolean isIgnore(ErmsImportState state
){
193 return ! state
.getConfig().isDoRelTaxa();