2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.utility
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collections
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import org
.apache
.log4j
.Logger
;
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
;
36 public class ImagesUtility
{
37 private static final Logger logger
= Logger
.getLogger(ImagesUtility
.class);
40 * Quick and dirty method to get an element's first image file.
44 * @deprecated not used by EDITor anymore
47 public static ImageFile
getImage(DescriptionElementBase element
) {
48 List
<Media
> medias
= element
.getMedia();
50 for(Media media
: medias
){
51 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
53 for(MediaRepresentation representation
: representations
){
54 List
<MediaRepresentationPart
> parts
= representation
.getParts();
56 for (MediaRepresentationPart part
: parts
){
57 if(part
instanceof ImageFile
){
58 return (ImageFile
) part
;
67 * @deprecated not used by EDITor anymore
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.");
78 imageList
.add((ImageFile
) part
);
86 * Returns the first Representation with images. If none is found, a
87 * Representation for storing images is created and returned.
89 * @deprecated not used by EDITor anymore
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
;
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
;
116 * @deprecated not used by EDITor anymore
121 public static Set
<ImageFile
> getImages(TaxonDescription description
){
122 Set
<ImageFile
> images
= new HashSet
<ImageFile
>();
124 for (DescriptionElementBase element
: description
.getElements()){
126 Feature feature
= element
.getFeature();
128 if(feature
.equals(Feature
.IMAGE())){
129 List
<Media
> medias
= element
.getMedia();
131 for(Media media
: medias
){
132 Set
<MediaRepresentation
> representations
= media
.getRepresentations();
134 for(MediaRepresentation representation
: representations
){
135 List
<MediaRepresentationPart
> parts
= representation
.getParts();
137 for (MediaRepresentationPart part
: parts
){
138 if(part
instanceof ImageFile
){
139 images
.add((ImageFile
) part
);
151 * @deprecated not used by EDITor anymore
156 public static void addTaxonImage(Taxon taxon
, DescriptionBase
<?
> imageGallery
, ImageFile imageFile
) {
158 imageGallery
.addElement(createImageElement(imageFile
));
163 * @deprecated not used by EDITor anymore
168 public static DescriptionElementBase
createImageElement(ImageFile imageFile
) {
170 DescriptionElementBase descriptionElement
= TextData
.NewInstance(Feature
.IMAGE());
172 Media media
= Media
.NewInstance();
173 MediaRepresentation representation
= MediaRepresentation
.NewInstance();
175 representation
.addRepresentationPart(imageFile
);
177 media
.addRepresentation(representation
);
179 descriptionElement
.addMedia(media
);
181 return descriptionElement
;
186 * Adds a new, empty image file to the end of a description element's
187 * ordered list of images.
189 * @deprecated not used by EDITor anymore
194 public static ImageFile
addImagePart(DescriptionElementBase element
) {
195 ImageFile imageFile
= ImageFile
.NewInstance(null, null);
196 getImageMediaRepresentation(element
).addRepresentationPart(imageFile
);
201 * @deprecated not used by EDITor anymore
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
>();
210 Set
<DescriptionElementBase
> images
= imageGallery
.getElements();
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
);
223 // and then remove the representation parts here
224 for (MediaRepresentationPart part
: representationPartsToRemove
){
225 representation
.removeRepresentationPart(part
);
227 // clear set for next run
228 representationPartsToRemove
.clear();
230 // description elements with empty representations should be deleted as well
231 if(representation
.getParts().size() == 0){
232 descriptionElementsToRemove
.add(descriptionElement
);
238 // remove the empty description elements
239 for(DescriptionElementBase descriptionElement
: descriptionElementsToRemove
){
240 imageGallery
.removeElement(descriptionElement
);
244 public static final int UP
= 1;
245 public static final int DOWN
= -1;
248 * @deprecated not used by EDITor anymore
250 * @param selectedImage
254 public static void moveImage(DescriptionElementBase element
, ImageFile image
,
257 MediaRepresentation representation
= getImageMediaRepresentation(element
);
258 int index
= representation
.getParts().indexOf(image
);
262 if (direction
== UP
&& index
!= 0) {
263 Collections
.swap(representation
.getParts(), index
, index
- 1);
265 if (direction
== DOWN
&& index
< representation
.getParts().size() - 1) {
266 Collections
.swap(representation
.getParts(), index
, index
+ 1);
271 * @deprecated not used by EDITor anymore
273 * @param selectedImage
276 public static void removeImage(DescriptionElementBase element
, ImageFile image
) {
277 getImageMediaRepresentation(element
).getParts().remove(image
);
281 * Iterate through all taxon's image galleries until the descriptive element containing
282 * the ImageFile is found.
284 * @deprecated not used by EDITor anymore
290 public static DescriptionElementBase
findImageElement(ImageFile image
, Taxon taxon
) {
294 for (TaxonDescription description
: taxon
.getDescriptions()) {
295 if (description
.isImageGallery()) {
296 for (DescriptionElementBase element
: description
.getElements()) {
297 if (getOrderedImages(element
).contains(image
)) {
311 public static void addMediaToGallery(DescriptionBase description
, Media media
){
312 DescriptionElementBase element
= getGalleryElement(description
);
313 element
.addMedia(media
);
321 public static void removeMediaFromGallery(DescriptionBase description
, Media media
){
322 DescriptionElementBase element
= getGalleryElement(description
);
323 element
.removeMedia(media
);
332 private static DescriptionElementBase
getGalleryElement(DescriptionBase description
){
333 if(! description
.isImageGallery()){
334 logger
.error("Description has to have imageGallery flag set.");
337 Set
<DescriptionElementBase
> elements
= description
.getElements();
339 if(elements
.size() != 1){
340 logger
.error("Image gallery should have only one description");
343 return elements
.iterator().next();