Project

General

Profile

Download (7.75 KB) Statistics
| Branch: | Tag: | 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.common.mapping;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
20
import eu.etaxonomy.cdm.model.common.VersionableEntity;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22

    
23
/**
24
 * This class either retrieves a defined Term from the database or creates and saves it in the database.
25
 * This is done by first checking the according transformer for term attributes map to an existing 
26
 * CDM term (term that is available by static methods in the term classes), if not it tries to retrieve
27
 * it via its uuid available through the transformer, if not retrievable it tries to retrieve from the
28
 * database via the original source, if not retrievable it creates the new and stores it in the database.
29
 * @author a.mueller
30
 * @since 12.05.2009
31
 * @version 1.0
32
 * @param <TERM>The class of the term to be created or retrieved.
33
 * @param <TERMED>The class of the object the term is added to.
34
 * @param <STATE>The class of the import state thate is available during import.
35
 */
36
public abstract class DbImportDefinedTermCreationMapperBase<TERM extends DefinedTermBase, TERMED extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<TERMED, STATE>  {
37
	private static final Logger logger = Logger.getLogger(DbImportDefinedTermCreationMapperBase.class);
38
	
39

    
40
//******************************* ATTRIBUTES ***************************************/
41
	protected String dbTermAttribute;
42
	protected String dbLabelAttribute;
43
	protected String dbLabelAbbrevAttribute;
44
	
45
//********************************* CONSTRUCTOR ****************************************/
46
	/**
47
	 * @param mappingImport
48
	 */
49
	protected DbImportDefinedTermCreationMapperBase(String dbIdAttribute, String termNamespace, String dbTermAttribute, String dbLabelAttribute, String dbLabelAbbrevAttribute) {
50
		super(dbIdAttribute, termNamespace);
51
		this.dbTermAttribute = dbTermAttribute;
52
		this.dbLabelAttribute = dbLabelAttribute;
53
		this.dbLabelAbbrevAttribute = dbLabelAbbrevAttribute;
54
	}
55

    
56
//************************************ METHODS *******************************************/
57

    
58
	/* (non-Javadoc)
59
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
60
	 */
61
	@Override
62
	public TERMED invoke(ResultSet rs, TERMED noObject) throws SQLException {
63
		String key = getKeyString(rs);
64
		if (key != null){
65
			TERM definedTerm = getDefinedTermIfExist(rs);
66
			if (definedTerm == null){
67
				definedTerm = createDefinedTerm(rs);
68
				Reference citation = null;
69
				getState().getCurrentIO().addOriginalSource(rs, definedTerm, dbIdAttribute, objectToCreateNamespace, citation);
70
				
71
				UUID transformerUuid = getUuidFromTransformer(rs);
72
				if (transformerUuid != null){
73
					definedTerm.setUuid(transformerUuid);
74
				}
75
				getState().getCurrentIO().getTermService().save(definedTerm);
76
				saveTermToState(definedTerm);
77
				if (transformerUuid == null){
78
					getState().addRelatedObject(objectToCreateNamespace, getKeyString(rs), definedTerm);
79
				}
80
			}
81
		}
82
		return noObject;
83
	}
84

    
85
	/**
86
	 * @param rs
87
	 * @param definedTerm
88
	 * @throws SQLException
89
	 */
90
	private void makeUuid(ResultSet rs, TERM definedTerm) throws SQLException {
91
		UUID uuid = getUuidFromTransformer(rs);
92
		if (uuid != null){
93
			definedTerm.setUuid(uuid);
94
		}
95
	}
96

    
97
	/**
98
	 * @param rs
99
	 * @return
100
	 * @throws SQLException 
101
	 */
102
	protected abstract TERM createDefinedTerm(ResultSet rs) throws SQLException;
103

    
104
	/**
105
	 * Returns the term if it is available via the transformer, or via the state (uuidMap or relatedObject)
106
	 * @return
107
	 * @throws SQLException 
108
	 */
109
	private TERM getDefinedTermIfExist(ResultSet rs) throws SQLException {
110
		//get object from transformer, return if not null
111
		TERM definedTerm = getTermFromTransformer(rs);
112
		if (definedTerm == null){
113
			//if null get uuid from transformer
114
			UUID uuidTerm = getUuidFromTransformer(rs);
115
			if (uuidTerm != null){
116
				definedTerm = getTermByUuid(uuidTerm, rs);
117
			}else{
118
				definedTerm = getTermByIdentifier(rs);
119
			}
120
		}
121
		return definedTerm;
122
	}
123

    
124
	/**
125
	 * @return
126
	 * @throws SQLException 
127
	 */
128
	protected TERM getTermByIdentifier(ResultSet rs) throws SQLException {
129
		//get object from stat.featur map
130
		String key = getKeyString(rs);
131
		if (key == null){
132
			return null;
133
		}
134
		TERM term = (TERM)getState().getRelatedObject(objectToCreateNamespace, key);
135
		return term;
136
	}
137

    
138

    
139
	/**
140
	 * @param uuidTerm
141
	 * @return
142
	 * @throws SQLException 
143
	 */
144
	protected TERM getTermByUuid(UUID uuidTerm, ResultSet rs) throws SQLException{
145
		TERM term = getTermFromState(uuidTerm);
146
		if (term == null){
147
			term = (TERM)getState().getCurrentIO().getTermService().find(uuidTerm);
148
			if (term != null){
149
				saveTermToState(term);
150
			}
151
		}
152
		return term;
153
	}
154

    
155

    
156

    
157
	/**
158
	 * Saves the defined term to the state
159
	 * @param rs
160
	 */
161
	protected abstract void saveTermToState(TERM term);
162

    
163
	/**
164
	 * @param rs
165
	 * @return
166
	 */
167
	protected abstract TERM getTermFromState(UUID uuid);
168

    
169
	
170
	/**
171
	 * @return
172
	 * @throws SQLException 
173
	 */
174
	protected UUID getUuidFromTransformer(ResultSet rs) throws SQLException{
175
		IInputTransformer transformer = getTransformer();
176
		String key = getKeyString(rs);
177
		UUID uuid;
178
		try {
179
			uuid = getUuidFromTransformer(key, transformer);
180
		} catch (UndefinedTransformerMethodException e) {
181
			logger.warn(e.getMessage());
182
			return null;
183
		}
184
		return uuid;
185
	}
186

    
187
	/**
188
	 * @return
189
	 * @throws UndefinedTransformerMethodException 
190
	 */
191
	protected abstract UUID getUuidFromTransformer(String key, IInputTransformer transformer) throws UndefinedTransformerMethodException;
192

    
193
	
194
	/**
195
	 * @param rs 
196
	 * @return
197
	 * @throws SQLException 
198
	 */
199
	protected TERM getTermFromTransformer(ResultSet rs) throws SQLException{
200
		IInputTransformer transformer = getTransformer();
201
		String key = getKeyString(rs);
202
		TERM term;
203
		try {
204
			term = getTermFromTransformer(key, transformer);
205
		} catch (UndefinedTransformerMethodException e) {
206
			logger.warn(e.getMessage());
207
			return null;
208
		}
209
		return term;
210
	}
211

    
212

    
213
	/**
214
	 * @param key
215
	 * @param transformer
216
	 * @return
217
	 * @throws UndefinedTransformerMethodException 
218
	 */
219
	protected abstract TERM getTermFromTransformer(String key, IInputTransformer transformer) throws UndefinedTransformerMethodException;
220

    
221
	/**
222
	 * @param rs
223
	 * @return
224
	 * @throws SQLException
225
	 */
226
	protected String getKeyString(ResultSet rs) throws SQLException {
227
		Object oKey = rs.getString(dbIdAttribute);
228
		if (oKey == null){
229
			return null;
230
		}
231
		String key = String.valueOf(oKey);
232
		key = key.trim();
233
		return key;
234
	}
235
	
236
	/* (non-Javadoc)
237
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#doInvoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
238
	 */
239
	@Override
240
	protected TERMED doInvoke(ResultSet rs, TERMED createdObject)
241
			throws SQLException {
242
		return createdObject; //not needed because invoke is implemented separately
243
	}
244

    
245
	/* (non-Javadoc)
246
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#createObject(java.sql.ResultSet)
247
	 */
248
	@Override
249
	protected TERMED createObject(ResultSet rs) throws SQLException {
250
		logger.warn("Never should read this");
251
		return null; //not needed, object is created in createDefineTerm(rs)
252
	}
253

    
254
	
255
	
256
}
(9-9/51)