Project

General

Profile

Download (8.42 KB) Statistics
| Branch: | Tag: | Revision:
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.util;
11

    
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18

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

    
30
/**
31
 * @author n.hoffmann
32
 * @since Jan 27, 2010
33
 */
34
public class ImagesUtility {
35
	private static final Logger logger = Logger.getLogger(ImagesUtility.class);
36

    
37
	/**
38
	 * Quick and dirty method to get an element's first image file.
39
	 *
40
	 * @param element
41
	 * @return
42
	 * @deprecated not used by EDITor anymore
43
	 */
44
	@Deprecated
45
	public static ImageFile getImage(DescriptionElementBase element) {
46
		List<Media> medias = element.getMedia();
47

    
48
		for(Media media : medias){
49
			Set<MediaRepresentation> representations = media.getRepresentations();
50

    
51
			for(MediaRepresentation representation : representations){
52
				List<MediaRepresentationPart> parts = representation.getParts();
53

    
54
				for (MediaRepresentationPart part : parts){
55
					if(part instanceof ImageFile){
56
						return (ImageFile) part;
57
					}
58
				}
59
			}
60
		}
61
		return null;
62
	}
63

    
64
	/**
65
	 * @deprecated not used by EDITor anymore
66
	 */
67
	@Deprecated
68
	public static List<ImageFile> getOrderedImages(DescriptionElementBase element){
69
		List<ImageFile> imageList = new ArrayList<ImageFile>();
70
		MediaRepresentation representation = getImageMediaRepresentation(element);
71
		if (representation != null) {
72
			for (MediaRepresentationPart part : representation.getParts()){
73
				if(!(part instanceof ImageFile)){
74
					throw new RuntimeException("Your database contains media that mix Image Files with non-Image Files.");
75
				} else {
76
					imageList.add((ImageFile) part);
77
				}
78
			}
79
		}
80
		return imageList;
81
	}
82

    
83
	/**
84
	 * Returns the first Representation with images. If none is found, a
85
	 * Representation for storing images is created and returned.
86
	 *
87
	 * @deprecated not used by EDITor anymore
88
	 * @param element
89
	 * @return
90
	 */
91
	@Deprecated
92
	private static MediaRepresentation getImageMediaRepresentation(DescriptionElementBase element) {
93
		// Drill down until a representation with images is found
94
		for(Media media : element.getMedia()){
95
			Set<MediaRepresentation> representations = media.getRepresentations();
96
			for(MediaRepresentation representation : representations){
97
				List<MediaRepresentationPart> parts = representation.getParts();
98
				for (MediaRepresentationPart part : parts){
99
					if(part instanceof ImageFile){
100
						return representation;
101
					}
102
				}
103
			}
104
		}
105
		// No representation with images found - create
106
		MediaRepresentation representation = MediaRepresentation.NewInstance();
107
		Media media = Media.NewInstance();
108
		element.addMedia(media);
109
		media.addRepresentation(representation);
110
		return representation;
111
	}
112

    
113
	/**
114
	 * @deprecated not used by EDITor anymore
115
	 * @param description
116
	 * @return
117
	 */
118
	@Deprecated
119
	public static Set<ImageFile> getImages(TaxonDescription description){
120
		Set<ImageFile> images = new HashSet<ImageFile>();
121

    
122
		for (DescriptionElementBase element : description.getElements()){
123

    
124
			Feature feature = element.getFeature();
125

    
126
			if(feature.equals(Feature.IMAGE())){
127
				List<Media> medias = element.getMedia();
128

    
129
				for(Media media : medias){
130
					Set<MediaRepresentation> representations = media.getRepresentations();
131

    
132
					for(MediaRepresentation representation : representations){
133
						List<MediaRepresentationPart> parts = representation.getParts();
134

    
135
						for (MediaRepresentationPart part : parts){
136
							if(part instanceof ImageFile){
137
								images.add((ImageFile) part);
138
							}
139
						}
140
					}
141
				}
142
			}
143
		}
144

    
145
		return images;
146
	}
147

    
148
	/**
149
	 * @deprecated not used by EDITor anymore
150
	 * @param taxon
151
	 * @param imageFile
152
	 */
153
	@Deprecated
154
	public static void addTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
155

    
156
		imageGallery.addElement(createImageElement(imageFile));
157

    
158
	}
159

    
160
	/**
161
	 * @deprecated not used by EDITor anymore
162
	 * @param imageFile
163
	 * @return
164
	 */
165
	@Deprecated
