Project

General

Profile

Download (10.6 KB) Statistics
| Branch: | 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.eflora.centralAfrica.checklist;
11

    
12
import java.sql.ResultSet;
13
import java.sql.ResultSetMetaData;
14
import java.sql.SQLException;
15
import java.sql.Timestamp;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.apache.log4j.Logger;
23
import org.joda.time.DateTime;
24

    
25
import eu.etaxonomy.cdm.common.CdmUtils;
26
import eu.etaxonomy.cdm.io.common.CdmImportBase;
27
import eu.etaxonomy.cdm.io.common.ICdmIO;
28
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
29
import eu.etaxonomy.cdm.io.common.ImportHelper;
30
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
31
import eu.etaxonomy.cdm.io.common.Source;
32
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
33
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
34
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
35
import eu.etaxonomy.cdm.model.common.Annotation;
36
import eu.etaxonomy.cdm.model.common.AnnotationType;
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.common.ExtensionType;
39
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
40
import eu.etaxonomy.cdm.model.common.Language;
41
import eu.etaxonomy.cdm.model.common.MarkerType;
42
import eu.etaxonomy.cdm.model.common.User;
43

    
44
/**
45
 * @author a.mueller
46
 * @created 20.03.2008
47
 * @version 1.0
48
 */
49
public abstract class CentralAfricaChecklistImportBase<CDM_BASE extends CdmBase> extends CdmImportBase<CentralAfricaChecklistImportConfigurator, CentralAfricaChecklistImportState> implements ICdmIO<CentralAfricaChecklistImportState>, IPartitionedIO<CentralAfricaChecklistImportState> {
50
	private static final Logger logger = Logger.getLogger(CentralAfricaChecklistImportBase.class);
51
	
52
	public static final UUID ID_IN_SOURCE_EXT_UUID = UUID.fromString("23dac094-e793-40a4-bad9-649fc4fcfd44");
53
	
54
	protected static final String SYNONYM_NAMESPACE = "synonyms";
55
	protected static final String TAXON_NAMESPACE = "checklist";
56
	protected static final String REFERENCE_NAMESPACE = "checklist_source";
57
	
58

    
59
	private String pluralString;
60
	private String dbTableName;
61
	//TODO needed?
62
	private Class cdmTargetClass;
63
	
64

    
65
	
66
	
67
	/**
68
	 * @param dbTableName
69
	 * @param dbTableName2 
70
	 */
71
	public CentralAfricaChecklistImportBase(String pluralString, String dbTableName, Class cdmTargetClass) {
72
		this.pluralString = pluralString;
73
		this.dbTableName = dbTableName;
74
		this.cdmTargetClass = cdmTargetClass;
75
	}
76

    
77
	protected boolean doInvoke(CentralAfricaChecklistImportState state){
78
		logger.info("start make " + getPluralString() + " ...");
79
		boolean success = true ;
80
		CentralAfricaChecklistImportConfigurator config = state.getConfig();
81
		Source source = config.getSource();
82
			
83
		String strIdQuery = getIdQuery();
84
		String strRecordQuery = getRecordQuery(config);
85

    
86
		int recordsPerTransaction = config.getRecordsPerTransaction();
87
		try{
88
			ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
89
			while (partitioner.nextPartition()){
90
				partitioner.doPartition(this, state);
91
			}
92
		} catch (SQLException e) {
93
			logger.error("SQLException:" +  e);
94
			return false;
95
		}
96
		
97
		logger.info("end make " + getPluralString() + " ... " + getSuccessString(success));
98
		return success;
99
	}
100
	
101
	public boolean doPartition(ResultSetPartitioner partitioner, CentralAfricaChecklistImportState state) {
102
		boolean success = true ;
103
		Set objectsToSave = new HashSet();
104
		
105
 		DbImportMapping<?, ?> mapping = getMapping();
106
		mapping.initialize(state, cdmTargetClass);
107
		
108
		ResultSet rs = partitioner.getResultSet();
109
		try{
110
			while (rs.next()){
111
				success &= mapping.invoke(rs,objectsToSave);
112
			}
113
		} catch (SQLException e) {
114
			logger.error("SQLException:" +  e);
115
			return false;
116
		}
117
	
118
		partitioner.startDoSave();
119
		getCommonService().save(objectsToSave);
120
		return success;
121
	}
122

    
123

    
124
	
125
	/**
126
	 * @return
127
	 */
128
	protected abstract DbImportMapping<?, ?> getMapping();
129
	
130
	/**
131
	 * @return
132
	 */
133
	protected abstract String getRecordQuery(CentralAfricaChecklistImportConfigurator config);
134

    
135
	/**
136
	 * @return
137
	 */
138
	protected String getIdQuery(){
139
		String result = " SELECT id FROM " + getTableName();
140
		return result;
141
	}
142
	
143
	/* (non-Javadoc)
144
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getPluralString()
145
	 */
146
	public String getPluralString(){
147
		return pluralString;
148
	}
149

    
150
	/**
151
	 * @return
152
	 */
153
	protected String getTableName(){
154
		return this.dbTableName;
155
	}
156
	
157
	protected boolean doIdCreatedUpdatedNotes(CentralAfricaChecklistImportState state, IdentifiableEntity identifiableEntity, ResultSet rs, long id, String namespace)
158
			throws SQLException{
159
		boolean success = true;
160
		//id
161
		success &= ImportHelper.setOriginalSource(identifiableEntity, state.getConfig().getSourceReference(), id, namespace);
162
		//createdUpdateNotes
163
		success &= doCreatedUpdatedNotes(state, identifiableEntity, rs, namespace);
164
		return success;
165
	}
166
	
167
	
168
	protected boolean doCreatedUpdatedNotes(CentralAfricaChecklistImportState state, AnnotatableEntity annotatableEntity, ResultSet rs, String namespace)
169
			throws SQLException{
170

    
171
		CentralAfricaChecklistImportConfigurator config = state.getConfig();
172
		Object createdWhen = rs.getObject("Created_When");
173
		String createdWho = rs.getString("Created_Who");
174
		Object updatedWhen = null;
175
		String updatedWho = null;
176
		try {
177
			updatedWhen = rs.getObject("Updated_When");
178
			updatedWho = rs.getString("Updated_who");
179
		} catch (SQLException e) {
180
			//Table "Name" has no updated when/who
181
		}
182
		String notes = rs.getString("notes");
183
		
184
		boolean success  = true;
185
		
186
		//Created When, Who, Updated When Who
187
		if (config.getEditor() == null || config.getEditor().equals(EDITOR.NO_EDITORS)){
188
			//do nothing
189
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_ANNOTATION)){
190
			String createdAnnotationString = "Berlin Model record was created By: " + String.valueOf(createdWho) + " (" + String.valueOf(createdWhen) + ") ";
191
			if (updatedWhen != null && updatedWho != null){
192
				createdAnnotationString += " and updated By: " + String.valueOf(updatedWho) + " (" + String.valueOf(updatedWhen) + ")";
193
			}
194
			Annotation annotation = Annotation.NewInstance(createdAnnotationString, Language.DEFAULT());
195
			annotation.setCommentator(config.getCommentator());
196
			annotation.setAnnotationType(AnnotationType.TECHNICAL());
197
			annotatableEntity.addAnnotation(annotation);
198
		}else if (config.getEditor().equals(EDITOR.EDITOR_AS_EDITOR)){
199
			User creator = getUser(createdWho, state);
200
			User updator = getUser(updatedWho, state);
201
			DateTime created = getDateTime(createdWhen);
202
			DateTime updated = getDateTime(updatedWhen);
203
			annotatableEntity.setCreatedBy(creator);
204
			annotatableEntity.setUpdatedBy(updator);
205
			annotatableEntity.setCreated(created);
206
			annotatableEntity.setUpdated(updated);
207
		}else {
208
			logger.warn("Editor type not yet implemented: " + config.getEditor());
209
		}
210
		
211
		
212
		//notes
213
		if (CdmUtils.isNotEmpty(notes)){
214
			String notesString = String.valueOf(notes);
215
			if (notesString.length() > 65530 ){
216
				notesString = notesString.substring(0, 65530) + "...";
217
				logger.warn("Notes string is longer than 65530 and was truncated: " + annotatableEntity);
218
			}
219
			Annotation notesAnnotation = Annotation.NewInstance(notesString, null);
220
			//notesAnnotation.setAnnotationType(AnnotationType.EDITORIAL());
221
			//notes.setCommentator(bmiConfig.getCommentator());
222
			annotatableEntity.addAnnotation(notesAnnotation);
223
		}
224
		return success;
225
	}
