1
2
3
4
5
6
7
8
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.util.Date;
17
18 import org.apache.log4j.Logger;
19 import org.hsqldb.Types;
20
21 import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportState;
22 import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportConfigurator;
23 import eu.etaxonomy.cdm.io.common.ImportHelper;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
26
27
28
29
30
31
32 public class RefDetailMapper extends DbSingleAttributeExportMapperBase<BerlinModelExportState> implements IDbExportMapper<BerlinModelExportState>{
33 private static final Logger logger = Logger.getLogger(RefDetailMapper.class);
34
35 private String cdmRefAttributeString;
36 private PreparedStatement preparedStatement;
37
38 public static RefDetailMapper NewInstance(String cdmAttributeString, String cdmRefAttributeString, String dbAttributeString){
39 return new RefDetailMapper(cdmAttributeString, cdmRefAttributeString, dbAttributeString);
40 }
41
42
43
44
45
46
47
48
49
50
51 private RefDetailMapper(String cdmAttributeString, String cdmRefAttributeString, String dbAttributeString) {
52 super(cdmAttributeString, dbAttributeString, null);
53 this.cdmRefAttributeString = cdmRefAttributeString;
54 }
55
56
57
58
59
60
61 @Override
62 public void initialize(PreparedStatement stmt, IndexCounter index,BerlinModelExportState state, String tableName) {
63 super.initialize(stmt, index, state, tableName);
64 String inRefSql = "INSERT INTO RefDetail (RefDetailId, RefFk , " +
65 " FullRefCache, FullNomRefCache, PreliminaryFlag , Details , " +
66 " SecondarySources, " +
67 " Created_When , Created_Who , Updated_When, Updated_Who, Notes ,IdInSource)"+
68 " VALUES (?,?, ?,?,?,?, ?, ?,?,?,?,?,?)";
69 Connection con = getState().getConfig().getDestination().getConnection();
70 try {
71 preparedStatement = con.prepareStatement(inRefSql);
72 } catch (SQLException e) {
73 e.printStackTrace();
74 throw new RuntimeException();
75 }
76 }
77
78
79
80
81 @Override
82 protected Object getValue(CdmBase cdmBase) {
83 String value = (String)super.getValue(cdmBase);
84 boolean isBoolean = false;
85 ReferenceBase<?> ref = (ReferenceBase<?>)ImportHelper.getValue(cdmBase, this.cdmRefAttributeString, isBoolean, true);
86 Object result = makeRefDetail(value, ref);
87
88 return result;
89 }
90
91
92 protected Integer makeRefDetail(String microRef, ReferenceBase<?> ref){
93 if (ref == null){
94 if (microRef == null || microRef.trim().equals("")){
95 return null;
96 }else{
97
98 logger.warn("ref == null not yet implemented");
99 return null;
100 }
101 }
102 Integer refDetailId = getState().getNextRefDetailId();
103 Integer refId = getState().getDbId(ref);
104
105
106 Boolean preliminaryFlag = false;
107
108 java.sql.Date created_When = new java.sql.Date(new Date().getTime());
109
110 String created_who = "autom.";
111
112
113
114 try {
115 preparedStatement.setInt(1, refDetailId);
116 preparedStatement.setInt(2, refId);
117 preparedStatement.setNull(3, Types.VARCHAR) ;
118 preparedStatement.setNull(4, Types.VARCHAR) ;
119 preparedStatement.setBoolean(5, preliminaryFlag);
120 if (microRef != null){
121 preparedStatement.setString(6, microRef);
122 }else{
123 preparedStatement.setNull(6, Types.VARCHAR);
124 }
125 preparedStatement.setNull(7, Types.VARCHAR) ;
126 preparedStatement.setDate(8, created_When);
127 preparedStatement.setString(9, created_who);
128 preparedStatement.setNull(10, Types.DATE) ;
129 preparedStatement.setNull(11, Types.VARCHAR) ;
130 preparedStatement.setNull(12, Types.VARCHAR) ;
131 preparedStatement.setNull(13, Types.VARCHAR) ;
132
133
134 preparedStatement.executeUpdate();
135 } catch (SQLException e) {
136
137 e.printStackTrace();
138 }
139 return refDetailId;
140 }
141
142 protected Integer getId(CdmBase cdmBase){
143 BerlinModelExportConfigurator config = getState().getConfig();
144 if (false && config.getIdType() == BerlinModelExportConfigurator.IdType.CDM_ID){
145 return cdmBase.getId();
146 }else{
147 Integer id = getState().getDbId(cdmBase);
148 return id;
149 }
150 }
151
152
153
154
155 @Override
156 protected int getSqlType() {
157 return Types.INTEGER;
158 }
159
160
161
162
163 @Override
164 public Class<?> getTypeClass() {
165 return String.class;
166 }
167
168
169
170
171 }