typo in @param description
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / model / ImagesHelper.java
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.taxeditor.model;
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.ReferencedMedia;
31 import eu.etaxonomy.cdm.model.taxon.Taxon;
32
33 /**
34 * @author p.ciardelli
35 * @created 31.03.2009
36 * @version 1.0
37 *
38 * This has to be moved to library at some point. Unfortunately, the whole handling of images
39 * is very half heartedly implemented in the library. We will not mark this here as depreciated
40 * until the situation in the library improves.
41 *
42 */
43 public class ImagesHelper {
44 private static final Logger logger = Logger
45 .getLogger(ImagesHelper.class);
46
47 /**
48 * Quick and dirty method to get an element's first image file.
49 *
50 * @param element
51 * @return
52 */
53 public static ImageFile getImage(DescriptionElementBase element) {
54 List<Media> medias = element.getMedia();
55
56 for(Media media : medias){
57 Set<MediaRepresentation> representations = media.getRepresentations();
58
59 for(MediaRepresentation representation : representations){
60 List<MediaRepresentationPart> parts = representation.getParts();
61
62 for (MediaRepresentationPart part : parts){
63 if(part instanceof ImageFile){
64 return (ImageFile) part;
65 }
66 }
67 }
68 }
69 return null;
70 }
71
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 * @param element
92 * @return
93 */
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 = ReferencedMedia.NewInstance();
110 element.addMedia(media);
111 media.addRepresentation(representation);
112 return representation;
113 }
114
115 /**
116 * @param description
117 * @return
118 */
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 * @param taxon
150 * @param imageFile
151 */
152 public static void addTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
153
154 imageGallery.addElement(createImageElement(imageFile));
155
156 }
157
158 public static DescriptionElementBase createImageElement(ImageFile imageFile) {
159
160 DescriptionElementBase descriptionElement = TextData.NewInstance(Feature.IMAGE());
161
162 Media media = Media.NewInstance();
163 MediaRepresentation representation = MediaRepresentation.NewInstance();
164
165 representation.addRepresentationPart(imageFile);
166
167 media.addRepresentation(representation);
168
169 descriptionElement.addMedia(media);
170
171 return descriptionElement;
172
173 }
174
175 /**
176 * Adds a new, empty image file to the end of a description element's
177 * ordered list of images.
178 *
179 * @param element
180 * @return
181 */
182 public static ImageFile addImagePart(DescriptionElementBase element) {
183 ImageFile imageFile = ImageFile.NewInstance(null, null);
184 getImageMediaRepresentation(element).addRepresentationPart(imageFile);
185 return imageFile;
186 }
187
188 /**
189 * @param taxon
190 * @param imageFile
191 */
192 public static void removeTaxonImage(Taxon taxon, DescriptionBase<?> imageGallery, ImageFile imageFile) {
193 Set<DescriptionElementBase> descriptionElementsToRemove = new HashSet<DescriptionElementBase>();
194 Set<MediaRepresentationPart> representationPartsToRemove = new HashSet<MediaRepresentationPart>();
195
196 Set<DescriptionElementBase> images = imageGallery.getElements();
197
198 // overmodelling of media in cdmlib makes this a little bit complicated
199 for(DescriptionElementBase descriptionElement : images){
200 for(Media media : descriptionElement.getMedia()){
201 for(MediaRepresentation representation : media.getRepresentations()){
202 for(MediaRepresentationPart part : representation.getParts()){
203 if(part.equals(imageFile)){
204 // because of concurrent modification, we just collect the parts to remove
205 representationPartsToRemove.add(part);
206 }
207 }
208
209 // and then remove the representation parts here
210 for (MediaRepresentationPart part : representationPartsToRemove){
211 representation.removeRepresentationPart(part);
212 }
213 // clear set for next run
214 representationPartsToRemove.clear();
215
216 // description elements with empty representations should be deleted as well
217 if(representation.getParts().size() == 0){
218 descriptionElementsToRemove.add(descriptionElement);
219 }
220 }
221 }
222 }
223
224 // remove the empty description elements
225 for(DescriptionElementBase descriptionElement : descriptionElementsToRemove){
226 imageGallery.removeElement(descriptionElement);
227 }
228 }
229
230 public static final int UP = 1;
231 public static final int DOWN = -1;
232
233 /**
234 * @param input
235 * @param selectedImage
236 * @param direction
237 */
238 public static void moveImage(DescriptionElementBase element, ImageFile image,
239 int direction) {
240
241 MediaRepresentation representation = getImageMediaRepresentation(element);
242 int index = representation.getParts().indexOf(image);
243 if (index == -1) {
244 return;
245 }
246 if (direction == UP && index != 0) {
247 Collections.swap(representation.getParts(), index, index - 1);
248 }
249 if (direction == DOWN && index < representation.getParts().size() - 1) {
250 Collections.swap(representation.getParts(), index, index + 1);
251 }
252 }
253
254 /**
255 * @param input
256 * @param selectedImage
257 */
258 public static void removeImage(DescriptionElementBase element, ImageFile image) {
259 getImageMediaRepresentation(element).getParts().remove(image);
260 }
261
262 /**
263 * Iterate through all taxon's image galleries until the descriptive element containing
264 * the ImageFile is found.
265 *
266 * @param image
267 * @param taxon
268 * @return
269 */
270 public static DescriptionElementBase findImageElement(ImageFile image, Taxon taxon) {
271 if (taxon == null) {
272 return null;
273 }
274 for (TaxonDescription description : taxon.getDescriptions()) {
275 if (description.isImageGallery()) {
276 for (DescriptionElementBase element : description.getElements()) {
277 if (getOrderedImages(element).contains(image)) {
278 return element;
279 }
280 }
281 }
282 }
283 return null;
284 }
285 }