1 |
9479da48
|
Andreas Müller
|
/**
|
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 |
ca21f846
|
Andreas Müller
|
package eu.etaxonomy.cdm.model.media;
|
11 |
9479da48
|
Andreas Müller
|
|
12 |
8bfb99f8
|
Katja Luther
|
import java.util.ArrayList;
|
13 |
22d007e0
|
Andreas Müller
|
import java.util.HashMap;
|
14 |
|
|
import java.util.HashSet;
|
15 |
8bfb99f8
|
Katja Luther
|
import java.util.List;
|
16 |
22d007e0
|
Andreas Müller
|
import java.util.Map;
|
17 |
8bfb99f8
|
Katja Luther
|
import java.util.NoSuchElementException;
|
18 |
22d007e0
|
Andreas Müller
|
import java.util.Set;
|
19 |
8bfb99f8
|
Katja Luther
|
import java.util.SortedMap;
|
20 |
|
|
import java.util.TreeMap;
|
21 |
|
|
import java.util.Map.Entry;
|
22 |
|
|
import java.util.regex.Matcher;
|
23 |
|
|
import java.util.regex.Pattern;
|
24 |
9479da48
|
Andreas Müller
|
|
25 |
22d007e0
|
Andreas Müller
|
import javax.persistence.Basic;
|
26 |
|
|
import javax.persistence.Entity;
|
27 |
|
|
import javax.persistence.FetchType;
|
28 |
|
|
import javax.persistence.Inheritance;
|
29 |
|
|
import javax.persistence.InheritanceType;
|
30 |
|
|
import javax.persistence.JoinTable;
|
31 |
|
|
import javax.persistence.ManyToOne;
|
32 |
|
|
import javax.persistence.OneToMany;
|
33 |
a641fad9
|
ben.clark
|
import javax.validation.constraints.NotNull;
|
34 |
e37a7b96
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlAccessType;
|
35 |
|
|
import javax.xml.bind.annotation.XmlAccessorType;
|
36 |
0ae18eec
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlElement;
|
37 |
|
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
38 |
e37a7b96
|
a.babadshanjan
|
import javax.xml.bind.annotation.XmlIDREF;
|
39 |
|
|
import javax.xml.bind.annotation.XmlRootElement;
|
40 |
|
|
import javax.xml.bind.annotation.XmlSchemaType;
|
41 |
|
|
import javax.xml.bind.annotation.XmlType;
|
42 |
|
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
43 |
9479da48
|
Andreas Müller
|
|
44 |
22d007e0
|
Andreas Müller
|
import org.apache.log4j.Logger;
|
45 |
|
|
import org.hibernate.annotations.Cascade;
|
46 |
|
|
import org.hibernate.annotations.CascadeType;
|
47 |
|
|
import org.hibernate.annotations.Type;
|
48 |
|
|
import org.hibernate.envers.Audited;
|
49 |
|
|
import org.hibernate.search.annotations.Indexed;
|
50 |
|
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
51 |
a641fad9
|
ben.clark
|
import org.hibernate.validator.constraints.NotEmpty;
|
52 |
22d007e0
|
Andreas Müller
|
import org.joda.time.DateTime;
|
53 |
|
|
|
54 |
|
|
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
|
55 |
|
|
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
|
56 |
|
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
57 |
|
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
58 |
|
|
import eu.etaxonomy.cdm.model.common.Language;
|
59 |
|
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
60 |
a641fad9
|
ben.clark
|
import eu.etaxonomy.cdm.validation.Level2;
|
61 |
22d007e0
|
Andreas Müller
|
|
62 |
9479da48
|
Andreas Müller
|
/**
|
63 |
6252a601
|
Andreas Müller
|
* A {@link Media media} is any kind of media that represents a media object.
|
64 |
|
|
* This media object can have multiple {@link MediaRepresentation media representations} that differ in MIME-type
|
65 |
|
|
* and/or quality.
|
66 |
|
|
* E.g.
|
67 |
|
|
* (1) an image can have a tiff and a jpg media representation.
|
68 |
|
|
* (2) an formatted text can have a text/html or an application/pdf representation.
|
69 |
9479da48
|
Andreas Müller
|
* @author m.doering
|
70 |
|
|
* @version 1.0
|
71 |
|
|
* @created 08-Nov-2007 13:06:34
|
72 |
|
|
*/
|
73 |
e37a7b96
|
a.babadshanjan
|
@XmlAccessorType(XmlAccessType.FIELD)
|
74 |
|
|
@XmlType(name = "Media", propOrder = {
|
75 |
|
|
"title",
|
76 |
|
|
"mediaCreated",
|
77 |
|
|
"description",
|
78 |
|
|
"representations",
|
79 |
|
|
"artist"
|
80 |
|
|
})
|
81 |
|
|
@XmlRootElement(name = "Media")
|
82 |
9479da48
|
Andreas Müller
|
@Entity
|
83 |
5d49833e
|
ben.clark
|
@Indexed
|
84 |
ee91bcd9
|
ben.clark
|
@Audited
|
85 |
9479da48
|
Andreas Müller
|
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
86 |
17f0cc4b
|
h.fradin
|
public class Media extends IdentifiableEntity implements Cloneable {
|
87 |
b3730261
|
Andreas Müller
|
private static final long serialVersionUID = -1927421567263473658L;
|
88 |
c7df337d
|
Andreas Müller
|
private static final Logger logger = Logger.getLogger(Media.class);
|
89 |
|
|
|
90 |
91c65d51
|
h.fradin
|
// TODO once hibernate annotations support custom collection type
|
91 |
|
|
// private MultilanguageText title = new MultilanguageText();
|
92 |
ee91bcd9
|
ben.clark
|
@XmlElement(name = "MediaTitle")
|
93 |
|
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
94 |
|
|
@OneToMany(fetch = FetchType.LAZY)
|
95 |
5d49833e
|
ben.clark
|
@IndexedEmbedded
|
96 |
a641fad9
|
ben.clark
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE,CascadeType.DELETE, CascadeType.DELETE_ORPHAN})
|
97 |
|
|
@NotNull
|
98 |
|
|
@NotEmpty(groups = Level2.class)
|
99 |
ee91bcd9
|
ben.clark
|
private Map<Language,LanguageString> title = new HashMap<Language,LanguageString>();
|
100 |
0ae18eec
|
a.babadshanjan
|
|
101 |
5d49833e
|
ben.clark
|
//creation date of the media (not of the record)
|
102 |
2beb45d7
|
ben.clark
|
@XmlElement(name = "MediaCreated", type= String.class)
|
103 |
|
|
@XmlJavaTypeAdapter(DateTimeAdapter.class)
|
104 |
|
|
@Type(type="dateTimeUserType")
|
105 |
|
|
@Basic(fetch = FetchType.LAZY)
|
106 |
|
|
private DateTime mediaCreated;
|
107 |
0ae18eec
|
a.babadshanjan
|
|
108 |
ee91bcd9
|
ben.clark
|
// TODO once hibernate annotations support custom collection type
|
109 |
|
|
// private MultilanguageText description = new MultilanguageText();
|
110 |
e37a7b96
|
a.babadshanjan
|
@XmlElement(name = "MediaDescription")
|
111 |
d26c2ef9
|
a.babadshanjan
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
112 |
ee91bcd9
|
ben.clark
|
@OneToMany(fetch = FetchType.LAZY)
|
113 |
5d49833e
|
ben.clark
|
@IndexedEmbedded
|
114 |
ee91bcd9
|
ben.clark
|
@JoinTable(name = "Media_Description")
|
115 |
a641fad9
|
ben.clark
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE,CascadeType.DELETE,CascadeType.DELETE_ORPHAN})
|
116 |
|
|
@NotNull
|
117 |
ee91bcd9
|
ben.clark
|
private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
|
118 |
0ae18eec
|
a.babadshanjan
|
|
119 |
|
|
//A single medium such as a picture can have multiple representations in files.
|
120 |
|
|
//Common are multiple resolutions or file formats for images for example
|
121 |
|
|
@XmlElementWrapper(name = "MediaRepresentations")
|
122 |
|
|
@XmlElement(name = "MediaRepresentation")
|
123 |
ee91bcd9
|
ben.clark
|
@OneToMany(mappedBy="media",fetch = FetchType.LAZY)
|
124 |
a641fad9
|
ben.clark
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN})
|
125 |
|
|
@NotNull
|
126 |
|
|
@NotEmpty(groups = Level2.class)
|
127 |
1059a526
|
Andreas Müller
|
private Set<MediaRepresentation> representations = new HashSet<MediaRepresentation>();
|
128 |
0ae18eec
|
a.babadshanjan
|
|
129 |
e37a7b96
|
a.babadshanjan
|
@XmlElement(name = "Artist")
|
130 |
|
|
@XmlIDREF
|
131 |
|
|
@XmlSchemaType(name = "IDREF")
|
132 |
ee91bcd9
|
ben.clark
|
@ManyToOne(fetch = FetchType.LAZY)
|
133 |
5d49833e
|
ben.clark
|
@IndexedEmbedded
|
134 |
ee91bcd9
|
ben.clark
|
@Cascade(CascadeType.SAVE_UPDATE)
|
135 |
|
|
private AgentBase artist;
|
136 |
1059a526
|
Andreas Müller
|
|
137 |
c7df337d
|
Andreas Müller
|
/**
|
138 |
|
|
* Factory method
|
139 |
|
|
* @return
|
140 |
|
|
*/
|
141 |
|
|
public static Media NewInstance(){
|
142 |
b3730261
|
Andreas Müller
|
logger.debug("NewInstance");
|
143 |
c7df337d
|
Andreas Müller
|
return new Media();
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
|
|
* Constructor
|
148 |
|
|
*/
|
149 |
|
|
protected Media() {
|
150 |
9479da48
|
Andreas Müller
|
super();
|
151 |
|
|
}
|
152 |
|
|
|
153 |
d5e92940
|
Andreas Müller
|
public Set<MediaRepresentation> getRepresentations(){
|
154 |
a641fad9
|
ben.clark
|
if(representations == null) {
|
155 |
|
|
this.representations = new HashSet<MediaRepresentation>();
|
156 |
|
|
}
|
157 |
d5e92940
|
Andreas Müller
|
return this.representations;
|
158 |
9479da48
|
Andreas Müller
|
}
|
159 |
ee91bcd9
|
ben.clark
|
|
160 |
b3730261
|
Andreas Müller
|
@SuppressWarnings("deprecation")
|
161 |
d5e92940
|
Andreas Müller
|
public void addRepresentation(MediaRepresentation representation){
|
162 |
|
|
if (representation != null){
|
163 |
|
|
this.getRepresentations().add(representation);
|
164 |
|
|
representation.setMedia(this);
|
165 |
|
|
}
|
166 |
9479da48
|
Andreas Müller
|
}
|
167 |
ee91bcd9
|
ben.clark
|
|
168 |
b3730261
|
Andreas Müller
|
@SuppressWarnings("deprecation")
|
169 |
d5e92940
|
Andreas Müller
|
public void removeRepresentation(MediaRepresentation representation){
|
170 |
|
|
this.getRepresentations().remove(representation);
|
171 |
|
|
if (representation != null){
|
172 |
|
|
representation.setMedia(null);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
9479da48
|
Andreas Müller
|
}
|
176 |
|
|
|
177 |
ee91bcd9
|
ben.clark
|
public AgentBase getArtist(){
|
178 |
9479da48
|
Andreas Müller
|
return this.artist;
|
179 |
|
|
}
|
180 |
ee91bcd9
|
ben.clark
|
|
181 |
|
|
public void setArtist(AgentBase artist){
|
182 |
9479da48
|
Andreas Müller
|
this.artist = artist;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
ee91bcd9
|
ben.clark
|
public Map<Language,LanguageString> getTitle(){
|
186 |
a641fad9
|
ben.clark
|
if(title == null) {
|
187 |
|
|
this.title = new HashMap<Language,LanguageString>();
|
188 |
|
|
}
|
189 |
9479da48
|
Andreas Müller
|
return this.title;
|
190 |
|
|
}
|
191 |
ee91bcd9
|
ben.clark
|
|
192 |
|
|
public void addTitle(LanguageString title){
|
193 |
|
|
this.title.put(title.getLanguage(), title);
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
public void removeTitle(Language language){
|
197 |
|
|
this.title.remove(language);
|
198 |
9479da48
|
Andreas Müller
|
}
|
199 |
|
|
|
200 |
2beb45d7
|
ben.clark
|
public DateTime getMediaCreated(){
|
201 |
9479da48
|
Andreas Müller
|
return this.mediaCreated;
|
202 |
|
|
}
|
203 |
ee91bcd9
|
ben.clark
|
|
204 |
2beb45d7
|
ben.clark
|
public void setMediaCreated(DateTime mediaCreated){
|
205 |
9479da48
|
Andreas Müller
|
this.mediaCreated = mediaCreated;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
ee91bcd9
|
ben.clark
|
public Map<Language,LanguageString> getDescription(){
|
209 |
a641fad9
|
ben.clark
|
if(this.description == null) {
|
210 |
|
|
this.description = new HashMap<Language,LanguageString>();
|
211 |
|
|
}
|
212 |
9479da48
|
Andreas Müller
|
return this.description;
|
213 |
|
|
}
|
214 |
ee91bcd9
|
ben.clark
|
|
215 |
|
|
public void addDescription(LanguageString description){
|
216 |
|
|
this.description.put(description.getLanguage(),description);
|
217 |
9479da48
|
Andreas Müller
|
}
|
218 |
ee91bcd9
|
ben.clark
|
|
219 |
c7df337d
|
Andreas Müller
|
public void addDescription(String text, Language language){
|
220 |
eadf5f3d
|
Andreas Müller
|
this.description.put(language, LanguageString.NewInstance(text, language));
|
221 |
9479da48
|
Andreas Müller
|
}
|
222 |
ee91bcd9
|
ben.clark
|
|
223 |
c7df337d
|
Andreas Müller
|
public void removeDescription(Language language){
|
224 |
|
|
this.description.remove(language);
|
225 |
9479da48
|
Andreas Müller
|
}
|
226 |
002b71ff
|
Andreas Müller
|
|
227 |
|
|
//************************* CLONE **************************/
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
/* (non-Javadoc)
|
231 |
|
|
* @see java.lang.Object#clone()
|
232 |
|
|
*/
|
233 |
|
|
@Override
|
234 |
|
|
public Object clone() throws CloneNotSupportedException{
|
235 |
|
|
Media result = (Media)super.clone();
|
236 |
c76ef9e7
|
Andreas Müller
|
//description
|
237 |
|
|
result.description = new HashMap<Language, LanguageString>();
|
238 |
|
|
for (Language language: this.description.keySet()){
|
239 |
|
|
result.description.put(language, this.description.get(language));
|
240 |
|
|
}
|
241 |
|
|
//title
|
242 |
|
|
result.title = new HashMap<Language, LanguageString>();
|
243 |
|
|
for (Language language: this.title.keySet()){
|
244 |
|
|
result.title.put(language, this.title.get(language));
|
245 |
|
|
}
|
246 |
|
|
//media representations
|
247 |
|
|
result.representations = new HashSet<MediaRepresentation>();
|
248 |
|
|
for (MediaRepresentation mediaRepresentation: this.representations){
|
249 |
|
|
result.representations.add((MediaRepresentation)mediaRepresentation.clone());
|
250 |
|
|
}
|
251 |
|
|
//no changes to: artist
|
252 |
002b71ff
|
Andreas Müller
|
return result;
|
253 |
|
|
}
|
254 |
17f0cc4b
|
h.fradin
|
|
255 |
|
|
public int compareTo(Object o) {
|
256 |
|
|
return 0;
|
257 |
|
|
}
|
258 |
8bfb99f8
|
Katja Luther
|
/**
|
259 |
|
|
* @param mimeTypeRegexes
|
260 |
|
|
* @param size
|
261 |
|
|
* @param widthOrDuration
|
262 |
|
|
* @param height
|
263 |
|
|
* @return
|
264 |
|
|
*
|
265 |
|
|
*
|
266 |
|
|
*/
|
267 |
ae1d3bd5
|
Katja Luther
|
public MediaRepresentation findBestMatchingRepresentation(Integer size, Integer height, Integer widthOrDuration, String[] mimeTypes){
|
268 |
8bfb99f8
|
Katja Luther
|
// find best matching representations of each media
|
269 |
|
|
Set<MediaRepresentation> reps = this.getRepresentations();
|
270 |
|
|
|
271 |
|
|
List<Media> returnMedia = new ArrayList<Media>(reps.size());
|
272 |
|
|
SortedMap<Integer, MediaRepresentation> prefRepresentations
|
273 |
|
|
= orderMediaRepresentations(mimeTypes, size, widthOrDuration, height);
|
274 |
|
|
try {
|
275 |
|
|
// take first one and remove all other representations
|
276 |
|
|
MediaRepresentation prefOne = prefRepresentations.get(prefRepresentations.firstKey());
|
277 |
|
|
|
278 |
|
|
return prefOne;
|
279 |
|
|
|
280 |
|
|
} catch (NoSuchElementException nse) {
|
281 |
|
|
/* IGNORE */
|
282 |
|
|
}
|
283 |
|
|
return null;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* @param mimeTypeRegexes
|
289 |
|
|
* @param size
|
290 |
|
|
* @param widthOrDuration
|
291 |
|
|
* @param height
|
292 |
|
|
* @return
|
293 |
|
|
*
|
294 |
|
|
*
|
295 |
|
|
*/
|
296 |
|
|
private SortedMap<Integer, MediaRepresentation> orderMediaRepresentations(String[] mimeTypeRegexes,
|
297 |
|
|
Integer size, Integer widthOrDuration, Integer height) {
|
298 |
|
|
SortedMap<Integer, MediaRepresentation> prefRepr = new TreeMap<Integer, MediaRepresentation>();
|
299 |
|
|
SortedMap<String, MediaRepresentation> sortedForSizeDistance = new TreeMap<String, MediaRepresentation>();
|
300 |
|
|
String keyString = "";
|
301 |
|
|
for (String mimeTypeRegex : mimeTypeRegexes) {
|
302 |
|
|
// getRepresentationByMimeType
|
303 |
|
|
Pattern mimeTypePattern = Pattern.compile(mimeTypeRegex);
|
304 |
|
|
int representationCnt = 0;
|
305 |
|
|
for (MediaRepresentation representation : getRepresentations()) {
|
306 |
|
|
|
307 |
|
|
Matcher mather = mimeTypePattern.matcher(representation.getMimeType());
|
308 |
|
|
if (mather.matches()) {
|
309 |
|
|
int dwa = 0;
|
310 |
|
|
|
311 |
|
|
//first the size is used for comparison
|
312 |
|
|
for (MediaRepresentationPart part : representation.getParts()) {
|
313 |
|
|
if (part.getSize()!= null){
|
314 |
|
|
int sizeOfPart = part.getSize();
|
315 |
|
|
int distance = sizeOfPart - size;
|
316 |
|
|
if (distance < 0) {
|
317 |
|
|
distance*= -1;
|
318 |
|
|
}
|
319 |
|
|
dwa += distance;
|
320 |
|
|
}
|
321 |
|
|
//if height and width/duration is defined, add this information, too
|
322 |
|
|
if (height != 0 && widthOrDuration != 0){
|
323 |
|
|
int dw = 0;
|
324 |
|
|
|
325 |
|
|
if (part instanceof ImageFile) {
|
326 |
|
|
ImageFile image = (ImageFile) part;
|
327 |
|
|
dw = image.getWidth() * image.getHeight() - height * widthOrDuration;
|
328 |
|
|
}
|
329 |
|
|
else if (part instanceof MovieFile){
|
330 |
|
|
MovieFile movie = (MovieFile) part;
|
331 |
|
|
dw = movie.getDuration() - widthOrDuration;
|
332 |
|
|
|
333 |
|
|
}else if (part instanceof AudioFile){
|
334 |
|
|
AudioFile audio = (AudioFile) part;
|
335 |
|
|
dw = audio.getDuration() - widthOrDuration;
|
336 |
|
|
|
337 |
|
|
}
|
338 |
|
|
if (dw < 0) {
|
339 |
|
|
dw *= -1;
|
340 |
|
|
}
|
341 |
|
|
dwa += dw;
|
342 |
|
|
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
dwa = (representation.getParts().size() > 0 ? dwa / representation.getParts().size() : 0);
|
346 |
|
|
|
347 |
|
|
//keyString =(dwa + representationCnt++) + '_' + representation.getMimeType();
|
348 |
|
|
|
349 |
|
|
prefRepr.put((dwa + representationCnt++), representation);
|
350 |
|
|
System.out.println(prefRepr.get(prefRepr.firstKey()) + " --- " + prefRepr.firstKey());
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
}
|
356 |
|
|
return prefRepr;
|
357 |
|
|
}
|
358 |
17f0cc4b
|
h.fradin
|
|
359 |
9479da48
|
Andreas Müller
|
}
|