Project

General

Profile

Download (5.93 KB) Statistics
| Branch: | Revision:
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.berlinModel.out.mapper;
12

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

    
19
import org.apache.log4j.Logger;
20

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

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

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

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

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

    
50

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

    
60

    
61

    
62
	/* (non-Javadoc)
63
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#initialize(java.sql.PreparedStatement, eu.etaxonomy.cdm.io.berlinModel.out.mapper.IndexCounter, eu.etaxonomy.cdm.io.berlinModel.out.DbExportState)
64
	 */
65
	@Override
66
	public void initialize(PreparedStatement stmt, IndexCounter index,BerlinModelExportState state, String tableName) {
67
		super.initialize(stmt, index, state, tableName);
68
		String inRefSql = "INSERT INTO RefDetail (RefDetailId, RefFk , " +
69
	 		" FullRefCache, FullNomRefCache, PreliminaryFlag , Details , " +
70
	 		" SecondarySources, " +
71
	 		" Created_When , Created_Who , Updated_When, Updated_Who, Notes ,IdInSource)"+
72
	 		" VALUES (?,?, ?,?,?,?, ?, ?,?,?,?,?,?)";
73
		Connection con = getState().getConfig().getDestination().getConnection();
74
		try {
75
			preparedStatement = con.prepareStatement(inRefSql);
76
		} catch (SQLException e) {
77
			e.printStackTrace();
78
			throw new RuntimeException();
79
		}
80
	}
81

    
82
	/* (non-Javadoc)
83
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue()
84
	 */
85
	@Override
86
	protected Object getValue(CdmBase cdmBase) {
87
		String value = (String)super.getValue(cdmBase);
88
		boolean isBoolean = false;
89
		Reference<?> ref = (Reference<?>)ImportHelper.getValue(cdmBase, this.cdmRefAttributeString, isBoolean, true);
90
		Object result = makeRefDetail(value, ref);
91
//		getState().getConfig().getCdmAppController().commitTransaction(tx);
92
		return result;
93
	}
94

    
95

    
96
	protected Integer makeRefDetail(String microRef, Reference<?> ref){
97
		if (ref == null){
98
			if (microRef == null || microRef.trim().equals("")){
99
				return null;
100
			}else{
101
				//TODO microRef with no reference
102
				logger.warn("ref == null not yet implemented");
103
				return null;
104
			}
105
		}
106
		Integer refDetailId = getState().getNextRefDetailId();
107
		Integer refId = getState().getDbId(ref);
108
//		String fullRefCache = null;
109
//		String fullNomRefCache = null;
110
		Boolean preliminaryFlag = false;
111
//		String secondarySources = null;
112
		java.sql.Date created_When = new java.sql.Date(new Date().getTime());
113
//		java.sql.Date updated_When = null;
114
		String created_who = "autom.";
115
//		String update_who = null;
116
//		String notes = null;
117

    
118
		try {
119
			preparedStatement.setInt(1, refDetailId);
120
			preparedStatement.setInt(2, refId);
121
			preparedStatement.setNull(3, Types.VARCHAR) ;//.setString(3, fullRefCache);
122
			preparedStatement.setNull(4, Types.VARCHAR) ;//.setString(4, fullNomRefCache);
123
			preparedStatement.setBoolean(5, preliminaryFlag);
124
			if (microRef != null){
125
				preparedStatement.setString(6, microRef);
126
			}else{
127
				preparedStatement.setNull(6, Types.VARCHAR);
128
			}
129
			preparedStatement.setNull(7, Types.VARCHAR) ;//.setString(7, secondarySources);
130
			preparedStatement.setDate(8, created_When);
131
			preparedStatement.setString(9, created_who);
132
			preparedStatement.setNull(10, Types.DATE) ;//.setDate(10, updated_When);
133
			preparedStatement.setNull(11, Types.VARCHAR) ;//.setString(11, update_who);
134
			preparedStatement.setNull(12, Types.VARCHAR) ;//.setString(12, notes);
135
			preparedStatement.setNull(13, Types.VARCHAR) ;//.setString(13, secondarySources);
136

    
137

    
138
			preparedStatement.executeUpdate();
139
		} catch (SQLException e) {
140
			// TODO Auto-generated catch block
141
			e.printStackTrace();
142
		}
143
		return refDetailId;
144
	}
145

    
146
	protected Integer getId(CdmBase cdmBase){
147
		BerlinModelExportConfigurator config = getState().getConfig();
148
		if (false && config.getIdType() == BerlinModelExportConfigurator.IdType.CDM_ID){
149
			return cdmBase.getId();
150
		}else{
151
			Integer id = getState().getDbId(cdmBase);
152
			return id;
153
		}
154
	}
155

    
156
	/* (non-Javadoc)
157
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
158
	 */
159
	@Override
160
	protected int getSqlType() {
161
		return Types.INTEGER;
162
	}
163

    
164
	/* (non-Javadoc)
165
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
166
	 */
167
	@Override
168
	public Class<?> getTypeClass() {
169
		return String.class;
170
	}
171

    
172

    
173

    
174

    
175
}
(2-2/3)