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
.xml
.bind
.annotation
.XmlAccessType
;
26 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
27 import javax
.xml
.bind
.annotation
.XmlElement
;
28 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
29 import javax
.xml
.bind
.annotation
.XmlElements
;
30 import javax
.xml
.bind
.annotation
.XmlIDREF
;
31 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
32 import javax
.xml
.bind
.annotation
.XmlType
;
34 import org
.apache
.log4j
.Logger
;
35 import org
.hibernate
.annotations
.Cascade
;
36 import org
.hibernate
.annotations
.CascadeType
;
37 import org
.hibernate
.annotations
.IndexColumn
;
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 * @created 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
)
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 //TODO why do we have cascade defined here and below
90 @OneToMany (cascade
= {javax
.persistence
.CascadeType
.ALL
}, fetch
= FetchType
.LAZY
, orphanRemoval
=true)
91 @IndexColumn(name
="sortIndex", base
= 0)
92 @JoinColumn (name
= "representation_id", nullable
=false)
93 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
, CascadeType
.REFRESH
})
94 private List
<MediaRepresentationPart
> mediaRepresentationParts
= new ArrayList
<MediaRepresentationPart
>();
102 public static MediaRepresentation
NewInstance(){
103 logger
.debug("NewInstance");
104 return new MediaRepresentation();
108 * Factory method which sets the mime type and the suffix
111 public static MediaRepresentation
NewInstance(String mimeType
, String suffix
){
112 MediaRepresentation result
= new MediaRepresentation();
113 result
.setMimeType(mimeType
);
114 result
.setSuffix(suffix
);
119 * Factory method which creates a new media representation and adds a media representation part
120 * for the {@link URI uri} and the given size.
121 * Returns <code>null</code> if uri is empty
123 * @throws IllegalAccessException
124 * @throws InstantiationException
126 public static<T
extends MediaRepresentationPart
> MediaRepresentation
NewInstance(String mimeType
, String suffix
, URI uri
, Integer size
, Class
<T
> clazz
) {
127 if (uri
== null || CdmUtils
.isEmpty(uri
.toString())){
130 MediaRepresentationPart part
;
133 Constructor
<T
> constr
= clazz
.getDeclaredConstructor();
134 constr
.setAccessible(true);
135 part
= constr
.newInstance();
136 } catch (Exception e
) {
137 throw new RuntimeException(e
);
142 part
= MediaRepresentationPart
.NewInstance(uri
, size
);
144 MediaRepresentation result
= new MediaRepresentation();
145 result
.setMimeType(mimeType
);
146 result
.setSuffix(suffix
);
147 result
.addRepresentationPart(part
);
152 protected MediaRepresentation(){
156 /*************** getter /setter *************************************/
159 public String
getMimeType(){
160 return this.mimeType
;
165 * @param mimeType mimeType
167 public void setMimeType(String mimeType
){
168 this.mimeType
= mimeType
;
172 public String
getSuffix(){
178 * @param mimeType mimeType
180 public void setSuffix(String suffix
){
181 this.suffix
= suffix
;
184 public Media
getMedia() {
189 * @deprecated for internal (bidirectional) use only
193 protected void setMedia(Media media
) {
198 public List
<MediaRepresentationPart
> getParts(){
199 return this.mediaRepresentationParts
;
202 @SuppressWarnings("deprecation")
203 public void addRepresentationPart(MediaRepresentationPart mediaRepresentationPart
){
204 if (mediaRepresentationPart
!= null){
205 this.getParts().add(mediaRepresentationPart
);
206 mediaRepresentationPart
.setMediaRepresentation(this);
209 @SuppressWarnings("deprecation")
210 public void removeRepresentationPart(MediaRepresentationPart representationPart
){
211 this.getParts().remove(representationPart
);
212 if (representationPart
!= null){
213 representationPart
.setMediaRepresentation(null);
217 //************************* CLONE **************************/
219 * @see java.lang.Object#clone()
222 public Object
clone() throws CloneNotSupportedException
{
223 MediaRepresentation result
= (MediaRepresentation
)super.clone();
225 //media representations
226 result
.mediaRepresentationParts
= new ArrayList
<MediaRepresentationPart
>();
227 for (MediaRepresentationPart mediaRepresentationPart
: this.mediaRepresentationParts
){
228 result
.mediaRepresentationParts
.add((MediaRepresentationPart
)mediaRepresentationPart
.clone());
231 //this.getMedia().addRepresentation(result);
234 //no changes to: mimeType, suffix