166
	public static DescriptionElementBase createImageElement(ImageFile imageFile) {
167

    
168
		DescriptionElementBase descriptionElement = TextData.NewInstance(Feature.IMAGE());
169

    
170
		Media media = Media.NewInstance();
171
		MediaRepresentation representation = MediaRepresentation.NewInstance();
172

    
173
		representation.addRepresentationPart(imageFile);
174

    
175
		media.addRepresentation(representation);
176

    
177
		descriptionElement.addMedia(media);
178

    
179
		return descriptionElement;
180

    
181
	}
182

    
183
	/**
184
	 * Adds a new, empty image file to the end of a description element's
185
	 * ordered list of images.
186
	 *
187
	 * @deprecated not used by EDITor anymore
188
	 * @param element
189
	 * @return
190
	 */
191
	@Deprecated
192
	public static ImageFile addImagePart(DescriptionElementBase element) {
193
		ImageFile imageFile = ImageFile.NewInstance(null, null);
194
		getImageMediaRepresentation(element).addRepresentationPart(imageFile);
195
		return imageFile;
196
	}
197

    
198
	/**
199
	 * @deprecated not used by EDITor anymore
200
	 * @param taxon
201
	 * @param imageFile
202
	 */
203
	@Deprecated
204
	public static void removeTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
205
		Set<DescriptionElementBase> descriptionElementsToRemove = new HashSet<DescriptionElementBase>();
206
		Set<MediaRepresentationPart> representationPartsToRemove = new HashSet<MediaRepresentationPart>();
207

    
208
		Set<DescriptionElementBase> images = imageGallery.getElements();
209

    
210
		// overmodelling of media in cdmlib makes this a little bit complicated
211
		for(DescriptionElementBase descriptionElement : images){
212
			for(Media media : descriptionElement.getMedia()){
213
				for(MediaRepresentation representation : media.getRepresentations()){
214
					for(MediaRepresentationPart part : representation.getParts()){
215
						if(part.equals(imageFile)){
216
							// because of concurrent modification, we just collect the parts to remove
217
							representationPartsToRemove.add(part);
218
						}
219
					}
220

    
221
					// and then remove the representation parts here
222
					for (MediaRepresentationPart part : representationPartsToRemove){
223
						representation.removeRepresentationPart(part);
224
					}
225
					// clear set for next run
226
					representationPartsToRemove.clear();
227

    
228
					// description elements with empty representations should be deleted as well
229
					if(representation.getParts().size() == 0){
230
						descriptionElementsToRemove.add(descriptionElement);
231
					}
232
				}
233
			}
234
		}
235

    
236
		// remove the empty description elements
237
		for(DescriptionElementBase descriptionElement : descriptionElementsToRemove){
238
			imageGallery.removeElement(descriptionElement);
239
		}
240
	}
241

    
242
	/**
243
	 * Iterate through all taxon's image galleries until the descriptive element containing
244
	 * the ImageFile is found.
245
	 *
246
	 * @deprecated not used by EDITor anymore
247
	 * @param image
248
	 * @param taxon
249
	 * @return
250
	 */
251
	@Deprecated
252
	public static DescriptionElementBase findImageElement(ImageFile image, Taxon taxon) {
253
		if (taxon == null) {
254
			return null;
255
		}
256
		for (TaxonDescription description : taxon.getDescriptions()) {
257
			if (description.isImageGallery()) {
258
				for (DescriptionElementBase element : description.getElements()) {
259
					if (getOrderedImages(element).contains(image)) {
260
						return element;
261
					}
262
				}
263
			}
264
		}
265
		return null;
266
	}
267

    
268
	public static void addMediaToGallery(DescriptionBase description, Media media){
269
		DescriptionElementBase element = getGalleryElement(description);
270
		element.addMedia(media);
271
	}
272

    
273
	public static void removeMediaFromGallery(DescriptionBase description, Media media){
274
		DescriptionElementBase element = getGalleryElement(description);
275
		element.removeMedia(media);
276
	}
277

    
278
	private static DescriptionElementBase getGalleryElement(DescriptionBase<?> description){
279
		if(! description.isImageGallery()){
280
			logger.error("Description has to have imageGallery flag set.");
281
		}
282

    
283
		Set<DescriptionElementBase> elements = description.getElements();
284

    
285
		if(elements.size() != 1){
286
			logger.error("Image gallery should have only one description");
287
		}
288

    
289
		return elements.iterator().next();
290
	}
291
}
(12-12/18)