Project

General

Profile

Download (10.2 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.berlinModel.in;
11

    
12
import java.net.URI;
13
import java.net.URISyntaxException;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Map;
19
import java.util.Set;
20

    
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
25
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelTypesImportValidator;
26
import eu.etaxonomy.cdm.io.common.IOValidator;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.io.common.Source;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.common.Annotation;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
35
import eu.etaxonomy.cdm.model.description.TextData;
36
import eu.etaxonomy.cdm.model.media.ImageFile;
37
import eu.etaxonomy.cdm.model.media.Media;
38
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
39
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
41
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
44

    
45
/**
46
 * @author a.mueller
47
 * @created 20.03.2008
48
 */
49
@Component
50
public class BerlinModelTypesImport extends BerlinModelImportBase /*implements IIO<BerlinModelImportConfigurator>*/ {
51
	private static final Logger logger = Logger.getLogger(BerlinModelTypesImport.class);
52

    
53
	private static int modCount = 10000;
54
	private static final String pluralString = "types";
55
	private static final String dbTableName = "TypeDesignation";
56

    
57
	
58
	public BerlinModelTypesImport(){
59
		super(dbTableName, pluralString);
60
	}
61

    
62

    
63
	/* (non-Javadoc)
64
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
65
	 */
66
	@Override
67
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
68
		String strRecordQuery = 
69
			" SELECT TypeDesignation.*, TypeStatus.Status " + 
70
			" FROM TypeDesignation LEFT OUTER JOIN " +
71
			" TypeStatus ON TypeDesignation.TypeStatusFk = TypeStatus.TypeStatusId " + 
72
			" WHERE (TypeDesignationId IN ("+ ID_LIST_TOKEN + ") )";
73
		return strRecordQuery;
74
	}
75

    
76
	/* (non-Javadoc)
77
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
78
	 */
79
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
80
		boolean result = true;
81
		Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
82
		Map<Integer, DerivedUnit> typeMap = new HashMap<Integer, DerivedUnit>();
83
		
84
		Map<String, TaxonNameBase> nameMap = (Map<String, TaxonNameBase>) partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
85
		Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
86
		
87
		BerlinModelImportConfigurator config = state.getConfig();
88
		Source source = config.getSource();
89
		
90
		ResultSet rs = partitioner.getResultSet();
91

    
92
		try {
93

    
94
			int i = 0;
95
			//for each reference
96
			while (rs.next()){
97
				
98
				if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Types handled: " + (i-1));}
99
				
100
				int typeDesignationId = rs.getInt("typeDesignationId");
101
				int nameId = rs.getInt("nameFk");
102
				int typeStatusFk = rs.getInt("typeStatusFk");
103
				Object refFkObj = rs.getObject("refFk");
104
				String refDetail = rs.getString("refDetail");
105
				String status = rs.getString("Status");
106
				String typePhrase = rs.getString("typePhrase");
107
				String notes = rs.getString("notes");
108
				
109
				//TODO 
110
				boolean isNotDesignated = false;
111
				
112
				
113
				//TODO
114
				//TypeCache leer
115
				//RejectedFlag false
116
				//PublishFlag xxx
117
				
118
				TaxonNameBase<?,?> taxonNameBase = nameMap.get(String.valueOf(nameId));
119
				
120
				if (taxonNameBase != null){
121
					try{
122
						SpecimenTypeDesignationStatus typeDesignationStatus = BerlinModelTransformer.typeStatusId2TypeStatus(typeStatusFk);
123
						Reference<?> citation = null;
124
						if (refFkObj != null){
125
							String relRefFk = String.valueOf(refFkObj);
126
							//get nomRef
127
							citation = refMap.get(relRefFk);
128
						}
129
						
130
						DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
131
						specimen.putDefinition(Language.DEFAULT(), typePhrase);
132
						
133
						if (typePhrase == null){
134
							String message = "No type phrase available for type with typeDesignationId %s of taxon name %s";
135
							message = String.format(message, typeDesignationId, taxonNameBase.getTitleCache());
136
							logger.warn(message);
137
						}else if (typePhrase.length()> 255){
138
							typePhrase = typePhrase.substring(0, 255);
139
							specimen.setTitleCache(typePhrase, true);
140
						}
141
						boolean addToAllNames = true;
142
						String originalNameString = null;
143
						SpecimenTypeDesignation type = taxonNameBase.addSpecimenTypeDesignation(specimen, typeDesignationStatus, citation, refDetail, originalNameString, isNotDesignated, addToAllNames);
144
						this.doNotes(type, notes);
145
						
146
						typeMap.put(typeDesignationId, specimen);
147
						namesToSave.add(taxonNameBase);
148

    
149
					}catch (UnknownCdmTypeException e) {
150
						logger.warn("TypeStatus '" + status + "' not yet implemented");
151
						result = false;
152
					}catch (Exception e) {
153
						logger.warn("Unexpected exception occurred while processing type with typeDesignationId " + String.valueOf(typeDesignationId));
154
						result = false;
155
					}
156
				}else{
157
					//TODO
158
					logger.warn("TaxonName for TypeDesignation " + typeDesignationId + " does not exist in store");
159
					result = false;
160
				}
161
				//put
162
			}
163
			
164
			result &= makeFigures(typeMap, source);
165
			logger.info("Names to save: " + namesToSave.size());
166
			getNameService().save(namesToSave);	
167
			return result;
168
		} catch (SQLException e) {
169
			logger.error("SQLException:" +  e);
170
			return false;
171
		}
172
	}
