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.model.media;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import javax.persistence.Basic;
|
19
|
import javax.persistence.Entity;
|
20
|
import javax.persistence.FetchType;
|
21
|
import javax.persistence.Inheritance;
|
22
|
import javax.persistence.InheritanceType;
|
23
|
import javax.persistence.JoinTable;
|
24
|
import javax.persistence.ManyToOne;
|
25
|
import javax.persistence.MapKeyJoinColumn;
|
26
|
import javax.persistence.OneToMany;
|
27
|
import javax.persistence.Transient;
|
28
|
import javax.validation.constraints.NotNull;
|
29
|
import javax.xml.bind.annotation.XmlAccessType;
|
30
|
import javax.xml.bind.annotation.XmlAccessorType;
|
31
|
import javax.xml.bind.annotation.XmlElement;
|
32
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
33
|
import javax.xml.bind.annotation.XmlIDREF;
|
34
|
import javax.xml.bind.annotation.XmlRootElement;
|
35
|
import javax.xml.bind.annotation.XmlSchemaType;
|
36
|
import javax.xml.bind.annotation.XmlType;
|
37
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
38
|
|
39
|
import org.apache.commons.lang.StringUtils;
|
40
|
import org.apache.log4j.Logger;
|
41
|
import org.hibernate.annotations.Cascade;
|
42
|
import org.hibernate.annotations.CascadeType;
|
43
|
import org.hibernate.annotations.Type;
|
44
|
import org.hibernate.envers.Audited;
|
45
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
46
|
import org.hibernate.validator.constraints.NotEmpty;
|
47
|
import org.joda.time.DateTime;
|
48
|
|
49
|
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
|
50
|
import eu.etaxonomy.cdm.jaxb.MultilanguageTextAdapter;
|
51
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
52
|
import eu.etaxonomy.cdm.model.common.IMultiLanguageTextHolder;
|
53
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
54
|
import eu.etaxonomy.cdm.model.common.Language;
|
55
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
56
|
import eu.etaxonomy.cdm.model.common.MultilanguageText;
|
57
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
58
|
import eu.etaxonomy.cdm.strategy.cache.media.MediaDefaultCacheStrategy;
|
59
|
import eu.etaxonomy.cdm.validation.Level2;
|
60
|
|
61
|
/**
|
62
|
* A {@link Media media} is any kind of media that represents a media object.
|
63
|
* This media object can have multiple {@link MediaRepresentation media representations} that differ in MIME-type
|
64
|
* and/or quality.
|
65
|
* E.g.
|
66
|
* (1) an image can have a tiff and a jpg media representation.
|
67
|
* (2) an formatted text can have a text/html or an application/pdf representation.
|
68
|
* @author m.doering
|
69
|
* @version 1.0
|
70
|
* @created 08-Nov-2007 13:06:34
|
71
|
*/
|
72
|
@XmlAccessorType(XmlAccessType.FIELD)
|
73
|
@XmlType(name = "Media", propOrder = {
|
74
|
"title",
|
75
|
"mediaCreated",
|
76
|
"description",
|
77
|
"representations",
|
78
|
"artist"
|
79
|
})
|
80
|
@XmlRootElement(name = "Media")
|
81
|
@Entity
|
82
|
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
|
83
|
//@Indexed
|
84
|
@Audited
|
85
|
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
86
|
public class Media extends IdentifiableEntity<IIdentifiableEntityCacheStrategy> implements Cloneable, IMultiLanguageTextHolder {
|
87
|
private static final long serialVersionUID = -1927421567263473658L;
|
88
|
@SuppressWarnings("unused")
|
89
|
private static final Logger logger = Logger.getLogger(Media.class);
|
90
|
|
91
|
// TODO once hibernate annotations support custom collection type
|
92
|
// private MultilanguageText title = new MultilanguageText();
|
93
|
@XmlElement(name = "MediaTitle")
|
94
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
95
|
@OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
|
96
|
@MapKeyJoinColumn(name="title_mapkey_id")
|
97
|
@IndexedEmbedded
|
98
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE,CascadeType.DELETE, CascadeType.REFRESH})
|
99
|
@NotNull
|
100
|
@NotEmpty(groups = Level2.class)
|
101
|
private Map<Language,LanguageString> title = new HashMap<>();
|
102
|
|
103
|
//creation date of the media (not of the record)
|
104
|
@XmlElement(name = "MediaCreated", type= String.class)
|
105
|
@XmlJavaTypeAdapter(DateTimeAdapter.class)
|
106
|
@Type(type="dateTimeUserType")
|
107
|
@Basic(fetch = FetchType.LAZY)
|
108
|
private DateTime mediaCreated;
|
109
|
|
110
|
// TODO once hibernate annotations support custom collection type
|
111
|
// private MultilanguageText description = new MultilanguageText();
|
112
|
@XmlElement(name = "MediaDescription")
|
113
|
@XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
|
114
|
@OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
|
115
|
@MapKeyJoinColumn(name="description_mapkey_id")
|
116
|
@JoinTable(name = "Media_Description")
|
117
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE,CascadeType.DELETE, CascadeType.REFRESH})
|
118
|
@IndexedEmbedded
|
119
|
@NotNull
|
120
|
private Map<Language,LanguageString> description = new HashMap<>();
|
121
|
|
122
|
//A single medium such as a picture can have multiple representations in files.
|
123
|
//Common are multiple resolutions or file formats for images for example
|
124
|
@XmlElementWrapper(name = "MediaRepresentations")
|
125
|
@XmlElement(name = "MediaRepresentation")
|
126
|
@OneToMany(mappedBy="media",fetch = FetchType.LAZY, orphanRemoval=true)
|
127
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.REFRESH})
|
128
|
@NotNull
|
129
|
@NotEmpty(groups = Level2.class)
|
130
|
private Set<MediaRepresentation> representations = new HashSet<MediaRepresentation>();
|
131
|
|
132
|
@XmlElement(name = "Artist")
|
133
|
@XmlIDREF
|
134
|
@XmlSchemaType(name = "IDREF")
|
135
|
@ManyToOne(fetch = FetchType.LAZY)
|
136
|
@IndexedEmbedded
|
137
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
138
|
private AgentBase<?> artist;
|
139
|
|
140
|
|
141
|
/**
|
142
|
* Factory method
|
143
|
* @return
|
144
|
*/
|
145
|
public static Media NewInstance(){
|
146
|
return new Media();
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Factory method which creates a new media, adds a reprsentation including mime type and suffix information
|
152
|
* and adds to the later a representation part for a given uri and size
|
153
|
* Returns <code>null</code> if uri is empty
|
154
|
* @return Media
|
155
|
*/
|
156
|
public static Media NewInstance(URI uri, Integer size, String mimeType, String suffix){
|
157
|
//TODO improve type recognition
|
158
|
Class<? extends MediaRepresentationPart> clazz = null;
|
159
|
if (StringUtils.isNotBlank(mimeType)){
|
160
|
if (mimeType.matches("image.*")){
|
161
|
clazz = ImageFile.class;
|
162
|
}
|
163
|
}
|
164
|
if (StringUtils.isNotBlank(suffix)){
|
165
|
if (suffix.matches("\\.(gif|jpe?g|tiff?)")){
|
166
|
clazz = ImageFile.class;
|
167
|
}
|
168
|
}else if (uri != null){
|
169
|
if (uri.toString().matches("\\.(gif|jpe?g|tiff?)")){
|
170
|
clazz = ImageFile.class;
|
171
|
}
|
172
|
}
|
173
|
MediaRepresentation representation = MediaRepresentation.NewInstance(mimeType, suffix, uri, size,clazz);
|
174
|
if (representation == null){
|
175
|
return null;
|
176
|
}
|
177
|
Media media = new Media();
|
178
|
media.addRepresentation(representation);
|
179
|
return media;
|
180
|
}
|
181
|
|
182
|
//********************************* CONSTRUCTOR **************************/
|
183
|
|
184
|
/**
|
185
|
* Constructor
|
186
|
*/
|
187
|
protected Media() {
|
188
|
super();
|
189
|
setMediaCacheStrategy();
|
190
|
}
|
191
|
|
192
|
private void setMediaCacheStrategy() {
|
193
|
// if (getClass() == Media.class){
|
194
|
this.cacheStrategy = MediaDefaultCacheStrategy.NewInstance();
|
195
|
// }
|
196
|
}
|
197
|
|
198
|
// ********************* GETTER / SETTER **************************/
|
199
|
|
200
|
public Set<MediaRepresentation> getRepresentations(){
|
201
|
if(representations == null) {
|
202
|
this.representations = new HashSet<MediaRepresentation>();
|
203
|
}
|
204
|
return this.representations;
|
205
|
}
|
206
|
|
207
|
@SuppressWarnings("deprecation")
|
208
|
public void addRepresentation(MediaRepresentation representation){
|
209
|
if (representation != null){
|
210
|
this.getRepresentations().add(representation);
|
211
|
representation.setMedia(this);
|
212
|
}
|
213
|
}
|
214
|
|
215
|
@SuppressWarnings("deprecation")
|
216
|
public void removeRepresentation(MediaRepresentation representation){
|
217
|
this.getRepresentations().remove(representation);
|
218
|
if (representation != null){
|
219
|
representation.setMedia(null);
|
220
|
}
|
221
|
|
222
|
}
|
223
|
|
224
|
public AgentBase getArtist(){
|
225
|
return this.artist;
|
226
|
}
|
227
|
|
228
|
public void setArtist(AgentBase artist){
|
229
|
this.artist = artist;
|
230
|
}
|
231
|
|
232
|
//************************ title / title cache *********************************
|
233
|
|
234
|
public LanguageString getTitle(){
|
235
|
return getTitle(Language.DEFAULT());
|
236
|
}
|
237
|
|
238
|
public LanguageString getTitle(Language language){
|
239
|
return title.get(language);
|
240
|
}
|
241
|
|
242
|
@Transient
|
243
|
public Map<Language,LanguageString> getAllTitles(){
|
244
|
if(title == null) {
|
245
|
this.title = new HashMap<>();
|
246
|
}
|
247
|
return this.title;
|
248
|
}
|
249
|
/**
|
250
|
* Adds the languageString to the {@link MultilanguageText multilanguage text}
|
251
|
* used to be the title of <i>this</i> media.
|
252
|
*
|
253
|
* @param title the languageString with the title in a particular language
|
254
|
* @see #getTitle()
|
255
|
* @see #putTitle(Language String)
|
256
|
*/
|
257
|
public void putTitle(LanguageString title){
|
258
|
this.title.put(title.getLanguage(), title);
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Creates a {@link LanguageString language string} based on the given text string
|
263
|
* and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
|
264
|
* used to be the title of <i>this</i> media.
|
265
|
*
|
266
|
* @param language the language in which the title string is formulated
|
267
|
* @param text the title in a particular language
|
268
|
*
|
269
|
* @see #getTitle()
|
270
|
* @see #putTitle(LanguageString)
|
271
|
*/
|
272
|
public void putTitle(Language language, String title){
|
273
|
this.title.put(language, LanguageString.NewInstance(title, language));
|
274
|
}
|
275
|
|
276
|
public void removeTitle(Language language){
|
277
|
this.title.remove(language);
|
278
|
}
|
279
|
|
280
|
|
281
|
@Transient
|
282
|
public String getTitleCacheByLanguage(Language lang){
|
283
|
if (cacheStrategy != null){
|
284
|
return ((MediaDefaultCacheStrategy)cacheStrategy).getTitleCacheByLanguage(this, lang);
|
285
|
}else{
|
286
|
return null;
|
287
|
}
|
288
|
|
289
|
}
|
290
|
|
291
|
|
292
|
/**
|
293
|
* Puts the title into the title field which is a multi-language string
|
294
|
* with default language as language
|
295
|
*/
|
296
|
@Override
|
297
|
public void setTitleCache(String titleCache) {
|
298
|
putTitle(LanguageString.NewInstance(titleCache, Language.DEFAULT()));
|
299
|
}
|
300
|
|
301
|
@Override
|
302
|
public String getTitleCache(){
|
303
|
if (protectedTitleCache){
|
304
|
return this.titleCache;
|
305
|
}
|
306
|
// is title dirty, i.e. equal NULL?
|
307
|
if (titleCache == null){
|
308
|
this.titleCache = generateTitle();
|
309
|
this.titleCache = getTruncatedCache(this.titleCache) ;
|
310
|
}else{
|
311
|
//do the same as listeners on dependend objects like representations parts
|
312
|
//are not yet installed
|
313
|
this.titleCache = generateTitle();
|
314
|
this.titleCache = getTruncatedCache(this.titleCache) ;
|
315
|
}
|
316
|
return titleCache;
|
317
|
}
|
318
|
|
319
|
|
320
|
|
321
|
|
322
|
public DateTime getMediaCreated(){
|
323
|
return this.mediaCreated;
|
324
|
}
|
325
|
|
326
|
public void setMediaCreated(DateTime mediaCreated){
|
327
|
this.mediaCreated = mediaCreated;
|
328
|
}
|
329
|
|
330
|
//************* Descriptions
|
331
|
|
332
|
public Map<Language,LanguageString> getAllDescriptions(){
|
333
|
if(this.description == null) {
|
334
|
this.description = new HashMap<>();
|
335
|
}
|
336
|
return this.description;
|
337
|
}
|
338
|
|
339
|
public LanguageString getDescription(Language language){
|
340
|
return getAllDescriptions().get(language);
|
341
|
}
|
342
|
|
343
|
public void addDescription(LanguageString description){
|
344
|
this.description.put(description.getLanguage(), description);
|
345
|
}
|
346
|
|
347
|
public void putDescription(Language language, String text){
|
348
|
this.description.put(language, LanguageString.NewInstance(text, language));
|
349
|
}
|
350
|
|
351
|
public void removeDescription(Language language){
|
352
|
this.description.remove(language);
|
353
|
}
|
354
|
|
355
|
//************************* CLONE **************************/
|
356
|
|
357
|
|
358
|
/* (non-Javadoc)
|
359
|
* @see java.lang.Object#clone()
|
360
|
*/
|
361
|
@Override
|
362
|
public Object clone() throws CloneNotSupportedException{
|
363
|
Media result = (Media)super.clone();
|
364
|
//description
|
365
|
result.description = cloneLanguageString(this.description);
|
366
|
|
367
|
//title
|
368
|
result.title = cloneLanguageString(this.title);
|
369
|
|
370
|
//media representations
|
371
|
result.representations = new HashSet<>();
|
372
|
for (MediaRepresentation mediaRepresentation: this.representations){
|
373
|
result.representations.add((MediaRepresentation)mediaRepresentation.clone());
|
374
|
}
|
375
|
//no changes to: artist
|
376
|
return result;
|
377
|
}
|
378
|
|
379
|
public int compareTo(Object o) {
|
380
|
return 0;
|
381
|
}
|
382
|
|
383
|
|
384
|
|
385
|
}
|