226

    
227
	
228
	private User getUser(String createdWho, CentralAfricaChecklistImportState state) {
229
		//not relevant here, for users see ERMS import
230
		return null;
231
	}
232
	
233
	private DateTime getDateTime(Object timeString){
234
		if (timeString == null){
235
			return null;
236
		}
237
		DateTime dateTime = null;
238
		if (timeString instanceof Timestamp){
239
			Timestamp timestamp = (Timestamp)timeString;
240
			dateTime = new DateTime(timestamp);
241
		}else{
242
			logger.warn("time ("+timeString+") is not a timestamp. Datetime set to current date. ");
243
			dateTime = new DateTime();
244
		}
245
		return dateTime;
246
	}
247
	
248
	
249
	/**
250
	 * Returns a map that holds all values of a ResultSet. This is needed if a value needs to
251
	 * be accessed twice
252
	 * @param rs
253
	 * @return
254
	 * @throws SQLException
255
	 */
256
	protected Map<String, Object> getValueMap(ResultSet rs) throws SQLException{
257
		try{
258
			Map<String, Object> valueMap = new HashMap<String, Object>();
259
			int colCount = rs.getMetaData().getColumnCount();
260
			for (int c = 0; c < colCount ; c++){
261
				Object value = rs.getObject(c+1);
262
				String label = rs.getMetaData().getColumnLabel(c+1).toLowerCase();
263
				if (value != null && ! CdmUtils.Nz(value.toString()).trim().equals("")){
264
					valueMap.put(label, value);
265
				}
266
			}
267
			return valueMap;
268
		}catch(SQLException e){
269
			throw e;
270
		}
271
	}
