added FEATURE_STORE to be compliant with XML-IMPORT for cdmlib-app
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / DbExportBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 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 package eu.etaxonomy.cdm.io.common;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.io.common.DbExportConfiguratorBase.IdType;
18 import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
19 import eu.etaxonomy.cdm.model.common.CdmBase;
20
21 /**
22 * @author a.mueller
23 * @author e.-m.lee
24 * @date 17.02.2010
25 *
26 */
27 public abstract class DbExportBase<CONFIG extends DbExportConfiguratorBase<STATE, TRANSFORM>, STATE extends ExportStateBase<CONFIG, TRANSFORM>, TRANSFORM extends IExportTransformer> extends CdmExportBase<CONFIG, STATE, TRANSFORM> {
28 private static Logger logger = Logger.getLogger(DbExportBase.class);
29
30 protected boolean checkSqlServerColumnExists(Source source, String tableName, String columnName){
31 String strQuery = "SELECT Count(t.id) as n " +
32 " FROM sysobjects AS t " +
33 " INNER JOIN syscolumns AS c ON t.id = c.id " +
34 " WHERE (t.xtype = 'U') AND " +
35 " (t.name = '" + tableName + "') AND " +
36 " (c.name = '" + columnName + "')";
37 ResultSet rs = source.getResultSet(strQuery) ;
38 int n;
39 try {
40 rs.next();
41 n = rs.getInt("n");
42 return n>0;
43 } catch (SQLException e) {
44 e.printStackTrace();
45 return false;
46 }
47
48 }
49
50 public abstract Class<? extends CdmBase> getStandardMethodParameter();
51
52 protected void doCount(int count, int modCount, String pluralString){
53 if ((count % modCount ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
54 }
55
56 @Override
57 public Object getDbId(CdmBase cdmBase, STATE state) {
58 CONFIG config = state.getConfig();
59 IdType type = config.getIdType();
60 if (cdmBase == null){
61 return null;
62 }
63 if (type == IdType.CDM_ID){
64 return cdmBase.getId();
65 }else if (type == IdType.CDM_ID_WITH_EXCEPTIONS){
66 return getDbIdCdmWithExceptions(cdmBase, state);
67 }else if(type == IdType.MAX_ID){
68 //TODO
69 logger.warn("MAX_ID not yet implemented");
70 return cdmBase.getId();
71 }else if(type == IdType.ORIGINAL_SOURCE_ID){
72 //TODO
73 logger.warn("ORIGINAL_SOURCE_ID not yet implemented");
74 return cdmBase.getId();
75 }else{
76 logger.warn("Unknown idType: " + type);
77 return cdmBase.getId();
78 }
79
80 }
81
82 protected Object getDbIdCdmWithExceptions(CdmBase cdmBase, STATE state) {
83 //default -> override
84 return cdmBase.getId();
85 }
86
87
88
89 }