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
;
40 public class DbImportSynonymMapper
<STATE
extends DbImportStateBase
<?
,?
>> extends DbImportMultiAttributeMapperBase
<CdmBase
, STATE
> {
41 private static final Logger logger
= Logger
.getLogger(DbImportSynonymMapper
.class);
43 //******************************** FACTORY METHOD ***************************************************/
45 public static DbImportSynonymMapper
<?
> NewInstance(String dbFromAttribute
, String dbToAttribute
, String relatedObjectNamespace
, String relTypeAttribute
){
46 return new DbImportSynonymMapper(dbFromAttribute
, dbToAttribute
, null, relatedObjectNamespace
, relTypeAttribute
);
49 //******************************* ATTRIBUTES ***************************************/
50 private String fromAttribute
;
51 private String toAttribute
;
52 // private TaxonRelationshipType relType;
53 private String relatedObjectNamespace
;
54 private String citationAttribute
;
55 private String microCitationAttribute
;
56 private String relationshipTypeAttribute
;
59 //********************************* CONSTRUCTOR ****************************************/
61 * @param relatedObjectNamespace
62 * @param mappingImport
64 protected DbImportSynonymMapper(String fromAttribute
, String toAttribute
, TaxonRelationshipType relType
, String relatedObjectNamespace
, String relTypeAttribute
) {
66 //TODO make it a single attribute mapper
67 this.fromAttribute
= fromAttribute
;
68 this.toAttribute
= toAttribute
;
69 // this.relType = relType;
70 this.relatedObjectNamespace
= relatedObjectNamespace
;
71 this.relationshipTypeAttribute
= relTypeAttribute
;
72 if (relTypeAttribute
!= null){
73 logger
.warn("Synonymrelationship type not yet implemented");
77 //************************************ METHODS *******************************************/
80 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
82 public CdmBase
invoke(ResultSet rs
, CdmBase cdmBase
) throws SQLException
{
83 STATE state
= getState();
84 ICdmIO currentImport
= state
.getCurrentIO();
85 if (currentImport
instanceof ICheckIgnoreMapper
){
86 boolean ignoreRecord
= ((ICheckIgnoreMapper
)currentImport
).checkIgnoreMapper(this, rs
);
92 TaxonBase fromObject
= (TaxonBase
)getRelatedObject(rs
, fromAttribute
);
93 TaxonBase toObject
= (TaxonBase
)getRelatedObject(rs
, toAttribute
);
94 String fromId
= String
.valueOf(rs
.getObject(fromAttribute
));
95 String toId
= String
.valueOf(rs
.getObject(toAttribute
));
98 Reference citation
= (Reference
)getRelatedObject(rs
, citationAttribute
);
99 String microCitation
= null;
100 if (citationAttribute
!= null){
101 microCitation
= rs
.getString(microCitationAttribute
);
105 if (fromObject
== null){
106 String warning
= "The synonym (" + fromId
+ ") could not be found. Synonym not added to accepted taxon";
107 logger
.warn(warning
);
110 Synonym synonym
= checkSynonymType(fromObject
, fromId
);
112 if (toObject
== null){
113 String warning
= "The accepted taxon (" + toId
+ ") could not be found. Synonym not added to accepted taxon";
114 logger
.warn(warning
);
117 Taxon taxon
= checkTaxonType(toObject
, "Accepted taxon", toId
);
119 SynonymRelationshipType relType
= SynonymRelationshipType
.SYNONYM_OF();
121 taxon
.addSynonym(synonym
, relType
, citation
, microCitation
);
126 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
130 * @throws SQLException
132 protected CdmBase
getRelatedObject(ResultSet rs
, String dbAttribute
) throws SQLException
{
133 CdmBase result
= null;
134 if (dbAttribute
!= null){
135 Object dbValue
= rs
.getObject(dbAttribute
);
136 String id
= String
.valueOf(dbValue
);
137 DbImportStateBase state
= importMapperHelper
.getState();
138 result
= state
.getRelatedObject(relatedObjectNamespace
, id
);
145 * Checks if cdmBase is of type Taxon
148 private Taxon
checkTaxonType(TaxonBase taxonBase
, String typeString
, String id
) {
149 if (! taxonBase
.isInstanceOf(Taxon
.class)){
150 String warning
= typeString
+ " (" + id
+ ") is not of type Taxon but of type " + taxonBase
.getClass().getSimpleName();
151 logger
.warn(warning
);
152 throw new IllegalArgumentException(warning
);
154 return (taxonBase
.deproxy(taxonBase
, Taxon
.class));
158 * Checks if cdmBase is of type Synonym
161 private Synonym
checkSynonymType(CdmBase cdmBase
, String id
) {
162 if (! cdmBase
.isInstanceOf(Synonym
.class)){
163 String warning
= "Synonym (" + id
+ ") is not of type Synonym but of type " + cdmBase
.getClass().getSimpleName();
164 logger
.warn(warning
);
165 throw new IllegalArgumentException(warning
);
167 return (cdmBase
.deproxy(cdmBase
, Synonym
.class));