173

    
174
	@Override
175
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
176
		String nameSpace;
177
		Class<?> cdmClass;
178
		Set<String> idSet;
179
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
180
		
181
		try{
182
			Set<String> nameIdSet = new HashSet<String>();
183
			Set<String> referenceIdSet = new HashSet<String>();
184
			while (rs.next()){
185
				handleForeignKey(rs, nameIdSet, "NameFk");
186
				handleForeignKey(rs, referenceIdSet, "RefFk");
187
	}
188
	
189
			//name map
190
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
191
			cdmClass = TaxonNameBase.class;
192
			idSet = nameIdSet;
193
			Map<String, Person> objectMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
194
			result.put(nameSpace, objectMap);
195

    
196
			//reference map
197
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
198
			cdmClass = Reference.class;
199
			idSet = referenceIdSet;
200
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
201
			result.put(nameSpace, referenceMap);
202
			
203
		} catch (SQLException e) {
204
			throw new RuntimeException(e);
205
		}
206
		return result;
207
	}
208

    
209
	
210
	private boolean makeFigures(Map<Integer, DerivedUnit> typeMap, Source source){
211
		boolean success = true;
212
		try {
213
			//get data from database
214
			String strQuery = 
215
					" SELECT * " +
216
					" FROM TypeFigure " + 
217
                    " WHERE (1=1) ";
218
			ResultSet rs = source.getResultSet(strQuery) ;
219

    
220
			int i = 0;
221
			//for each reference
222
			while (rs.next()){
223
				
224
				if ((i++ % modCount) == 0){ logger.info("TypesFigures handled: " + (i-1));}
225
				
226
				Integer typeFigureId = rs.getInt("typeFigureId");
227
				Integer typeDesignationFk = rs.getInt("typeDesignationFk");
228
				Integer collectionFk = rs.getInt("collectionFk");
229
				String filename = rs.getString("filename");
230
				
231
				String figurePhrase = rs.getString("figurePhrase");
232
				
233
				String mimeType = null; //"image/jpg";
234
				String suffix = null; //"jpg";
235
				java.net.URI uri = new URI(filename);
236
				Media media = ImageFile.NewMediaInstance(null, null, uri, mimeType, suffix, null, null, null);
237
				if (figurePhrase != null) {
238
					media.addAnnotation(Annotation.NewDefaultLanguageInstance(figurePhrase));
239
				}
240
				DerivedUnit typeSpecimen = typeMap.get(typeDesignationFk);
241
				if (typeSpecimen != null) {
242
					SpecimenDescription desc = this.getSpecimenDescription(typeSpecimen, IMAGE_GALLERY, CREATE);
243
					if (desc.getElements().isEmpty()){
244
						desc.addElement(TextData.NewInstance(Feature.IMAGE()));
245
					}
246
					TextData textData = (TextData)CdmBase.deproxy(desc.getElements().iterator().next(), TextData.class);
247
					textData.addMedia(media);
248
//					typeSpecimen.addMedia(media);  #3597
249
				}
250
				
251
				//mimeType + suffix
252
				//TODO
253
				//RefFk
254
				//RefDetail
255
				//VerifiedBy
256
				//VerifiedWhen
257
				//PrefFigureFlag
258
				//PublishedFlag
259
				//etc.
260
			}
261
		} catch (SQLException e) {
262
			logger.error("SQLException:" +  e);
263
			return false;
264
		} catch (URISyntaxException e) {
265
			logger.error("URISyntaxException:" +  e);
266
			return false;
267
		}
268
			
269
		return success;
270
	}
271
	
272
	/* (non-Javadoc)
273
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
274
	 */
275
	@Override
276
	protected boolean doCheck(BerlinModelImportState state){
277
		IOValidator<BerlinModelImportState> validator = new BerlinModelTypesImportValidator();
278
		return validator.validate(state);
279
	}
280

    
281
	
282
	/* (non-Javadoc)
283
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
284
	 */
285
	protected boolean isIgnore(BerlinModelImportState state){
286
		return ! state.getConfig().isDoTypes();
287
	}
288

    
289
}
(18-18/21)