Project

General

Profile

Download (9.8 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.api.utility;
12

    
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.model.description.DescriptionBase;
22
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23
import eu.etaxonomy.cdm.model.description.Feature;
24
import eu.etaxonomy.cdm.model.description.TaxonDescription;
25
import eu.etaxonomy.cdm.model.description.TextData;
26
import eu.etaxonomy.cdm.model.media.ImageFile;
27
import eu.etaxonomy.cdm.model.media.Media;
28
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
29
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
30
import eu.etaxonomy.cdm.model.media.ReferencedMediaBase;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @created Jan 27, 2010
36
 * @version 1.0
37
 */
38
public class ImagesUtility {
39
	private static final Logger logger = Logger.getLogger(ImagesUtility.class);
40
	
41
	/**
42
	 * Quick and dirty method to get an element's first image file.
43
	 * 
44
	 * @param element
45
	 * @return
46
	 * @deprecated not used by EDITor anymore
47
	 */
48
	@Deprecated
49
	public static ImageFile getImage(DescriptionElementBase element) {
50
		List<Media> medias = element.getMedia();
51
		
52
		for(Media media : medias){
53
			Set<MediaRepresentation> representations = media.getRepresentations();
54
			
55
			for(MediaRepresentation representation : representations){
56
				List<MediaRepresentationPart> parts = representation.getParts();
57
				
58
				for (MediaRepresentationPart part : parts){
59
					if(part instanceof ImageFile){
60
						return (ImageFile) part;
61
					}
62
				}
63
			}							
64
		}
65
		return null;
66
	}
67
	
68
	/**
69
	 * @deprecated not used by EDITor anymore
70
	 */
71
	@Deprecated
72
	public static List<ImageFile> getOrderedImages(DescriptionElementBase element){
73
		List<ImageFile> imageList = new ArrayList<ImageFile>();
74
		MediaRepresentation representation = getImageMediaRepresentation(element);
75
		if (representation != null) {
76
			for (MediaRepresentationPart part : representation.getParts()){
77
				if(!(part instanceof ImageFile)){
78
					throw new RuntimeException("Your database contains media that mix Image Files with non-Image Files.");
79
				} else {
80
					imageList.add((ImageFile) part);
81
				}
82
			}
83
		}
84
		return imageList;
85
	}
86
		
87
	/**
88
	 * Returns the first Representation with images. If none is found, a
89
	 * Representation for storing images is created and returned.
90
	 * 
91
	 * @deprecated not used by EDITor anymore
92
	 * @param element
93
	 * @return
94
	 */
95
	@Deprecated
96
	private static MediaRepresentation getImageMediaRepresentation(DescriptionElementBase element) {
97
		// Drill down until a representation with images is found
98
		for(Media media : element.getMedia()){
99
			Set<MediaRepresentation> representations = media.getRepresentations();
100
			for(MediaRepresentation representation : representations){
101
				List<MediaRepresentationPart> parts = representation.getParts();
102
				for (MediaRepresentationPart part : parts){
103
					if(part instanceof ImageFile){
104
						return representation;
105
					}
106
				}
107
			}							
108
		}		
109
		// No representation with images found - create
110
		MediaRepresentation representation = MediaRepresentation.NewInstance();
111
		Media media = ReferencedMediaBase.NewInstance();
112
		element.addMedia(media);
113
		media.addRepresentation(representation);
114
		return representation;
115
	}
116
	
117
	/**
118
	 * @deprecated not used by EDITor anymore
119
	 * @param description
120
	 * @return
121
	 */
122
	@Deprecated
123
	public static Set<ImageFile> getImages(TaxonDescription description){
124
		Set<ImageFile> images = new HashSet<ImageFile>();
125
		
126
		for (DescriptionElementBase element : description.getElements()){
127
			
128
			Feature feature = element.getFeature();
129
			
130
			if(feature.equals(Feature.IMAGE())){
131
				List<Media> medias = element.getMedia();
132
				
133
				for(Media media : medias){
134
					Set<MediaRepresentation> representations = media.getRepresentations();
135
					
136
					for(MediaRepresentation representation : representations){
137
						List<MediaRepresentationPart> parts = representation.getParts();
138
						
139
						for (MediaRepresentationPart part : parts){
140
							if(part instanceof ImageFile){
141
								images.add((ImageFile) part);
142
							}
143
						}
144
					}							
145
				}										
146
			}
147
		}
148
		
149
		return images;
150
	}
151

    
152
	/**
153
	 * @deprecated not used by EDITor anymore
154
	 * @param taxon
155
	 * @param imageFile
156
	 */
157
	@Deprecated
158
	public static void addTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
159
				
160
		imageGallery.addElement(createImageElement(imageFile));
161
		
162
	}
163
	
164
	/**
165
	 * @deprecated not used by EDITor anymore
166
	 * @param imageFile
167
	 * @return
168
	 */
169
	@Deprecated
170
	public static DescriptionElementBase createImageElement(ImageFile imageFile) {
171
		
172
		DescriptionElementBase descriptionElement = TextData.NewInstance(Feature.IMAGE());
173
		
174
		Media media = Media.NewInstance();
175
		MediaRepresentation representation = MediaRepresentation.NewInstance();
176
		
177
		representation.addRepresentationPart(imageFile);
178
		
179
		media.addRepresentation(representation);
180
		
181
		descriptionElement.addMedia(media);
182
		
183
		return descriptionElement;
184
		
185
	}
186
	
187
	/**
188
	 * Adds a new, empty image file to the end of a description element's
189
	 * ordered list of images.
190
	 * 
191
	 * @deprecated not used by EDITor anymore
192
	 * @param element
193
	 * @return 
194
	 */
195
	@Deprecated
196
	public static ImageFile addImagePart(DescriptionElementBase element) {
197
		ImageFile imageFile = ImageFile.NewInstance(null, null);
198
		getImageMediaRepresentation(element).addRepresentationPart(imageFile);
199
		return imageFile;
200
	}
201
	
202
	/**
203
	 * @deprecated not used by EDITor anymore
204
	 * @param taxon
205
	 * @param imageFile
206
	 */
207
	@Deprecated
208
	public static void removeTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
209
		Set<DescriptionElementBase> descriptionElementsToRemove = new HashSet<DescriptionElementBase>();
210
		Set<MediaRepresentationPart> representationPartsToRemove = new HashSet<MediaRepresentationPart>();
211
		
212
		Set<DescriptionElementBase> images = imageGallery.getElements();
213
		
214
		// overmodelling of media in cdmlib makes this a little bit complicated
215
		for(DescriptionElementBase descriptionElement : images){
216
			for(Media media : descriptionElement.getMedia()){
217
				for(MediaRepresentation representation : media.getRepresentations()){
218
					for(MediaRepresentationPart part : representation.getParts()){
219
						if(part.equals(imageFile)){
220
							// because of concurrent modification, we just collect the parts to remove 
221
							representationPartsToRemove.add(part);
222
						}
223
					}
224

    
225
					// and then remove the representation parts here
226
					for (MediaRepresentationPart part : representationPartsToRemove){
227
						representation.removeRepresentationPart(part);
228
					}
229
					// clear set for next run
230
					representationPartsToRemove.clear();
231
					
232
					// description elements with empty representations should be deleted as well
233
					if(representation.getParts().size() == 0){
234
						descriptionElementsToRemove.add(descriptionElement);
235
					}
236
				}
237
			}
238
		}	
239
		
240
		// remove the empty description elements
241
		for(DescriptionElementBase descriptionElement : descriptionElementsToRemove){
242
			imageGallery.removeElement(descriptionElement);
243
		}		
244
	}
