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.api.utility;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collections;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
21
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
22
|
import eu.etaxonomy.cdm.model.description.Feature;
|
23
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
24
|
import eu.etaxonomy.cdm.model.description.TextData;
|
25
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
26
|
import eu.etaxonomy.cdm.model.media.Media;
|
27
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
28
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
|
31
|
/**
|
32
|
* @author n.hoffmann
|
33
|
* @created Jan 27, 2010
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
public class ImagesUtility {
|
37
|
private static final Logger logger = Logger.getLogger(ImagesUtility.class);
|
38
|
|
39
|
/**
|
40
|
* Quick and dirty method to get an element's first image file.
|
41
|
*
|
42
|
* @param element
|
43
|
* @return
|
44
|
* @deprecated not used by EDITor anymore
|
45
|
*/
|
46
|
@Deprecated
|
47
|
public static ImageFile getImage(DescriptionElementBase element) {
|
48
|
List<Media> medias = element.getMedia();
|
49
|
|
50
|
for(Media media : medias){
|
51
|
Set<MediaRepresentation> representations = media.getRepresentations();
|
52
|
|
53
|
for(MediaRepresentation representation : representations){
|
54
|
List<MediaRepresentationPart> parts = representation.getParts();
|
55
|
|
56
|
for (MediaRepresentationPart part : parts){
|
57
|
if(part instanceof ImageFile){
|
58
|
return (ImageFile) part;
|
59
|
}
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
return null;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* @deprecated not used by EDITor anymore
|
68
|
*/
|
69
|
@Deprecated
|
70
|
public static List<ImageFile> getOrderedImages(DescriptionElementBase element){
|
71
|
List<ImageFile> imageList = new ArrayList<ImageFile>();
|
72
|
MediaRepresentation representation = getImageMediaRepresentation(element);
|
73
|
if (representation != null) {
|
74
|
for (MediaRepresentationPart part : representation.getParts()){
|
75
|
if(!(part instanceof ImageFile)){
|
76
|
throw new RuntimeException("Your database contains media that mix Image Files with non-Image Files.");
|
77
|
} else {
|
78
|
imageList.add((ImageFile) part);
|
79
|
}
|
80
|
}
|
81
|
}
|
82
|
return imageList;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Returns the first Representation with images. If none is found, a
|
87
|
* Representation for storing images is created and returned.
|
88
|
*
|
89
|
* @deprecated not used by EDITor anymore
|
90
|
* @param element
|
91
|
* @return
|
92
|
*/
|
93
|
@Deprecated
|
94
|
private static MediaRepresentation getImageMediaRepresentation(DescriptionElementBase element) {
|
95
|
// Drill down until a representation with images is found
|
96
|
for(Media media : element.getMedia()){
|
97
|
Set<MediaRepresentation> representations = media.getRepresentations();
|
98
|
for(MediaRepresentation representation : representations){
|
99
|
List<MediaRepresentationPart> parts = representation.getParts();
|
100
|
for (MediaRepresentationPart part : parts){
|
101
|
if(part instanceof ImageFile){
|
102
|
return representation;
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
// No representation with images found - create
|
108
|
MediaRepresentation representation = MediaRepresentation.NewInstance();
|
109
|
Media media = Media.NewInstance();
|
110
|
element.addMedia(media);
|
111
|
media.addRepresentation(representation);
|
112
|
return representation;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* @deprecated not used by EDITor anymore
|
117
|
* @param description
|
118
|
* @return
|
119
|
*/
|
120
|
@Deprecated
|
121
|
public static Set<ImageFile> getImages(TaxonDescription description){
|
122
|
Set<ImageFile> images = new HashSet<ImageFile>();
|
123
|
|
124
|
for (DescriptionElementBase element : description.getElements()){
|
125
|
|
126
|
Feature feature = element.getFeature();
|
127
|
|
128
|
if(feature.equals(Feature.IMAGE())){
|
129
|
List<Media> medias = element.getMedia();
|
130
|
|
131
|
for(Media media : medias){
|
132
|
Set<MediaRepresentation> representations = media.getRepresentations();
|
133
|
|
134
|
for(MediaRepresentation representation : representations){
|
135
|
List<MediaRepresentationPart> parts = representation.getParts();
|
136
|
|
137
|
for (MediaRepresentationPart part : parts){
|
138
|
if(part instanceof ImageFile){
|
139
|
images.add((ImageFile) part);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
|
147
|
return images;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @deprecated not used by EDITor anymore
|
152
|
* @param taxon
|
153
|
* @param imageFile
|
154
|
*/
|
155
|
@Deprecated
|
156
|
public static void addTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
|
157
|
|
158
|
imageGallery.addElement(createImageElement(imageFile));
|
159
|
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* @deprecated not used by EDITor anymore
|
164
|
* @param imageFile
|
165
|
* @return
|
166
|
*/
|
167
|
@Deprecated
|
168
|
public static DescriptionElementBase createImageElement(ImageFile imageFile) {
|
169
|
|
170
|
DescriptionElementBase descriptionElement = TextData.NewInstance(Feature.IMAGE());
|
171
|
|
172
|
Media media = Media.NewInstance();
|
173
|
MediaRepresentation representation = MediaRepresentation.NewInstance();
|
174
|
|
175
|
representation.addRepresentationPart(imageFile);
|
176
|
|
177
|
media.addRepresentation(representation);
|
178
|
|
179
|
descriptionElement.addMedia(media);
|
180
|
|
181
|
return descriptionElement;
|
182
|
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Adds a new, empty image file to the end of a description element's
|
187
|
* ordered list of images.
|
188
|
*
|
189
|
* @deprecated not used by EDITor anymore
|
190
|
* @param element
|
191
|
* @return
|
192
|
*/
|
193
|
@Deprecated
|
194
|
public static ImageFile addImagePart(DescriptionElementBase element) {
|
195
|
ImageFile imageFile = ImageFile.NewInstance(null, null);
|
196
|
getImageMediaRepresentation(element).addRepresentationPart(imageFile);
|
197
|
return imageFile;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* @deprecated not used by EDITor anymore
|
202
|
* @param taxon
|
203
|
* @param imageFile
|
204
|
*/
|
205
|
@Deprecated
|
206
|
public static void removeTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
|
207
|
Set<DescriptionElementBase> descriptionElementsToRemove = new HashSet<DescriptionElementBase>();
|
208
|
Set<MediaRepresentationPart> representationPartsToRemove = new HashSet<MediaRepresentationPart>();
|
209
|
|
210
|
Set<DescriptionElementBase> images = imageGallery.getElements();
|
211
|
|
212
|
// overmodelling of media in cdmlib makes this a little bit complicated
|
213
|
for(DescriptionElementBase descriptionElement : images){
|
214
|
for(Media media : descriptionElement.getMedia()){
|
215
|
for(MediaRepresentation representation : media.getRepresentations()){
|
216
|
for(MediaRepresentationPart part : representation.getParts()){
|
217
|
if(part.equals(imageFile)){
|
218
|
// because of concurrent modification, we just collect the parts to remove
|
219
|
representationPartsToRemove.add(part);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
// and then remove the representation parts here
|
224
|
for (MediaRepresentationPart part : representationPartsToRemove){
|
225
|
representation.removeRepresentationPart(part);
|
226
|
}
|
227
|
// clear set for next run
|
228
|
representationPartsToRemove.clear();
|
229
|
|
230
|
// description elements with empty representations should be deleted as well
|
231
|
if(representation.getParts().size() == 0){
|
232
|
descriptionElementsToRemove.add(descriptionElement);
|
233
|
}
|
234
|
}
|
235
|
}
|
236
|
}
|
237
|
|
238
|
// remove the empty description elements
|
239
|
for(DescriptionElementBase descriptionElement : descriptionElementsToRemove){
|
240
|
imageGallery.removeElement(descriptionElement);
|
241
|
}
|
242
|
}
|
243
|
|
244
|
public static final int UP = 1;
|
245
|
public static final int DOWN = -1;
|
246
|
|
247
|
/**
|
248
|
* @deprecated not used by EDITor anymore
|
249
|
* @param input
|
250
|
* @param selectedImage
|
251
|
* @param direction
|
252
|
*/
|
253
|
@Deprecated
|
254
|
public static void moveImage(DescriptionElementBase element, ImageFile image,
|
255
|
int direction) {
|
256
|
|
257
|
MediaRepresentation representation = getImageMediaRepresentation(element);
|
258
|
int index = representation.getParts().indexOf(image);
|
259
|
if (index == -1) {
|
260
|
return;
|
261
|
}
|
262
|
if (direction == UP && index != 0) {
|
263
|
Collections.swap(representation.getParts(), index, index - 1);
|
264
|
}
|
265
|
if (direction == DOWN && index < representation.getParts().size() - 1) {
|
266
|
Collections.swap(representation.getParts(), index, index + 1);
|
267
|
}
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* @deprecated not used by EDITor anymore
|
272
|
* @param input
|
273
|
* @param selectedImage
|
274
|
*/
|
275
|
@Deprecated
|
276
|
public static void removeImage(DescriptionElementBase element, ImageFile image) {
|
277
|
getImageMediaRepresentation(element).getParts().remove(image);
|
278
|
}
|
279
|
|
280
|
/**
|
281
|
* Iterate through all taxon's image galleries until the descriptive element containing
|
282
|
* the ImageFile is found.
|
283
|
*
|
284
|
* @deprecated not used by EDITor anymore
|
285
|
* @param image
|
286
|
* @param taxon
|
287
|
* @return
|
288
|
*/
|
289
|
@Deprecated
|
290
|
public static DescriptionElementBase findImageElement(ImageFile image, Taxon taxon) {
|
291
|
if (taxon == null) {
|
292
|
return null;
|
293
|
}
|
294
|
for (TaxonDescription description : taxon.getDescriptions()) {
|
295
|
if (description.isImageGallery()) {
|
296
|
for (DescriptionElementBase element : description.getElements()) {
|
297
|
if (getOrderedImages(element).contains(image)) {
|
298
|
return element;
|
299
|
}
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
return null;
|
304
|
}
|
305
|
|
306
|
/**
|
307
|
*
|
308
|
* @param description
|
309
|
* @param media
|
310
|
*/
|
311
|
public static void addMediaToGallery(DescriptionBase description, Media media){
|
312
|
DescriptionElementBase element = getGalleryElement(description);
|
313
|
element.addMedia(media);
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
*
|
318
|
* @param description
|
319
|
* @param media
|
320
|
*/
|
321
|
public static void removeMediaFromGallery(DescriptionBase description, Media media){
|
322
|
DescriptionElementBase element = getGalleryElement(description);
|
323
|
element.removeMedia(media);
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
*
|
328
|
*
|
329
|
* @param description
|
330
|
* @return
|
331
|
*/
|
332
|
private static DescriptionElementBase getGalleryElement(DescriptionBase description){
|
333
|
if(! description.isImageGallery()){
|
334
|
logger.error("Description has to have imageGallery flag set.");
|
335
|
}
|
336
|
|
337
|
Set<DescriptionElementBase> elements = description.getElements();
|
338
|
|
339
|
if(elements.size() != 1){
|
340
|
logger.error("Image gallery should have only one description");
|
341
|
}
|
342
|
|
343
|
return elements.iterator().next();
|
344
|
}
|
345
|
}
|