(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMultiAttributeMapperBase.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.model.common.CdmBase;
20
21 /**
22 * @author a.mueller
23 * @created 12.03.2010
24 * @version 1.0
25 */
26 public abstract class DbImportMultiAttributeMapperBase<CDMBASE extends CdmBase, STATE extends DbImportStateBase<?,?>> extends MultipleAttributeMapperBase<CdmSingleAttributeMapperBase> implements IDbImportMapper<STATE, CDMBASE>{
27 @SuppressWarnings("unused")
28 private static final Logger logger = Logger.getLogger(DbImportMultiAttributeMapperBase.class);
29
30 //**************************** ATTRIBUTES ****************************************************
31
32 protected DbImportMapperBase<STATE> importMapperHelper = new DbImportMapperBase<STATE>();
33
34 /* (non-Javadoc)
35 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
36 */
37 public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
38 importMapperHelper.initialize(state, destinationClass);
39 }
40
41 protected STATE getState(){
42 return importMapperHelper.getState();
43 }
44
45
46 /**
47 * Retrieves a related object from the state's related object map. Needs casting.
48 * @param namespace
49 * @param foreignKey
50 * @return
51 */
52 protected CdmBase getRelatedObject(String namespace, String foreignKey) {
53 STATE state = importMapperHelper.getState();
54 CdmBase result = state.getRelatedObject(namespace, foreignKey);
55 return result;
56 }
57
58 /**
59 * Retrieves a related object from the state's related object map. Needs casting.
60 * @param namespace
61 * @param foreignKey
62 * @return
63 * @throws SQLException
64 */
65 protected CdmBase getRelatedObject(ResultSet rs, String namespace, String fkAttribute) throws SQLException {
66 STATE state = importMapperHelper.getState();
67 String foreignKey = getForeignKey(rs, fkAttribute);
68 CdmBase result = state.getRelatedObject(namespace, foreignKey);
69 return result;
70 }
71
72 /**
73 * @param rs
74 * @param dbReferenceFkAttribute2
75 * @return
76 * @throws SQLException
77 */
78 protected String getForeignKey(ResultSet rs, String fkAttribute) throws SQLException {
79 Object oForeignKey = rs.getObject(fkAttribute);
80 String result = String.valueOf(oForeignKey);
81 return result;
82 }
83
84
85 }