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.SQLException;
14
import java.sql.Timestamp;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

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

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.io.common.CdmImportBase;
26
import eu.etaxonomy.cdm.io.common.ICdmIO;
27
import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
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.mapping.DbImportMapping;
33
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
34
import eu.etaxonomy.cdm.model.common.Annotation;
35
import eu.etaxonomy.cdm.model.common.AnnotationType;
36
import eu.etaxonomy.cdm.model.common.CdmBase;
37
import eu.etaxonomy.cdm.model.common.ExtensionType;
38
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
39
import eu.etaxonomy.cdm.model.common.Language;
40
import eu.etaxonomy.cdm.model.common.MarkerType;
41
import eu.etaxonomy.cdm.model.common.User;
42

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

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

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

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

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

    
122

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

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

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

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

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

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

    
324

    
325
	
326
}
(1-1/7)