245

    
246
	public static final int UP = 1;
247
	public static final int DOWN = -1;
248
	
249
	/**
250
	 * @deprecated not used by EDITor anymore
251
	 * @param input
252
	 * @param selectedImage
253
	 * @param direction
254
	 */
255
	@Deprecated
256
	public static void moveImage(DescriptionElementBase element, ImageFile image,
257
			int direction) {
258
		
259
		MediaRepresentation representation = getImageMediaRepresentation(element);
260
		int index = representation.getParts().indexOf(image);
261
		if (index == -1) {
262
			return;
263
		}		
264
		if (direction == UP && index != 0) {
265
			Collections.swap(representation.getParts(), index, index - 1);
266
		}
267
		if (direction == DOWN && index < representation.getParts().size() - 1) {
268
			Collections.swap(representation.getParts(), index, index + 1);
269
		}
270
	}
271

    
272
	/**
273
	 * @deprecated not used by EDITor anymore
274
	 * @param input
275
	 * @param selectedImage
276
	 */
277
	@Deprecated
278
	public static void removeImage(DescriptionElementBase element, ImageFile image) {
279
		getImageMediaRepresentation(element).getParts().remove(image);
280
	}
281

    
282
	/**
283
	 * Iterate through all taxon's image galleries until the descriptive element containing
284
	 * the ImageFile is found.
285
	 * 
286
	 * @deprecated not used by EDITor anymore
287
	 * @param image
288
	 * @param taxon 
289
	 * @return 
290
	 */
291
	@Deprecated
292
	public static DescriptionElementBase findImageElement(ImageFile image, Taxon taxon) {
293
		if (taxon == null) {
294
			return null;
295
		}
296
		for (TaxonDescription description : taxon.getDescriptions()) {
297
			if (description.isImageGallery()) {
298
				for (DescriptionElementBase element : description.getElements()) {
299
					if (getOrderedImages(element).contains(image)) {
300
						return element;
301
					}
302
				}
303
			}
304
		}
305
		return null;		
306
	}
307
	
308
	/**
309
	 * 
310
	 * @param description
311
	 * @param media
312
	 */
313
	public static void addMediaToGallery(DescriptionBase description, Media media){
314
		DescriptionElementBase element = getGalleryElement(description);
315
		element.addMedia(media);
316
	}
317
	
318
	/**
319
	 * 
320
	 * @param description
321
	 * @param media
322
	 */
323
	public static void removeMediaFromGallery(DescriptionBase description, Media media){
324
		DescriptionElementBase element = getGalleryElement(description);
325
		element.removeMedia(media);
326
	}
327
	
328
	/**
329
	 * 
330
	 * 
331
	 * @param description
332
	 * @return
333
	 */
334
	private static DescriptionElementBase getGalleryElement(DescriptionBase description){
335
		if(! description.isImageGallery()){
336
			logger.error("Description has to have imageGallery flag set.");
337
		}
338
		
339
		Set<DescriptionElementBase> elements = description.getElements();
340
		
341
		if(elements.size() != 1){
342
			logger.error("Image gallery should have only one description");
343
		}
344
		
345
		return elements.iterator().next();
346
	}
347
}
    (1-1/1)