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.DbImportStateBase;
19  import eu.etaxonomy.cdm.model.common.CdmBase;
20  import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
21  import eu.etaxonomy.cdm.model.common.IOriginalSource;
22  import eu.etaxonomy.cdm.model.common.ISourceable;
23  import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24  import eu.etaxonomy.cdm.model.common.IdentifiableSource;
25  import eu.etaxonomy.cdm.model.common.VersionableEntity;
26  import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27  import eu.etaxonomy.cdm.model.reference.ReferenceBase;
28  
29  /**
30   * @author a.mueller
31   * @created 12.05.2009
32   * @version 1.0
33   */
34  //TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
35  public abstract class DbImportObjectCreationMapperBase<CREATE extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CREATE, STATE>  {
36  	private static final Logger logger = Logger.getLogger(DbImportObjectCreationMapperBase.class);
37  	
38  	
39  //******************************* ATTRIBUTES ***************************************/
40  	protected String dbIdAttribute;
41  	//TODO get standard namespace from mappingImport
42  	protected String objectToCreateNamespace;
43  	
44  	
45  //********************************* CONSTRUCTOR ****************************************/
46  	/**
47  	 * @param mappingImport
48  	 */
49  	protected DbImportObjectCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace) {
50  		super();
51  		//TODO make it a single attribute mapper
52  		this.dbIdAttribute = dbIdAttribute;
53  		this.objectToCreateNamespace = objectToCreateNamespace;
54  	}
55  
56  //************************************ METHODS *******************************************/
57  
58  	/* (non-Javadoc)
59  	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
60  	 */
61  	public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
62  		super.initialize(state, destinationClass);
63  		//Todo remove initialize when this logging is not needed anymore
64  		logger.warn("DbImportObjectCreationMapperBase still needs 'citation' implemented for OriginalSource"); //see addOriginalSource()
65  	}
66  	
67  	
68  	/* (non-Javadoc)
69  	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
70  	 */
71  	public CREATE invoke(ResultSet rs, CREATE noObject) throws SQLException {
72  		CREATE result = createObject(rs);
73  		result = doInvoke(rs, result);
74  		addOriginalSource(rs, result);
75  		return result;
76  	}
77  
78  	/**
79  	 * @param rs
80  	 * @param result
81  	 * @throws SQLException 
82  	 */
83  	protected abstract CREATE doInvoke(ResultSet rs, CREATE createdObject) throws SQLException;
84  
85  	/**
86  	 * This method creates the object to be created. It needs to be implemented by the concrete classes.
87  	 * E.g. if you have a TaxonNameCreation class which inherits from this class you need to implement
88  	 * createObject by creating an empty taxon name.
89  	 * @param rs The result set
90  	 * @return The object to be created
91  	 * @throws SQLException 
92  	 */
93  	protected abstract CREATE createObject(ResultSet rs) throws SQLException;
94  
95  	/**
96  	 * TODO also implemented in CdmImportBase (reduce redundance)
97  	 * @throws SQLException 
98  	 */
99  	public void addOriginalSource(ResultSet rs, CREATE cdmBase) throws SQLException {
100 		if (cdmBase instanceof ISourceable ){
101 			IOriginalSource source;
102 			ISourceable sourceable = (ISourceable)cdmBase;
103 			Object id = rs.getObject(dbIdAttribute);
104 			String strId = String.valueOf(id);
105 			String idNamespace = this.objectToCreateNamespace;
106 			//FIXME
107 			ReferenceBase citation = null;
108 			//importMapperHelper.getState().getConfig()xxx;
109 			String microCitation = null;
110 			if (cdmBase instanceof IdentifiableEntity){
111 				source = IdentifiableSource.NewInstance(strId, idNamespace, citation, microCitation);
112 			}else if (cdmBase instanceof DescriptionElementBase){
113 				source = DescriptionElementSource.NewInstance(strId, idNamespace, citation, microCitation);
114 			}else{
115 				logger.warn("ISourceable not beeing identifiable entities or description element base are not yet supported. CdmBase is of type " + cdmBase.getClass().getSimpleName() + ". Original source not added.");
116 				return;
117 			}
118 			sourceable.addSource(source);
119 		}
120 	}
121 	
122 
123 	
124 	
125 	/**
126 	 * Returns the transformer from the configuration
127 	 * @return
128 	 */
129 	protected IInputTransformer getTransformer(){
130 		return getState().getConfig().getTransformer();
131 	}
132 	
133 }