Project

General

Profile

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

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

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
21

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

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

    
54
//************************************ METHODS *******************************************/
55

    
56
	/* (non-Javadoc)
57
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
58
	 */
59
	@Override
60
	public TERM invoke(ResultSet rs, TERM noObject) throws SQLException {
61
		TERM definedTerm = getDefinedTermIfExist(rs);
62
		if (definedTerm == null){
63
			definedTerm = super.invoke(rs, noObject);
64
			getState().getCurrentIO().getTermService().save(definedTerm);
65
		}
66
		getState().addRelatedObject(objectToCreateNamespace, getKeyString(rs), definedTerm);
67
		return definedTerm;
68
	}
69

    
70
	/**
71
	 * @return
72
	 * @throws SQLException 
73
	 */
74
	private TERM getDefinedTermIfExist(ResultSet rs) throws SQLException {
75
		//get object from transformer, return if not null
76
		TERM definedTerm = getTermFromTransformer(rs);
77
		if (definedTerm == null){
78
			//if null get uuid from transformer
79
			UUID uuidTerm = getUuidFromTransformer(rs);
80
			if (uuidTerm != null){
81
				definedTerm = getTermByUuid(uuidTerm, rs);
82
			}else{
83
				definedTerm = getTermByIdentifier(rs);
84
			}
85
		}
86
		return definedTerm;
87
	}
88

    
89
	/**
90
	 * @return
91
	 * @throws SQLException 
92
	 */
93
	protected TERM getTermByIdentifier(ResultSet rs) throws SQLException {
94
		//get object from stat.featur map
95
		String key = getKeyString(rs);
96
		if (key == null){
97
			return null;
98
		}
99
		TERM term = (TERM)getState().getRelatedObject(objectToCreateNamespace, key);
100
		return term;
101
	}
102

    
103

    
104
	/**
105
	 * @param uuidTerm
106
	 * @return
107
	 * @throws SQLException 
108
	 */
109
	protected TERM getTermByUuid(UUID uuidTerm, ResultSet rs) throws SQLException{
110
		TERM term = getTermFromState(uuidTerm);
111
		if (term == null){
112
			term = (TERM)getState().getCurrentIO().getTermService().find(uuidTerm);
113
			if (term != null){
114
				saveTermToState(term);
115
			}
116
		}
117
		return term;
118
	}
119

    
120

    
121

    
122
	/**
123
	 * Saves the defined term to the state
124
	 * @param rs
125
	 */
126
	protected abstract void saveTermToState(TERM term);
127

    
128
	/**
129
	 * @param rs
130
	 * @return
131
	 */
132
	protected abstract TERM getTermFromState(UUID uuid);
133

    
134
	
135
	/**
136
	 * @return
137
	 * @throws SQLException 
138
	 */
139
	protected UUID getUuidFromTransformer(ResultSet rs) throws SQLException{
140
		IInputTransformer transformer = getTransformer();
141
		String key = getKeyString(rs);
142
		UUID uuid;
143
		try {
144
			uuid = getUuidFromTransformer(key, transformer);
145
		} catch (UndefinedTransformerMethodException e) {
146
			logger.warn(e.getMessage());
147
			return null;
148
		}
149
		return uuid;
150
	}
151

    
152
	/**
153
	 * @return
154
	 * @throws UndefinedTransformerMethodException 
155
	 */
156
	protected abstract UUID getUuidFromTransformer(String key, IInputTransformer transformer) throws UndefinedTransformerMethodException;
157

    
158
	
159
	/**
160
	 * @param rs 
161
	 * @return
162
	 * @throws SQLException 
163
	 */
164
	protected TERM getTermFromTransformer(ResultSet rs) throws SQLException{
165
		IInputTransformer transformer = getTransformer();
166
		String key = getKeyString(rs);
167
		TERM term;
168
		try {
169
			term = getTermFromTransformer(key, transformer);
170
		} catch (UndefinedTransformerMethodException e) {
171
			logger.warn(e.getMessage());
172
			return null;
173
		}
174
		return term;
175
	}
176

    
177

    
178
	/**
179
	 * @param key
180
	 * @param transformer
181
	 * @return
182
	 * @throws UndefinedTransformerMethodException 
183
	 */
184
	protected abstract TERM getTermFromTransformer(String key, IInputTransformer transformer) throws UndefinedTransformerMethodException;
185

    
186
	/**
187
	 * @param rs
188
	 * @return
189
	 * @throws SQLException
190
	 */
191
	protected String getKeyString(ResultSet rs) throws SQLException {
192
		Object oKey = rs.getString(dbIdAttribute);
193
		String key = String.valueOf(oKey);
194
		return key;
195
	}
196
	
197
	/* (non-Javadoc)
198
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#doInvoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
199
	 */
200
	@Override
201
	protected TERM doInvoke(ResultSet rs, TERM createdObject)
202
			throws SQLException {
203
		return createdObject; //not needed because invoke is implemented separately
204
	}
205

    
206
	
207
}
(8-8/40)