View Javadoc

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  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.UUID;
18  
19  import org.apache.log4j.Logger;
20  
21  import eu.etaxonomy.cdm.common.CdmUtils;
22  import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
23  import eu.etaxonomy.cdm.io.common.CdmImportBase;
24  import eu.etaxonomy.cdm.io.common.DbImportStateBase;
25  import eu.etaxonomy.cdm.io.common.ICdmIO;
26  import eu.etaxonomy.cdm.io.erms.ICheckIgnoreMapper;
27  import eu.etaxonomy.cdm.model.common.CdmBase;
28  import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
29  import eu.etaxonomy.cdm.model.common.IOriginalSource;
30  import eu.etaxonomy.cdm.model.common.ISourceable;
31  import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32  import eu.etaxonomy.cdm.model.common.IdentifiableSource;
33  import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34  import eu.etaxonomy.cdm.model.reference.ReferenceBase;
35  import eu.etaxonomy.cdm.model.taxon.Synonym;
36  import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
37  import eu.etaxonomy.cdm.model.taxon.Taxon;
38  import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39  import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
40  import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
41  
42  /**
43   * @author a.mueller
44   * @created 12.05.2009
45   * @version 1.0
46   */
47  /**
48   * @author a.mueller
49   * @created 02.03.2010
50   * @version 1.0
51   * @param <CDM_BASE>
52   * @param <STATE>
53   */
54  public class DbImportSynonymMapper<STATE extends DbImportStateBase> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
55  	private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
56  	
57  //******************************** FACTORY METHOD ***************************************************/
58  	
59  	public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute){
60  		return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, relTypeAttribute);
61  	}
62  	
63  //******************************* ATTRIBUTES ***************************************/
64  	private String fromAttribute;
65  	private String toAttribute;
66  //	private TaxonRelationshipType relType;
67  	private String relatedObjectNamespace;
68  	private String citationAttribute;
69  	private String microCitationAttribute;
70  	private String relationshipTypeAttribute;
71  	
72  	
73  //********************************* CONSTRUCTOR ****************************************/
74  	/**
75  	 * @param relatedObjectNamespace 
76  	 * @param mappingImport
77  	 */
78  	protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
79  		super();
80  		//TODO make it a single attribute mapper
81  		this.fromAttribute = fromAttribute;
82  		this.toAttribute = toAttribute;
83  //		this.relType = relType;
84  		this.relatedObjectNamespace = relatedObjectNamespace;
85  		this.relationshipTypeAttribute = relTypeAttribute;
86  		if (relTypeAttribute != null){
87  			logger.warn("Synonymrelationship type not yet implemented");
88  		}
89  	}
90  
91  //************************************ METHODS *******************************************/
92  
93  	/* (non-Javadoc)
94  	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
95  	 */
96  	public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
97  		STATE state = getState();
98  		ICdmIO currentImport = state.getCurrentIO();
99  		if (currentImport instanceof ICheckIgnoreMapper){
100 			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
101 			if (ignoreRecord){
102 				return cdmBase;
103 			}
104 		}
105 		
106 		TaxonBase fromObject = (TaxonBase)getRelatedObject(rs, fromAttribute);
107 		TaxonBase toObject = (TaxonBase)getRelatedObject(rs, toAttribute);
108 		String fromId = String.valueOf(rs.getObject(fromAttribute));
109 		String toId = String.valueOf(rs.getObject(toAttribute));
110 		
111 		//TODO cast
112 		ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, citationAttribute);
113 		String microCitation = null;
114 		if (citationAttribute != null){
115 			microCitation = rs.getString(microCitationAttribute);
116 		}
117 
118 		
119 		if (fromObject == null){
120 			String warning  = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
121 			logger.warn(warning);
122 			return cdmBase;
123 		}
124 		Synonym synonym = checkSynonymType(fromObject, fromId);
125 		
126 		if (toObject == null){
127 			String warning  = "The accepted taxon (" + toId + ") could not be found. Synonym not added to accepted taxon";
128 			logger.warn(warning);
129 			return cdmBase;
130 		}
131 		Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
132 		
133 		SynonymRelationshipType relType = SynonymRelationshipType.SYNONYM_OF();
134 		
135 		taxon.addSynonym(synonym, relType, citation, microCitation);
136 		return synonym;
137 	}
138 	
139 	/**
140 	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
141 	 * @param rs
142 	 * @param dbAttribute
143 	 * @return
144 	 * @throws SQLException
145 	 */
146 	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
147 		CdmBase result = null;
148 		if (dbAttribute != null){
149 			Object dbValue = rs.getObject(dbAttribute);
150 			String id = String.valueOf(dbValue);
151 			DbImportStateBase state = importMapperHelper.getState();
152 			result = state.getRelatedObject(relatedObjectNamespace, id);
153 		}
154 		return result;
155 	}
156 	
157 
158 	/**
159 	 * Checks if cdmBase is of type Taxon 
160 	 * @param fromObject
161 	 */
162 	private Taxon checkTaxonType(TaxonBase taxonBase, String typeString, String id) {
163 		if (! taxonBase.isInstanceOf(Taxon.class)){
164 			String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
165 			logger.warn(warning);
166 			throw new IllegalArgumentException(warning);
167 		}
168 		return (taxonBase.deproxy(taxonBase, Taxon.class));
169 	}
170 	
171 	/**
172 	 * Checks if cdmBase is of type Synonym 
173 	 * @param fromObject
174 	 */
175 	private Synonym checkSynonymType(CdmBase cdmBase, String id) {
176 		if (! cdmBase.isInstanceOf(Synonym.class)){
177 			String warning = "Synonym (" + id + ") is not of type Synonym but of type " + cdmBase.getClass().getSimpleName();
178 			logger.warn(warning);
179 			throw new IllegalArgumentException(warning);
180 		}
181 		return (cdmBase.deproxy(cdmBase, Synonym.class));
182 	}
183 
184 
185 }