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  
16  import org.apache.log4j.Logger;
17  
18  import eu.etaxonomy.cdm.io.common.CdmImportBase;
19  import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20  import eu.etaxonomy.cdm.io.erms.ICheckIgnoreMapper;
21  import eu.etaxonomy.cdm.model.common.CdmBase;
22  import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
23  import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24  import eu.etaxonomy.cdm.model.reference.ReferenceBase;
25  
26  /**
27   * @author a.mueller
28   * @created 12.05.2009
29   * @version 1.0
30   */
31  /**
32   * @author a.mueller
33   * @created 02.03.2010
34   * @version 1.0
35   * @param <CDM_BASE>
36   * @param <STATE>
37   */
38  public class DbImportNameTypeDesignationMapper<STATE extends DbImportStateBase<?,?>, T extends IDbImportTransformed> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
39  	private static final Logger logger = Logger.getLogger(DbImportNameTypeDesignationMapper.class);
40  	
41  //******************************** FACTORY METHOD ***************************************************/
42  	
43  	public static DbImportNameTypeDesignationMapper<?,?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String desigStatusAttribute){
44  		return new DbImportNameTypeDesignationMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, desigStatusAttribute);
45  	}
46  	
47  //******************************* ATTRIBUTES ***************************************/
48  	private String fromAttribute;
49  	private String toAttribute;
50  	private NameTypeDesignationStatus designationStatus;
51  	private String relatedObjectNamespace;
52  	private String citationAttribute;
53  	private String microCitationAttribute;
54  	private String designationStatusAttribute;
55  	
56  	
57  //********************************* CONSTRUCTOR ****************************************/
58  	/**
59  	 * @param relatedObjectNamespace 
60  	 * @param mappingImport
61  	 */
62  	protected DbImportNameTypeDesignationMapper(String fromAttribute, String toAttribute, NameTypeDesignationStatus designationStatus, String relatedObjectNamespace, String desigStatusAttribute) {
63  		super();
64  		//TODO make it a single attribute mapper
65  		this.fromAttribute = fromAttribute;
66  		this.toAttribute = toAttribute;
67  		this.relatedObjectNamespace = relatedObjectNamespace;
68  		this.designationStatusAttribute = desigStatusAttribute;
69  		this.designationStatus = designationStatus;
70  	}
71  
72  //************************************ METHODS *******************************************/
73  	
74  	/* (non-Javadoc)
75  	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
76  	 */
77  	public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
78  		STATE state = importMapperHelper.getState();
79  		CdmImportBase currentImport = state.getCurrentIO();
80  		if (currentImport instanceof ICheckIgnoreMapper){
81  			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
82  			if (ignoreRecord){
83  				return cdmBase;
84  			}
85  		}
86  		
87  		CdmBase fromObject = getRelatedObject(rs, fromAttribute);
88  		CdmBase toObject = getRelatedObject(rs, toAttribute);
89  		//TODO cast
90  		ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, citationAttribute);
91  		String microCitation = null;
92  		if (citationAttribute != null){
93  			microCitation = rs.getString(microCitationAttribute);
94  		}
95  		
96  		Object designationStatusValue = null;
97  		if (citationAttribute != null){
98  			designationStatusValue = rs.getObject(designationStatusAttribute);
99  		}
100 
101 
102 		if (fromObject == null){
103 			String warning  = "Higher rank name could not be found. Name type not added to higher rank name";
104 			logger.warn(warning);
105 			return cdmBase;
106 		}
107 		TaxonNameBase typifiedName = checkTaxonNameBaseType(fromObject);
108 		
109 		if (toObject == null){
110 			String warning  = "Species name could not be found. Name type not added to higher rank name";
111 			logger.warn(warning);
112 			return cdmBase;
113 		}
114 		TaxonNameBase typeName = checkTaxonNameBaseType(toObject);
115 		
116 		boolean addToAllHomotypicNames = false; //TODO check if this is correct
117 		String originalNameString = null; //TODO what is this
118 		
119 		NameTypeDesignationStatus status = this.designationStatus;
120 		if (designationStatusValue != null){
121 			//FIXME this needs work in generics to remove casts. Or find an other solution
122 			if (currentImport instanceof IDbImportTransformed){
123 				IDbImportTransformer transformer = ((IDbImportTransformed)currentImport).getTransformer();
124 				status = transformer.transformNameTypeDesignationStatus(designationStatusValue);
125 			}
126 		}
127 		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
128 		
129 		return typifiedName;
130 	}
131 	
132 	/**
133 	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
134 	 * @param rs
135 	 * @param dbAttribute
136 	 * @return
137 	 * @throws SQLException
138 	 */
139 	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
140 		CdmBase result = null;
141 		if (dbAttribute != null){
142 			Object dbValue = rs.getObject(dbAttribute);
143 			String id = String.valueOf(dbValue);
144 			DbImportStateBase state = importMapperHelper.getState();
145 			result = state.getRelatedObject(relatedObjectNamespace, id);
146 		}
147 		return result;
148 	}
149 	
150 	/**
151 	 * Checks if cdmBase is of type Taxon 
152 	 * @param fromObject
153 	 */
154 	private TaxonNameBase checkTaxonNameBaseType(CdmBase cdmBase) {
155 		if (! cdmBase.isInstanceOf(TaxonNameBase.class)){
156 			String warning = "Type name or typifier name is not of type TaxonNameBase but " + cdmBase.getClass().getName();
157 			logger.warn(warning);
158 			throw new IllegalArgumentException(warning);
159 		}
160 		return (cdmBase.deproxy(cdmBase, TaxonNameBase.class));
161 	}
162 
163 
164 }