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
.model
.media
;
13 import java
.lang
.reflect
.Constructor
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import javax
.persistence
.Entity
;
19 import javax
.persistence
.FetchType
;
20 import javax
.persistence
.Inheritance
;
21 import javax
.persistence
.InheritanceType
;
22 import javax
.persistence
.JoinColumn
;
23 import javax
.persistence
.ManyToOne
;
24 import javax
.persistence
.OneToMany
;
25 import javax
.persistence
.OrderColumn
;
26 import javax
.xml
.bind
.annotation
.XmlAccessType
;
27 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
28 import javax
.xml
.bind
.annotation
.XmlElement
;
29 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
30 import javax
.xml
.bind
.annotation
.XmlElements
;
31 import javax
.xml
.bind
.annotation
.XmlIDREF
;
32 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
33 import javax
.xml
.bind
.annotation
.XmlType
;
35 import org
.apache
.log4j
.Logger
;
36 import org
.hibernate
.annotations
.Cascade
;
37 import org
.hibernate
.annotations
.CascadeType
;
38 import org
.hibernate
.envers
.Audited
;
40 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
41 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
44 * A media representation is basically anything having a <a
45 * href="http://iana.org/assignments/media-types/">MIME Media Type</a>. A media
46 * representation consists of one or more parts. Each of them having the same
47 * MIME Type, file suffix (if existing) and quality (more or less).
48 * E.g. a list of jpg files that represent a scanned article of multiple pages.
52 * @since 08-Nov-2007 13:06:34
54 @XmlAccessorType(XmlAccessType
.FIELD
)
55 @XmlType(name
= "MediaRepresentation", propOrder
= {
59 "mediaRepresentationParts"
63 @Inheritance(strategy
=InheritanceType
.SINGLE_TABLE
)
64 public class MediaRepresentation
extends VersionableEntity
implements Cloneable
{
65 private static final long serialVersionUID
= -1520078266008619806L;
66 private static final Logger logger
= Logger
.getLogger(MediaRepresentation
.class);
68 //http://www.iana.org/assignments/media-types
69 @XmlElement(name
= "MimeType")
70 private String mimeType
;
72 //the file suffix (e.g. jpg, tif, mov)
73 @XmlElement(name
= "Suffix")
74 private String suffix
;
76 @XmlElement(name
= "Media")
78 @XmlSchemaType(name
= "IDREF")
79 @ManyToOne(fetch
= FetchType
.LAZY
)
80 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
})
83 @XmlElementWrapper(name
= "MediaRepresentationParts")
85 @XmlElement(name
= "AudioFile", namespace
= "http://etaxonomy.eu/cdm/model/media/1.0", type
= AudioFile
.class),
86 @XmlElement(name
= "ImageFile", namespace
= "http://etaxonomy.eu/cdm/model/media/1.0", type
= ImageFile
.class),
87 @XmlElement(name
= "MovieFile", namespace
= "http://etaxonomy.eu/cdm/model/media/1.0", type
= MovieFile
.class)
89 @OneToMany (fetch
= FetchType
.LAZY
, orphanRemoval
=true)
90 @OrderColumn(name
="sortIndex")
91 @JoinColumn (name
= "representation_id", nullable
=false)
92 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
, CascadeType
.REFRESH
})
93 private List
<MediaRepresentationPart
> mediaRepresentationParts
= new ArrayList
<>();
101 public static MediaRepresentation
NewInstance(){
102 logger
.debug("NewInstance");
103 return new MediaRepresentation();
107 * Factory method which sets the mime type and the suffix
110 public static MediaRepresentation
NewInstance(String mimeType
, String suffix
){
111 MediaRepresentation result
= new MediaRepresentation();
112 result
.setMimeType(mimeType
);
113 result
.setSuffix(suffix
);
118 * Factory method which creates a new media representation and adds a media representation part
119 * for the {@link URI uri} and the given size.
120 * Returns <code>null</code> if uri is empty
122 * @throws IllegalAccessException
123 * @throws InstantiationException
125 public static<T
extends MediaRepresentationPart
> MediaRepresentation
NewInstance(String mimeType
, String suffix
, URI uri
, Integer size
, Class
<T
> clazz
) {
126 if (uri
== null || CdmUtils
.isEmpty(uri
.toString())){
129 MediaRepresentationPart part
;
132 Constructor
<T
> constr
= clazz
.getDeclaredConstructor();
133 constr
.setAccessible(true);
134 part
= constr
.newInstance();
135 } catch (Exception e
) {
136 throw new RuntimeException(e
);
141 part
= MediaRepresentationPart
.NewInstance(uri
, size
);
143 MediaRepresentation result
= new MediaRepresentation();
144 result
.setMimeType(mimeType
);
145 result
.setSuffix(suffix
);
146 result
.addRepresentationPart(part
);
151 protected MediaRepresentation(){
155 /*************** getter /setter *************************************/
158 public String
getMimeType(){
159 return this.mimeType
;
164 * @param mimeType mimeType
166 public void setMimeType(String mimeType
){
167 this.mimeType
= mimeType
;
171 public String
getSuffix(){
177 * @param mimeType mimeType
179 public void setSuffix(String suffix
){
180 this.suffix
= suffix
;
183 public Media
getMedia() {
188 * @deprecated for internal (bidirectional) use only
192 protected void setMedia(Media media
) {
197 public List
<MediaRepresentationPart
> getParts(){
198 return this.mediaRepresentationParts
;
201 @SuppressWarnings("deprecation")
202 public void addRepresentationPart(MediaRepresentationPart mediaRepresentationPart
){
203 if (mediaRepresentationPart
!= null){
204 this.getParts().add(mediaRepresentationPart
);
205 mediaRepresentationPart
.setMediaRepresentation(this);
208 @SuppressWarnings("deprecation")
209 public void removeRepresentationPart(MediaRepresentationPart representationPart
){
210 this.getParts().remove(representationPart
);
211 if (representationPart
!= null){
212 representationPart
.setMediaRepresentation(null);
216 //************************* CLONE **************************/
218 * @see java.lang.Object#clone()
221 public Object
clone() throws CloneNotSupportedException
{
222 MediaRepresentation result
= (MediaRepresentation
)super.clone();
224 //media representations
225 result
.mediaRepresentationParts
= new ArrayList
<MediaRepresentationPart
>();
226 for (MediaRepresentationPart mediaRepresentationPart
: this.mediaRepresentationParts
){
227 result
.mediaRepresentationParts
.add((MediaRepresentationPart
)mediaRepresentationPart
.clone());
230 //this.getMedia().addRepresentation(result);
233 //no changes to: mimeType, suffix