6f2e3e5bbe0a00f6970ebbde24eb8f203724a8cb
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMapping.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 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22
23 /**
24 * @author a.mueller
25 * @created 12.05.2009
26 * @version 1.0
27 */
28 public class DbImportMapping<STATE extends DbImportStateBase, CONFIG extends IImportConfigurator> extends CdmIoMapping {
29 private static final Logger logger = Logger.getLogger(DbImportMapping.class);
30
31 private boolean isInitialized = false;;
32 private Class<? extends CdmBase> destinationClass;
33 private DbImportMapping<STATE, CONFIG> secondPathMapping;
34
35 public DbImportMapping(){
36 // this.dbTableName = tableName;
37 }
38
39 public boolean initialize(DbImportStateBase state, Class<? extends CdmBase> destinationClass){
40 if (!isInitialized){
41 // this.dbTableName = tableName;
42 this.destinationClass = destinationClass;
43 for (CdmAttributeMapperBase mapper: this.mapperList){
44 if (mapper instanceof IDbImportMapper){
45 ((IDbImportMapper) mapper).initialize(state, destinationClass);
46 }
47 }
48 isInitialized = true;
49 if (secondPathMapping != null){
50 secondPathMapping.initialize(state, destinationClass);
51 }
52 }
53 return true;
54 }
55
56 /**
57 * Invokes the second path mapping if one has been defined
58 * @param rs
59 * @param objectsToSave
60 * @return
61 * @throws SQLException
62 */
63 public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave) throws SQLException{
64 return invoke(rs, objectsToSave, false);
65 }
66
67 /**
68 * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
69 * @param rs
70 * @param objectsToSave
71 * @param secondPath
72 * @return
73 * @throws SQLException
74 */
75 public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave, boolean secondPath) throws SQLException{
76 boolean result = true;
77 if (secondPath == true && secondPathMapping != null){
78 return secondPathMapping.invoke(rs, objectsToSave);
79 } else {
80 CdmBase objectToSave = null;
81 // try {
82 for (CdmAttributeMapperBase mapper : this.mapperList){
83 if (mapper instanceof IDbImportMapper){
84 IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper)mapper;
85 try {
86 objectToSave = dbMapper.invoke(rs, objectToSave);
87 } catch (Exception e) {
88 result = false;
89 logger.error("Error occurred in mapping.invoke");
90 e.printStackTrace();
91 continue;
92 }
93 }else{
94 logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());
95 }
96 }
97 if (objectToSave != null){
98 objectsToSave.add(objectToSave);
99 }else{
100 logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");
101 }
102 return result;
103 }
104 }
105
106 public void setSecondPathMapping(DbImportMapping secondPathMapping){
107 this.secondPathMapping = secondPathMapping;
108 }
109
110 // /**
111 // * @return the berlinModelTableName
112 // */
113 // public String getDbTableName() {
114 // return dbTableName;
115 // }
116 //
117 // /**
118 // * @param berlinModelTableName the berlinModelTableName to set
119 // */
120 // public void setDbTableName(String dbTableName) {
121 // this.dbTableName = dbTableName;
122 // }
123 //
124 //
125 // protected List<CdmAttributeMapperBase> getAttributeMapperList(){
126 // List<CdmAttributeMapperBase> list = this.mapperList;
127 // return list;
128 // }
129
130
131 }