remove "NEW" comment
[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 02.03.2010
31 * @version 1.0
32 * @param <CDM_BASE>
33 * @param <STATE>
34 */
35 public class DbImportSynonymMapper<STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
36 private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
37
38 //******************************** FACTORY METHOD ***************************************************/
39
40 /**
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)
47 * @return
48 */
49 public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute, TaxonRelationshipType taxonRelationshipType){
50 return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, taxonRelationshipType, relatedObjectNamespace, relTypeAttribute);
51 }
52
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;
62
63
64 //********************************* CONSTRUCTOR ****************************************/
65 /**
66 * @param relatedObjectNamespace
67 * @param mappingImport
68 */
69 protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
70 super();
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");
79 }
80 this.useTaxonRelationship = (relType != null);
81 }
82
83 //************************************ METHODS *******************************************/
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
87 */
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);
93 if (ignoreRecord){
94 return cdmBase;
95 }
96 }
97
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));
102
103 if (toId == null){
104 return fromObject;
105 }
106
107 Reference<?> citation = CdmBase.deproxy(getRelatedObject(rs, citationAttribute), Reference.class);
108 String microCitation = null;
109 if (citationAttribute != null){
110 microCitation = rs.getString(microCitationAttribute);
111 }
112
113
114 if (fromObject == null){
115 String warning = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
116 logger.warn(warning);
117 return cdmBase;
118 }
119 checkSynonymType(fromObject, fromId);
120
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);
124 return cdmBase;
125 }
126 Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
127
128
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);
137
138 }else{
139 logger.warn("Taxon is not a synonym and accepted taxa are not allowed as synonyms: " + fromObject.getTitleCache() + "; " + fromObject.getId());
140 }
141 return fromObject;
142 }
143
144 /**
145 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
146 * @param rs
147 * @param dbAttribute
148 * @return
149 * @throws SQLException
150 */
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);
158 }
159 return result;
160 }
161
162
163 /**
164 * Checks if cdmBase is of type Taxon
165 * @param fromObject
166 */
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);
172 }
173 return (CdmBase.deproxy(taxonBase, Taxon.class));
174 }
175
176 /**
177 * Checks if cdmBase is of type Synonym
178 * @param fromObject
179 */
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);
186 }else{
187 logger.info(warning);
188 }
189 }
190 return (CdmBase.deproxy(cdmBase, TaxonBase.class));
191 }
192
193
194 }