cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportSynonymMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15
16 import org.apache.log4j.Logger;
17
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;
27
28 /**
29 * @author a.mueller
30 * @created 12.05.2009
31 * @version 1.0
32 */
33 /**
34 * @author a.mueller
35 * @created 02.03.2010
36 * @version 1.0
37 * @param <CDM_BASE>
38 * @param <STATE>
39 */
40 public class DbImportSynonymMapper<STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
41 private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
42
43 //******************************** FACTORY METHOD ***************************************************/
44
45 public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute){
46 return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, relTypeAttribute);
47 }
48
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;
57
58
59 //********************************* CONSTRUCTOR ****************************************/
60 /**
61 * @param relatedObjectNamespace
62 * @param mappingImport
63 */
64 protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
65 super();
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");
74 }
75 }
76
77 //************************************ METHODS *******************************************/
78
79 /* (non-Javadoc)
80 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
81 */
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);
87 if (ignoreRecord){
88 return cdmBase;
89 }
90 }
91
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));
96
97 //TODO cast
98 Reference citation = (Reference)getRelatedObject(rs, citationAttribute);
99 String microCitation = null;
100 if (citationAttribute != null){
101 microCitation = rs.getString(microCitationAttribute);
102 }
103
104
105 if (fromObject == null){
106 String warning = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
107 logger.warn(warning);
108 return cdmBase;
109 }
110 Synonym synonym = checkSynonymType(fromObject, fromId);
111
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);
115 return cdmBase;
116 }
117 Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
118
119 SynonymRelationshipType relType = SynonymRelationshipType.SYNONYM_OF();
120
121 taxon.addSynonym(synonym, relType, citation, microCitation);
122 return synonym;
123 }
124
125 /**
126 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
127 * @param rs
128 * @param dbAttribute
129 * @return
130 * @throws SQLException
131 */
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);
139 }
140 return result;
141 }
142
143
144 /**
145 * Checks if cdmBase is of type Taxon
146 * @param fromObject
147 */
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);
153 }
154 return (taxonBase.deproxy(taxonBase, Taxon.class));
155 }
156
157 /**
158 * Checks if cdmBase is of type Synonym
159 * @param fromObject
160 */
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);
166 }
167 return (cdmBase.deproxy(cdmBase, Synonym.class));
168 }
169
170
171 }