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.algaterra;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase;
|
18
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
19
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
20
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
21
|
import eu.etaxonomy.cdm.model.common.Language;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
24
|
import eu.etaxonomy.cdm.model.description.Feature;
|
25
|
import eu.etaxonomy.cdm.model.description.TextData;
|
26
|
import eu.etaxonomy.cdm.model.media.Media;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @created 12.09.2012
|
34
|
*/
|
35
|
public abstract class AlgaTerraImageImportBase extends BerlinModelImportBase{
|
36
|
private static final Logger logger = Logger.getLogger(AlgaTerraImageImportBase.class);
|
37
|
|
38
|
public static final String TERMS_NAMESPACE = "ALGA_TERRA_TERMS";
|
39
|
|
40
|
private static final String ALGAE_URL_BASE = "http://mediastorage.bgbm.org/fsi/server?type=image&profile=jpeg&quality=100&source=Algaterra%2FAlgae%2F";
|
41
|
private static final String SITE_URL_BASE = "http://mediastorage.bgbm.org/fsi/server?type=image&profile=jpeg&quality=100&source=Algaterra%2FSites%2F";
|
42
|
private static final String VOUCHER_URL_BASE = "http://mediastorage.bgbm.org/fsi/server?type=image&profile=jpeg&quality=100&source=Algaterra%2FVoucher%2F";
|
43
|
|
44
|
private static final String ALGAE_URL_ORIGINAL = "http://media.bgbm.org/erez/erez?cmd=get&src=Algaterra%2FAlgae%2F";
|
45
|
private static final String SITE_URL_ORIGINAL = "http://media.bgbm.org/erez/erez?cmd=get&src=Algaterra%2FSites%2F";
|
46
|
private static final String VOUCHER_URL_ORIGINAL = "http://media.bgbm.org/erez/erez?cmd=get&src=Algaterra%2FVoucher%2F";
|
47
|
|
48
|
|
49
|
protected enum PathType {
|
50
|
Image (ALGAE_URL_BASE, ALGAE_URL_ORIGINAL),
|
51
|
Site (SITE_URL_BASE, SITE_URL_ORIGINAL),
|
52
|
Voucher (VOUCHER_URL_BASE, VOUCHER_URL_ORIGINAL)
|
53
|
;
|
54
|
|
55
|
String urlThumbnail;
|
56
|
String urlOriginal;
|
57
|
private PathType(String urlThumbnail, String urlOriginal){
|
58
|
this.urlThumbnail = urlThumbnail;
|
59
|
this.urlOriginal = urlOriginal;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
public AlgaTerraImageImportBase(String tableName, String pluralString) {
|
64
|
super(tableName, pluralString);
|
65
|
}
|
66
|
|
67
|
|
68
|
/**
|
69
|
* Creates a media object and
|
70
|
* @param rs
|
71
|
* @param derivedUnit
|
72
|
* @param state
|
73
|
* @param partitioner
|
74
|
* @return
|
75
|
* @throws SQLException
|
76
|
*/
|
77
|
protected Media handleSingleImage(ResultSet rs, IdentifiableEntity<?> identifiableEntity, AlgaTerraImportState state, ResultSetPartitioner partitioner, PathType pathType) throws SQLException {
|
78
|
|
79
|
try {
|
80
|
String figurePhrase = rs.getString("FigurePhrase");
|
81
|
// String filePath = rs.getString("filePath");
|
82
|
String fileName = rs.getString("fileName");
|
83
|
//TODO publishFlag
|
84
|
Boolean publishFlag = rs.getBoolean("RestrictedFlag");
|
85
|
|
86
|
|
87
|
if (isBlank(fileName)){
|
88
|
throw new RuntimeException("FileName is missing");
|
89
|
}
|
90
|
// if (isBlank(filePath)){
|
91
|
// filePath = state.getAlgaTerraConfigurator().getImageBaseUrl();
|
92
|
// }
|
93
|
|
94
|
|
95
|
//handle thumbnail
|
96
|
String filePath = pathType.urlThumbnail;
|
97
|
String fullPath = filePath + fileName;
|
98
|
Media media = getImageMedia(fullPath, READ_MEDIA_DATA);
|
99
|
if (media == null){
|
100
|
logger.warn("Thumbnail image not found: " + filePath);
|
101
|
}
|
102
|
|
103
|
//handle original
|
104
|
if (state.getAlgaTerraConfigurator().isImportOriginalSizeMedia()){
|
105
|
filePath = pathType.urlOriginal;
|
106
|
fullPath = filePath + fileName;
|
107
|
Media mediaOriginal = getImageMedia(fullPath, READ_MEDIA_DATA);
|
108
|
if (mediaOriginal != null){
|
109
|
if (media == null){
|
110
|
media = mediaOriginal;
|
111
|
}else {
|
112
|
media.addRepresentation(mediaOriginal.getRepresentations().iterator().next());
|
113
|
}
|
114
|
}else{
|
115
|
logger.warn("Original image not found: " + filePath);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
if (media == null){
|
120
|
throw new RuntimeException ("Media not found for " +fullPath);
|
121
|
}
|
122
|
|
123
|
|
124
|
|
125
|
if (isNotBlank(figurePhrase)){
|
126
|
media.putTitle(Language.DEFAULT(), figurePhrase);
|
127
|
}
|
128
|
|
129
|
//TODO ref
|
130
|
Reference<?> ref = null;
|
131
|
if (identifiableEntity != null){
|
132
|
DescriptionBase<?> desc = getDescription(identifiableEntity, ref, IMAGE_GALLERY, CREATE);
|
133
|
TextData textData = null;
|
134
|
for (DescriptionElementBase descEl : desc.getElements()){
|
135
|
if (descEl.isInstanceOf(TextData.class)){
|
136
|
textData = CdmBase.deproxy(descEl, TextData.class);
|
137
|
}
|
138
|
}
|
139
|
if (textData == null){
|
140
|
textData = TextData.NewInstance(Feature.IMAGE());
|
141
|
}
|
142
|
desc.addElement(textData);
|
143
|
textData.addMedia(media);
|
144
|
}else{
|
145
|
logger.warn("Identifiable Entity is null. Can't add media ");
|
146
|
}
|
147
|
|
148
|
//notes
|
149
|
|
150
|
//TODO restrictedFlag
|
151
|
|
152
|
//TODO id, created for
|
153
|
// this.doIdCreatedUpdatedNotes(state, descriptionElement, rs, id, namespace);
|
154
|
|
155
|
return media;
|
156
|
} catch (Exception e) {
|
157
|
throw new RuntimeException(e);
|
158
|
}
|
159
|
|
160
|
}
|
161
|
|
162
|
|
163
|
|
164
|
|
165
|
private DescriptionBase<?> getDescription(IdentifiableEntity identifiableEntity, Reference<?> ref,
|
166
|
boolean imageGallery, boolean create) {
|
167
|
if (identifiableEntity.isInstanceOf(SpecimenOrObservationBase.class)){
|
168
|
return getSpecimenDescription(CdmBase.deproxy(identifiableEntity, SpecimenOrObservationBase.class), ref, imageGallery, create);
|
169
|
}else if (identifiableEntity.isInstanceOf(Taxon.class)){
|
170
|
return getTaxonDescription(CdmBase.deproxy(identifiableEntity, Taxon.class), ref, imageGallery, create);
|
171
|
}else{
|
172
|
logger.warn("Unsupported IdentifiableEntity type: " + identifiableEntity.getClass());
|
173
|
return null;
|
174
|
}
|
175
|
}
|
176
|
}
|