272
	
273
	protected ExtensionType getExtensionType(UUID uuid, String label, String text, String labelAbbrev){
274
		ExtensionType extensionType = (ExtensionType)getTermService().find(uuid);
275
		if (extensionType == null){
276
			extensionType = ExtensionType.NewInstance(text, label, labelAbbrev);
277
			extensionType.setUuid(uuid);
278
			getTermService().save(extensionType);
279
		}
280
		return extensionType;
281
	}
282
	
283
	protected MarkerType getMarkerType(UUID uuid, String label, String text, String labelAbbrev){
284
		MarkerType markerType = (MarkerType)getTermService().find(uuid);
285
		if (markerType == null){
286
			markerType = MarkerType.NewInstance(label, text, labelAbbrev);
287
			markerType.setUuid(uuid);
288
			getTermService().save(markerType);
289
		}
290
		return markerType;
291
	}
292
	
293

    
294
	/**
295
	 * Reads a foreign key field from the result set and adds its value to the idSet.
296
	 * @param rs
297
	 * @param teamIdSet
298
	 * @throws SQLException
299
	 */
300
	protected void handleForeignKey(ResultSet rs, Set<String> idSet, String attributeName)
301
			throws SQLException {
302
		Object idObj = rs.getObject(attributeName);
303
		if (idObj != null){
304
			String id  = String.valueOf(idObj);
305
			idSet.add(id);
306
		}
307
	}
308
	
309
	/**
310
	 * Returns true if i is a multiple of recordsPerTransaction
311
	 * @param i
312
	 * @param recordsPerTransaction
313
	 * @return
314
	 */
315
	protected boolean loopNeedsHandling(int i, int recordsPerLoop) {
316
		startTransaction();
317
		return (i % recordsPerLoop) == 0;
318
	}
319
	
320
	protected void doLogPerLoop(int count, int recordsPerLog, String pluralString){
321
		if ((count % recordsPerLog ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
322
	}
323
	
324

    
325

    
326
	
327
}
(1-1/7)