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