Project

General

Profile

Download (9.4 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.common.Annotation;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
34
import eu.etaxonomy.cdm.model.description.TextData;
35
import eu.etaxonomy.cdm.model.media.ImageFile;
36
import eu.etaxonomy.cdm.model.media.Media;
37
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
38
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
43

    
44
/**
45
 * @author a.mueller
46
 * @since 20.03.2008
47
 */
48
@Component
49
public class BerlinModelTypesImport extends BerlinModelImportBase /*implements IIO<BerlinModelImportConfigurator>*/ {
50

    
51
    private static final long serialVersionUID = -8468807718014749046L;
52
    private static final Logger logger = Logger.getLogger(BerlinModelTypesImport.class);
53

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

    
58

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

    
63
	@Override
64
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
65
		String strRecordQuery =
66
			" SELECT TypeDesignation.*, TypeStatus.Status " +
67
			" FROM TypeDesignation LEFT OUTER JOIN " +
68
			" TypeStatus ON TypeDesignation.TypeStatusFk = TypeStatus.TypeStatusId " +
69
			" WHERE (TypeDesignationId IN ("+ ID_LIST_TOKEN + ") )";
70
		return strRecordQuery;
71
	}
72

    
73
	@Override
74
    public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, BerlinModelImportState state) {
75

    
76
	    boolean result = true;
77
		Set<TaxonName> namesToSave = new HashSet<>();
78
		Map<Integer, DerivedUnit> typeMap = new HashMap<>();
79

    
80
		@SuppressWarnings("unchecked")
81
        Map<String, TaxonName> nameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
82
		@SuppressWarnings("unchecked")
83
        Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
84

    
85
		BerlinModelImportConfigurator config = state.getConfig();
86
		Source source = config.getSource();
87

    
88
		ResultSet rs = partitioner.getResultSet();
89

    
90
		try {
91

    
92
			int i = 0;
93
			//for each reference
94
			while (rs.next()){
95

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

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

    
107
				//TODO
108
				boolean isNotDesignated = false;
109

    
110

    
111
				//TODO
112
				//TypeCache leer
113
				//RejectedFlag false
114
				//PublishFlag xxx
115

    
116
				TaxonName taxonNameBase = nameMap.get(String.valueOf(nameId));
117

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

    
128
						DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
129
						specimen.putDefinition(Language.DEFAULT(), typePhrase);
130

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

    
144
						typeMap.put(typeDesignationId, specimen);
145
						namesToSave.add(taxonNameBase);
146

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

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

    
172
	@Override
173
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
174

    
175
	    String nameSpace;
176
		Set<String> idSet;
177
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
178

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

    
187
			//name map
188
			nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
189
			idSet = nameIdSet;
190
			Map<String, TaxonName> objectMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonName.class, idSet, nameSpace);
191
			result.put(nameSpace, objectMap);
192

    
193
			//reference map
194
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
195
			idSet = referenceIdSet;
196
			Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
197
			result.put(nameSpace, referenceMap);
198

    
199
		} catch (SQLException e) {
200
			throw new RuntimeException(e);
201
		}
202
		return result;
203
	}
204

    
205

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

    
216
			int i = 0;
217
			//for each reference
218
			while (rs.next()){
219

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

    
222
				Integer typeFigureId = rs.getInt("typeFigureId");
223
				Integer typeDesignationFk = rs.getInt("typeDesignationFk");
224
				Integer collectionFk = rs.getInt("collectionFk");
225
				String filename = rs.getString("filename");
226

    
227
				String figurePhrase = rs.getString("figurePhrase");
228

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

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

    
265
		return success;
266
	}
267

    
268
	@Override
269
	protected boolean doCheck(BerlinModelImportState state){
270
		IOValidator<BerlinModelImportState> validator = new BerlinModelTypesImportValidator();
271
		return validator.validate(state);
272
	}
273

    
274
	@Override
275
    protected boolean isIgnore(BerlinModelImportState state){
276
		return ! state.getConfig().isDoTypes();
277
	}
278
}
(19-19/22)