Project

General

Profile

Download (9.73 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.taxon.Taxon;
31

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

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

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

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

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

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