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
.common
.mapping
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
19 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
35 public class DbImportSynonymMapper
<STATE
extends DbImportStateBase
<?
,?
>> extends DbImportMultiAttributeMapperBase
<CdmBase
, STATE
> {
36 private static final Logger logger
= Logger
.getLogger(DbImportSynonymMapper
.class);
38 //******************************** FACTORY METHOD ***************************************************/
41 * Creates a new instance of SynonymMapper.
42 * @param dbFromAttribute
43 * @param dbToAttribute
44 * @param relatedObjectNamespace
45 * @param relTypeAttribute
46 * @param taxonRelationshipType this relationshiptype is taken for accepted taxa being synonyms (may be the case if data are dirty)
49 public static DbImportSynonymMapper
<?
> NewInstance(String dbFromAttribute
, String dbToAttribute
, String relatedObjectNamespace
, String relTypeAttribute
, TaxonRelationshipType taxonRelationshipType
){
50 return new DbImportSynonymMapper(dbFromAttribute
, dbToAttribute
, taxonRelationshipType
, relatedObjectNamespace
, relTypeAttribute
);
53 //******************************* ATTRIBUTES ***************************************/
54 private String fromAttribute
;
55 private String toAttribute
;
56 private TaxonRelationshipType relType
;
57 private String relatedObjectNamespace
;
58 private String citationAttribute
;
59 private String microCitationAttribute
;
60 private String relationshipTypeAttribute
;
61 private boolean useTaxonRelationship
;
64 //********************************* CONSTRUCTOR ****************************************/
66 * @param relatedObjectNamespace
67 * @param mappingImport
69 protected DbImportSynonymMapper(String fromAttribute
, String toAttribute
, TaxonRelationshipType relType
, String relatedObjectNamespace
, String relTypeAttribute
) {
71 //TODO make it a single attribute mapper
72 this.fromAttribute
= fromAttribute
;
73 this.toAttribute
= toAttribute
;
74 this.relType
= relType
;
75 this.relatedObjectNamespace
= relatedObjectNamespace
;
76 this.relationshipTypeAttribute
= relTypeAttribute
;
77 if (relTypeAttribute
!= null){
78 logger
.warn("Synonymrelationship type not yet implemented");
80 this.useTaxonRelationship
= (relType
!= null);
83 //************************************ METHODS *******************************************/
86 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
88 public CdmBase
invoke(ResultSet rs
, CdmBase cdmBase
) throws SQLException
{
89 STATE state
= getState();
90 ICdmIO
<?
> currentImport
= state
.getCurrentIO();
91 if (currentImport
instanceof ICheckIgnoreMapper
){
92 boolean ignoreRecord
= ((ICheckIgnoreMapper
)currentImport
).checkIgnoreMapper(this, rs
);
98 TaxonBase
<?
> fromObject
= (TaxonBase
<?
>)getRelatedObject(rs
, fromAttribute
);
99 TaxonBase
<?
> toObject
= (TaxonBase
<?
>)getRelatedObject(rs
, toAttribute
);
100 String fromId
= rs
.getObject(fromAttribute
)== null ?
null: String
.valueOf(rs
.getObject(fromAttribute
));
101 String toId
= rs
.getObject(toAttribute
) == null?
null : String
.valueOf(rs
.getObject(toAttribute
));
107 Reference
<?
> citation
= CdmBase
.deproxy(getRelatedObject(rs
, citationAttribute
), Reference
.class);
108 String microCitation
= null;
109 if (citationAttribute
!= null){
110 microCitation
= rs
.getString(microCitationAttribute
);
114 if (fromObject
== null){
115 String warning
= "The synonym (" + fromId
+ ") could not be found. Synonym not added to accepted taxon";
116 logger
.warn(warning
);
119 checkSynonymType(fromObject
, fromId
);
121 if (toObject
== null){
122 String warning
= "The accepted taxon (" + toId
+ ") could not be found. Synonym not added to accepted taxon";
123 logger
.warn(warning
);
126 Taxon taxon
= checkTaxonType(toObject
, "Accepted taxon", toId
);
129 if (fromObject
.isInstanceOf(Synonym
.class)){
130 SynonymRelationshipType relType
= SynonymRelationshipType
.SYNONYM_OF();
131 Synonym synonym
= CdmBase
.deproxy(fromObject
, Synonym
.class);
132 taxon
.addSynonym(synonym
, relType
, citation
, microCitation
);
133 }else if (fromObject
.isInstanceOf(Taxon
.class) && this.useTaxonRelationship
){
134 TaxonRelationshipType type
= relType
;
135 Taxon synonymTaxon
= CdmBase
.deproxy(fromObject
, Taxon
.class);
136 synonymTaxon
.addTaxonRelation(taxon
, type
, citation
, microCitation
);
139 logger
.warn("Taxon is not a synonym and accepted taxa are not allowed as synonyms: " + fromObject
.getTitleCache() + "; " + fromObject
.getId());
145 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
149 * @throws SQLException
151 protected CdmBase
getRelatedObject(ResultSet rs
, String dbAttribute
) throws SQLException
{
152 CdmBase result
= null;
153 if (dbAttribute
!= null){
154 Object dbValue
= rs
.getObject(dbAttribute
);
155 String id
= String
.valueOf(dbValue
);
156 DbImportStateBase
<?
,?
> state
= importMapperHelper
.getState();
157 result
= state
.getRelatedObject(relatedObjectNamespace
, id
);
164 * Checks if cdmBase is of type Taxon
167 private Taxon
checkTaxonType(TaxonBase
<?
> taxonBase
, String typeString
, String id
) {
168 if (! taxonBase
.isInstanceOf(Taxon
.class)){
169 String warning
= typeString
+ " (" + id
+ ") is not of type Taxon but of type " + taxonBase
.getClass().getSimpleName();
170 logger
.warn(warning
);
171 throw new IllegalArgumentException(warning
);
173 return (CdmBase
.deproxy(taxonBase
, Taxon
.class));
177 * Checks if cdmBase is of type Synonym
180 private TaxonBase
<?
> checkSynonymType(CdmBase cdmBase
, String id
) {
181 if (! cdmBase
.isInstanceOf(Synonym
.class)){
182 String warning
= "Synonym (" + id
+ ") is not of type Synonym but of type " + cdmBase
.getClass().getSimpleName();
183 if (! this.useTaxonRelationship
){
184 logger
.warn(warning
);
185 throw new IllegalArgumentException(warning
);
187 logger
.info(warning
);
190 return (CdmBase
.deproxy(cdmBase
, TaxonBase
.class));