Project

General

Profile

Download (5.31 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.io.berlinModel.out.mapper;
11

    
12
import java.sql.Connection;
13
import java.sql.PreparedStatement;
14
import java.sql.SQLException;
15
import java.sql.Types;
16
import java.util.Date;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportConfigurator;
21
import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportState;
22
import eu.etaxonomy.cdm.io.common.ImportHelper;
23
import eu.etaxonomy.cdm.io.common.mapping.out.DbSingleAttributeExportMapperBase;
24
import eu.etaxonomy.cdm.io.common.mapping.out.IDbExportMapper;
25
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
26
import eu.etaxonomy.cdm.io.common.mapping.out.IndexCounter;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29

    
30
/**
31
 * @author a.mueller
32
 * @since 12.05.2009
33
 */
34
public class RefDetailMapper extends DbSingleAttributeExportMapperBase<BerlinModelExportState>
35
        implements IDbExportMapper<BerlinModelExportState, IExportTransformer>{
36
	private static final Logger logger = Logger.getLogger(RefDetailMapper.class);
37

    
38
	private final String cdmRefAttributeString;
39
	private PreparedStatement preparedStatement;
40

    
41
	public static RefDetailMapper NewInstance(String cdmAttributeString, String cdmRefAttributeString, String dbAttributeString){
42
		return new RefDetailMapper(cdmAttributeString, cdmRefAttributeString, dbAttributeString);
43
	}
44

    
45
//	public static RefDetailMapper NewInstance(String cdmAttributeString, String dbAttributeString){
46
//		return new RefDetailMapper();
47
//	}
48

    
49

    
50
	/**
51
	 * @param dbAttributString
52
	 * @param cdmAttributeString
53
	 */
54
	private RefDetailMapper(String cdmAttributeString, String cdmRefAttributeString, String dbAttributeString) {
55
		super(cdmAttributeString, dbAttributeString, null);
56
		this.cdmRefAttributeString = cdmRefAttributeString;
57
	}
58

    
59
	@Override
60
	public void initialize(PreparedStatement stmt, IndexCounter index,BerlinModelExportState state, String tableName) {
61
		super.initialize(stmt, index, state, tableName);
62
		String inRefSql = "INSERT INTO RefDetail (RefDetailId, RefFk , " +
63
	 		" FullRefCache, FullNomRefCache, PreliminaryFlag , Details , " +
64
	 		" SecondarySources, " +
65
	 		" Created_When , Created_Who , Updated_When, Updated_Who, Notes ,IdInSource)"+
66
	 		" VALUES (?,?, ?,?,?,?, ?, ?,?,?,?,?,?)";
67
		Connection con = getState().getConfig().getDestination().getConnection();
68
		try {
69
			preparedStatement = con.prepareStatement(inRefSql);
70
		} catch (SQLException e) {
71
			e.printStackTrace();
72
			throw new RuntimeException();
73
		}
74
	}
75

    
76
	@Override
77
	protected Object getValue(CdmBase cdmBase) {
78
		String value = (String)super.getValue(cdmBase);
79
		boolean isBoolean = false;
80
		Reference ref = (Reference)ImportHelper.getValue(cdmBase, this.cdmRefAttributeString, isBoolean, true);
81
		Object result = makeRefDetail(value, ref);
82
//		getState().getConfig().getCdmAppController().commitTransaction(tx);
83
		return result;
84
	}
85

    
86

    
87
	protected Integer makeRefDetail(String microRef, Reference ref){
88
		if (ref == null){
89
			if (microRef == null || microRef.trim().equals("")){
90
				return null;
91
			}else{
92
				//TODO microRef with no reference
93
				logger.warn("ref == null not yet implemented");
94
				return null;
95
			}
96
		}
97
		Integer refDetailId = getState().getNextRefDetailId();
98
		Integer refId = getState().getDbId(ref);
99
//		String fullRefCache = null;
100
//		String fullNomRefCache = null;
101
		Boolean preliminaryFlag = false;
102
//		String secondarySources = null;
103
		java.sql.Date created_When = new java.sql.Date(new Date().getTime());
104
//		java.sql.Date updated_When = null;
105
		String created_who = "autom.";
106
//		String update_who = null;
107
//		String notes = null;
108

    
109
		try {
110
			preparedStatement.setInt(1, refDetailId);
111
			preparedStatement.setInt(2, refId);
112
			preparedStatement.setNull(3, Types.VARCHAR) ;//.setString(3, fullRefCache);
113
			preparedStatement.setNull(4, Types.VARCHAR) ;//.setString(4, fullNomRefCache);
114
			preparedStatement.setBoolean(5, preliminaryFlag);
115
			if (microRef != null){
116
				preparedStatement.setString(6, microRef);
117
			}else{
118
				preparedStatement.setNull(6, Types.VARCHAR);
119
			}
120
			preparedStatement.setNull(7, Types.VARCHAR) ;//.setString(7, secondarySources);
121
			preparedStatement.setDate(8, created_When);
122
			preparedStatement.setString(9, created_who);
123
			preparedStatement.setNull(10, Types.DATE) ;//.setDate(10, updated_When);
124
			preparedStatement.setNull(11, Types.VARCHAR) ;//.setString(11, update_who);
125
			preparedStatement.setNull(12, Types.VARCHAR) ;//.setString(12, notes);
126
			preparedStatement.setNull(13, Types.VARCHAR) ;//.setString(13, secondarySources);
127

    
128

    
129
			preparedStatement.executeUpdate();
130
		} catch (SQLException e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		}
134
		return refDetailId;
135
	}
136

    
137
	protected Integer getId(CdmBase cdmBase){
138
		BerlinModelExportConfigurator config = getState().getConfig();
139
		if (false && config.getIdType() == BerlinModelExportConfigurator.IdType.CDM_ID){
140
			return cdmBase.getId();
141
		}else{
142
			Integer id = getState().getDbId(cdmBase);
143
			return id;
144
		}
145
	}
146

    
147
	@Override
148
	protected int getSqlType() {
149
		return Types.INTEGER;
150
	}
151

    
152
	@Override
153
	public Class<?> getTypeClass() {
154
		return String.class;
155
	}
156

    
157
}
(2-2/3)