View Javadoc

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.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   * @author a.mueller
29   * @created 12.05.2009
30   * @version 1.0
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  //	public static RefDetailMapper NewInstance(String cdmAttributeString, String dbAttributeString){
43  //		return new RefDetailMapper();
44  //	}
45  
46  	
47  	/**
48  	 * @param dbAttributString
49  	 * @param cdmAttributeString
50  	 */
51  	private RefDetailMapper(String cdmAttributeString, String cdmRefAttributeString, String dbAttributeString) {
52  		super(cdmAttributeString, dbAttributeString, null);
53  		this.cdmRefAttributeString = cdmRefAttributeString;
54  	}
55  
56  	
57  	
58  	/* (non-Javadoc)
59  	 * @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)
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  	/* (non-Javadoc)
79  	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue()
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  //		getState().getConfig().getCdmAppController().commitTransaction(tx);
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  				//TODO microRef with no reference
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 //		String fullRefCache = null;
105 //		String fullNomRefCache = null;
106 		Boolean preliminaryFlag = false;
107 //		String secondarySources = null;
108 		java.sql.Date created_When = new java.sql.Date(new Date().getTime());
109 //		java.sql.Date updated_When = null;
110 		String created_who = "autom.";
111 //		String update_who = null;
112 //		String notes = null;
113 		
114 		try {
115 			preparedStatement.setInt(1, refDetailId);
116 			preparedStatement.setInt(2, refId);
117 			preparedStatement.setNull(3, Types.VARCHAR) ;//.setString(3, fullRefCache);
118 			preparedStatement.setNull(4, Types.VARCHAR) ;//.setString(4, fullNomRefCache);
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) ;//.setString(7, secondarySources);
126 			preparedStatement.setDate(8, created_When);
127 			preparedStatement.setString(9, created_who);
128 			preparedStatement.setNull(10, Types.DATE) ;//.setDate(10, updated_When);
129 			preparedStatement.setNull(11, Types.VARCHAR) ;//.setString(11, update_who);
130 			preparedStatement.setNull(12, Types.VARCHAR) ;//.setString(12, notes);
131 			preparedStatement.setNull(13, Types.VARCHAR) ;//.setString(13, secondarySources);
132 			
133 			
134 			preparedStatement.executeUpdate();
135 		} catch (SQLException e) {
136 			// TODO Auto-generated catch block
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 	/* (non-Javadoc)
153 	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
154 	 */
155 	@Override
156 	protected int getSqlType() {
157 		return Types.INTEGER;
158 	}
159 
160 	/* (non-Javadoc)
161 	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
162 	 */
163 	@Override
164 	public Class<?> getTypeClass() {
165 		return String.class;
166 	}
167 
168 
169 
170 
171 }