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.app.greece;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
22
|
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
|
23
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
24
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
25
|
import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
28
|
import eu.etaxonomy.cdm.model.media.Media;
|
29
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
30
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @since 05.2017
|
35
|
*/
|
36
|
public class GreeceLargeImagesAdderActivator {
|
37
|
@SuppressWarnings("unused")
|
38
|
private static final Logger logger = Logger.getLogger(GreeceLargeImagesAdderActivator.class);
|
39
|
|
40
|
|
41
|
static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
|
42
|
// static final ICdmDataSource cdmDestination = CdmDestinations.cdm_production_greece_checklist();
|
43
|
|
44
|
static boolean testOnly = false;
|
45
|
|
46
|
|
47
|
private void addLargeImage(ICdmDataSource cdmDestination){
|
48
|
CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
|
49
|
TransactionStatus tx = app.startTransaction();
|
50
|
|
51
|
List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
|
52
|
for (Media media : list){
|
53
|
if (! isFirstImportMedia(media)){
|
54
|
continue;
|
55
|
}
|
56
|
Set<MediaRepresentation> reps = media.getRepresentations();
|
57
|
if (reps.size() != 2){
|
58
|
System.out.println("Media has not exactly 2 representations: " + media.getId() + "; " + media.getTitleCache());
|
59
|
continue;
|
60
|
}else{
|
61
|
MediaRepresentation first = reps.iterator().next();
|
62
|
if (first.getParts().size() != 1){
|
63
|
System.out.println("Media has representation with not exactly 1 parts: " + media.getId() + "; " + media.getTitleCache());
|
64
|
continue;
|
65
|
}
|
66
|
MediaRepresentationPart part = first.getParts().iterator().next();
|
67
|
String uri = part.getUri().toString();
|
68
|
if (uri.startsWith("https://media.e-taxonomy.eu/flora-greece/medium/Plate")){
|
69
|
uri = uri.replace("flora-greece/medium/Plate", "flora-greece/large/Plate");
|
70
|
}else if(uri.startsWith("https://media.e-taxonomy.eu/flora-greece/thumbs/Plate")) {
|
71
|
uri = uri.replace("flora-greece/thumbs/Plate", "flora-greece/large/Plate");
|
72
|
}else{
|
73
|
System.out.println("URI has unexpected format: " + uri);
|
74
|
continue;
|
75
|
}
|
76
|
handleUri(media, uri);
|
77
|
}
|
78
|
}
|
79
|
if (testOnly){
|
80
|
tx.setRollbackOnly();
|
81
|
}
|
82
|
app.commitTransaction(tx);
|
83
|
}
|
84
|
|
85
|
private void updateImageSizes(ICdmDataSource cdmDestination){
|
86
|
CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
|
87
|
TransactionStatus tx = app.startTransaction();
|
88
|
|
89
|
List<Media> list = app.getMediaService().list(Media.class, null, null, null, null);
|
90
|
for (Media media : list){
|
91
|
if (! isFirstImportMedia(media)){
|
92
|
continue;
|
93
|
}
|
94
|
Set<MediaRepresentation> reps = media.getRepresentations();
|
95
|
for (MediaRepresentation rep : reps){
|
96
|
for (MediaRepresentationPart part : rep.getParts()){
|
97
|
if (part.isInstanceOf(ImageFile.class)){
|
98
|
handlePart(CdmBase.deproxy(part, ImageFile.class));
|
99
|
}else{
|
100
|
System.out.println("Representation part is not of type ImageFile: "+ part.getId());
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
|
106
|
if (testOnly){
|
107
|
tx.setRollbackOnly();
|
108
|
}
|
109
|
app.commitTransaction(tx);
|
110
|
|
111
|
}
|
112
|
|
113
|
|
114
|
|
115
|
/**
|
116
|
* @param media
|
117
|
* @return
|
118
|
*/
|
119
|
private boolean isFirstImportMedia(Media media) {
|
120
|
Boolean result = null;
|
121
|
Set<String> urls = getUrlStringForMedia(media);
|
122
|
for (String url : urls){
|
123
|
if (url.startsWith("http://150.140.202.8/files/Goula/") || url.startsWith("http://n4412.gr/images/Globula")){
|
124
|
if (result == Boolean.TRUE){
|
125
|
System.out.println("Ambigous: " + media.getId());
|
126
|
return false;
|
127
|
}
|
128
|
result = false;
|
129
|
}else if (url.startsWith("https://media.e-taxonomy.eu/flora-greece")){
|
130
|
if (result == Boolean.FALSE){
|
131
|
System.out.println("Ambigous: " + media.getId());
|
132
|
return false;
|
133
|
}
|
134
|
result = true;
|
135
|
}
|
136
|
}
|
137
|
if (result == null){
|
138
|
System.out.println("No data: " + media.getId());
|
139
|
return false;
|
140
|
}
|
141
|
return result;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* @param media
|
146
|
* @return
|
147
|
*/
|
148
|
private Set<String> getUrlStringForMedia(Media media) {
|
149
|
Set<String> result = new HashSet<>();
|
150
|
for (MediaRepresentation rep : media.getRepresentations()){
|
151
|
for (MediaRepresentationPart part : rep.getParts()){
|
152
|
URI uri = part.getUri();
|
153
|
if (uri != null){
|
154
|
result.add(uri.toString());
|
155
|
}else{
|
156
|
System.out.println("URI is null:" + media.getId());
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
return result;
|
161
|
}
|
162
|
|
163
|
private void handlePart(ImageFile part) {
|
164
|
CdmImageInfo imageInfo = null;
|
165
|
URI uri = part.getUri();
|
166
|
try {
|
167
|
imageInfo = CdmImageInfo.NewInstance(uri, 0);
|
168
|
} catch (Exception e) {
|
169
|
String message = "An error occurred when trying to read image meta data for %s.";
|
170
|
message = String.format(message, uri.toString());
|
171
|
System.out.println(message);
|
172
|
return;
|
173
|
}
|
174
|
part.setHeight(imageInfo.getHeight());
|
175
|
part.setWidth(imageInfo.getWidth());
|
176
|
|
177
|
MediaRepresentation representation = part.getMediaRepresentation();
|
178
|
representation.setMimeType(imageInfo.getMimeType());
|
179
|
representation.setSuffix(imageInfo.getSuffix());
|
180
|
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* @param reps
|
185
|
* @param uri
|
186
|
*/
|
187
|
private void handleUri(Media media, String uriStr) {
|
188
|
URI uri = URI.create(uriStr);
|
189
|
CdmImageInfo imageInfo = null;
|
190
|
try {
|
191
|
imageInfo = CdmImageInfo.NewInstance(uri, 0);
|
192
|
} catch (Exception e) {
|
193
|
String message = "An error occurred when trying to read image meta data for %s.";
|
194
|
message = String.format(message, uri.toString());
|
195
|
System.out.println(message);
|
196
|
}
|
197
|
ImageFile imageFile = ImageFile.NewInstance(uri, null, imageInfo);
|
198
|
|
199
|
MediaRepresentation representation = MediaRepresentation.NewInstance();
|
200
|
|
201
|
if(imageInfo != null){
|
202
|
representation.setMimeType(imageInfo.getMimeType());
|
203
|
representation.setSuffix(imageInfo.getSuffix());
|
204
|
}
|
205
|
representation.addRepresentationPart(imageFile);
|
206
|
media.addRepresentation(representation);
|
207
|
|
208
|
}
|
209
|
|
210
|
|
211
|
|
212
|
|
213
|
/**
|
214
|
* @param args
|
215
|
*/
|
216
|
public static void main(String[] args) {
|
217
|
GreeceLargeImagesAdderActivator me = new GreeceLargeImagesAdderActivator();
|
218
|
// me.addLargeImage(cdmDestination);
|
219
|
me.updateImageSizes(cdmDestination);
|
220
|
// me.test();
|
221
|
System.exit(0);
|
222
|
}
|
223
|
|
224
|
}
|