Project

General

Profile

Download (10.1 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
	@Override
80
    public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
81
		boolean result = true;
82
		Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
83
		Map<Integer, DerivedUnit> typeMap = new HashMap<Integer, DerivedUnit>();
84

    
85
		Map<String, TaxonNameBase> nameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
86
		Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
87

    
88
		BerlinModelImportConfigurator config = state.getConfig();
89
		Source source = config.getSource();
90

    
91
		ResultSet rs = partitioner.getResultSet();
92

    
93
		try {
94

    
95
			int i = 0;
96
			//for each reference
97
			while (rs.next()){
98

    
99
				if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Types handled: " + (i-1));}
100

    
101
				int typeDesignationId = rs.getInt("typeDesignationId");
102
				int nameId = rs.getInt("nameFk");
103
				int typeStatusFk = rs.getInt("typeStatusFk");
104
				Object refFkObj = rs.getObject("refFk");
105
				String refDetail = rs.getString("refDetail");
106
				String status = rs.getString("Status");
107
				String typePhrase = rs.getString("typePhrase");
108
				String notes = rs.getString("notes");
109

    
110
				//TODO
111
				boolean isNotDesignated = false;
112

    
113

    
114
				//TODO
115
				//TypeCache leer
116
				//RejectedFlag false
117
				//PublishFlag xxx
118

    
119
				TaxonNameBase<?,?> taxonNameBase = nameMap.get(String.valueOf(nameId));
120

    
121
				if (taxonNameBase != null){
122
					try{
123
						SpecimenTypeDesignationStatus typeDesignationStatus = BerlinModelTransformer.typeStatusId2TypeStatus(typeStatusFk);
124
						Reference citation = null;
125
						if (refFkObj != null){
126
							String relRefFk = String.valueOf(refFkObj);
127
							//get nomRef
128
							citation = refMap.get(relRefFk);
129
						}
130

    
131
						DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
132
						specimen.putDefinition(Language.DEFAULT(), typePhrase);
133

    
134
						if (typePhrase == null){
135
							String message = "No type phrase available for type with typeDesignationId %s of taxon name %s";
136
							message = String.format(message, typeDesignationId, taxonNameBase.getTitleCache());
137
							logger.warn(message);
138
						}else if (typePhrase.length()> 255){
139
							typePhrase = typePhrase.substring(0, 255);
140
							specimen.setTitleCache(typePhrase, true);
141
						}
142
						boolean addToAllNames = true;
143
						String originalNameString = null;
144
						SpecimenTypeDesignation type = taxonNameBase.addSpecimenTypeDesignation(specimen, typeDesignationStatus, citation, refDetail, originalNameString, isNotDesignated, addToAllNames);
145
						this.doNotes(type, notes);
146

    
147
						typeMap.put(typeDesignationId, specimen);
148
						namesToSave.add(taxonNameBase);
149

    
150
					}catch (UnknownCdmTypeException e) {
151
						logger.warn("TypeStatus '" + status + "' not yet implemented");
152
						result = false;
153
					}catch (Exception e) {
154
						logger.warn("Unexpected exception occurred while processing type with typeDesignationId " + String.valueOf(typeDesignationId));
155
						result = false;
156
					}
157
				}else{
158
					//TODO
159
					logger.warn("TaxonName for TypeDesignation " + typeDesignationId + " does not exist in store");
160
					result = false;
161
				}
162
				//put
163
			}
164

    
165
			result &= makeFigures(typeMap, source);
166
			logger.info("Names to save: " + namesToSave.size());
167
			getNameService().save(namesToSave);
168
			return result;
169
		} catch (SQLException e) {
170
			logger.error("SQLException:" +  e);
171
			return false;
172
		}
173
	}
174

    
175
	@Override
176
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
177
		String nameSpace;
178
		Class<?> cdmClass;
179
		Set<String> idSet;
180
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
181

    
182
		try{
183
			Set<String> nameIdSet = new HashSet<String>();
184
			Set<String> referenceIdSet = new HashSet<String>();
185
			while (rs.next()){
186
				handleForeignKey(rs, nameIdSet, "NameFk");
187
				handleForeignKey(rs, referenceIdSet, "RefFk");
188
	}
189

    
190
			//name map
191
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
192
			cdmClass = TaxonNameBase.class;
193
			idSet = nameIdSet;
194
			Map<String, Person> objectMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
195
			result.put(nameSpace, objectMap);
196

    
197
			//reference map
198
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
199
			cdmClass = Reference.class;
200
			idSet = referenceIdSet;
201
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
202
			result.put(nameSpace, referenceMap);
203

    
204
		} catch (SQLException e) {
205
			throw new RuntimeException(e);
206
		}
207
		return result;
208
	}
209

    
210

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

    
221
			int i = 0;
222
			//for each reference
223
			while (rs.next()){
224

    
225
				if ((i++ % modCount) == 0){ logger.info("TypesFigures handled: " + (i-1));}
226

    
227
				Integer typeFigureId = rs.getInt("typeFigureId");
228
				Integer typeDesignationFk = rs.getInt("typeDesignationFk");
229
				Integer collectionFk = rs.getInt("collectionFk");
230
				String filename = rs.getString("filename");
231

    
232
				String figurePhrase = rs.getString("figurePhrase");
233

    
234
				String mimeType = null; //"image/jpg";
235
				String suffix = null; //"jpg";
236
				java.net.URI uri = new URI(filename);
237
				Media media = ImageFile.NewMediaInstance(null, null, uri, mimeType, suffix, null, null, null);
238
				if (figurePhrase != null) {
239
					media.addAnnotation(Annotation.NewDefaultLanguageInstance(figurePhrase));
240
				}
241
				DerivedUnit typeSpecimen = typeMap.get(typeDesignationFk);
242
				if (typeSpecimen != null) {
243
					SpecimenDescription desc = this.getSpecimenDescription(typeSpecimen, IMAGE_GALLERY, CREATE);
244
					if (desc.getElements().isEmpty()){
245
						desc.addElement(TextData.NewInstance(Feature.IMAGE()));
246
					}
247
					TextData textData = CdmBase.deproxy(desc.getElements().iterator().next(), TextData.class);
248
					textData.addMedia(media);
249
//					typeSpecimen.addMedia(media);  #3597
250
				}
251

    
252
				//mimeType + suffix
253
				//TODO
254
				//RefFk
255
				//RefDetail
256
				//VerifiedBy
257
				//VerifiedWhen
258
				//PrefFigureFlag
259
				//PublishedFlag
260
				//etc.
261
			}
262
		} catch (SQLException e) {
263
			logger.error("SQLException:" +  e);
264
			return false;
265
		} catch (URISyntaxException e) {
266
			logger.error("URISyntaxException:" +  e);
267
			return false;
268
		}
269

    
270
		return success;
271
	}
272

    
273
	/* (non-Javadoc)
274
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
275
	 */
276
	@Override
277
	protected boolean doCheck(BerlinModelImportState state){
278
		IOValidator<BerlinModelImportState> validator = new BerlinModelTypesImportValidator();
279
		return validator.validate(state);
280
	}
281

    
282

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

    
291
}
(18-18/21)