Merge branch 'release/5.0.0'
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / media / MediaRepresentation.java
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
13 import java.lang.reflect.Constructor;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.List;
17
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;
34
35 import org.apache.log4j.Logger;
36 import org.hibernate.annotations.Cascade;
37 import org.hibernate.annotations.CascadeType;
38 import org.hibernate.envers.Audited;
39
40 import eu.etaxonomy.cdm.common.CdmUtils;
41 import eu.etaxonomy.cdm.model.common.VersionableEntity;
42
43 /**
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.
49 *
50 * @author m.doering
51 * @version 1.0
52 * @since 08-Nov-2007 13:06:34
53 */
54 @XmlAccessorType(XmlAccessType.FIELD)
55 @XmlType(name = "MediaRepresentation", propOrder = {
56 "mimeType",
57 "suffix",
58 "media",
59 "mediaRepresentationParts"
60 })
61 @Entity
62 @Audited
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);
67
68 //http://www.iana.org/assignments/media-types
69 @XmlElement(name = "MimeType")
70 private String mimeType;
71
72 //the file suffix (e.g. jpg, tif, mov)
73 @XmlElement(name = "Suffix")
74 private String suffix;
75
76 @XmlElement(name = "Media")
77 @XmlIDREF
78 @XmlSchemaType(name = "IDREF")
79 @ManyToOne(fetch = FetchType.LAZY)
80 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
81 private Media media;
82
83 @XmlElementWrapper(name = "MediaRepresentationParts")
84 @XmlElements({
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)
88 })
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<>();
94
95
96
97 /**
98 * Factory method
99 * @return
100 */
101 public static MediaRepresentation NewInstance(){
102 logger.debug("NewInstance");
103 return new MediaRepresentation();
104 }
105
106 /**
107 * Factory method which sets the mime type and the suffix
108 * @return
109 */
110 public static MediaRepresentation NewInstance(String mimeType, String suffix){
111 MediaRepresentation result = new MediaRepresentation();
112 result.setMimeType(mimeType);
113 result.setSuffix(suffix);
114 return result;
115 }
116
117 /**
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
121 * @return
122 * @throws IllegalAccessException
123 * @throws InstantiationException
124 */
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())){
127 return null;
128 }
129 MediaRepresentationPart part;
130 if (clazz != null){
131 try {
132 Constructor<T> constr = clazz.getDeclaredConstructor();
133 constr.setAccessible(true);
134 part = constr.newInstance();
135 } catch (Exception e) {
136 throw new RuntimeException(e);
137 }
138 part.setUri(uri);
139 part.setSize(size);
140 }else{
141 part = MediaRepresentationPart.NewInstance(uri, size);
142 }
143 MediaRepresentation result = new MediaRepresentation();
144 result.setMimeType(mimeType);
145 result.setSuffix(suffix);
146 result.addRepresentationPart(part);
147 return result;
148 }
149
150
151 protected MediaRepresentation(){
152 super();
153 }
154
155 /*************** getter /setter *************************************/
156
157
158 public String getMimeType(){
159 return this.mimeType;
160 }
161
162 /**
163 *
164 * @param mimeType mimeType
165 */
166 public void setMimeType(String mimeType){
167 this.mimeType = mimeType;
168 }
169
170
171 public String getSuffix(){
172 return this.suffix;
173 }
174
175 /**
176 *
177 * @param mimeType mimeType
178 */
179 public void setSuffix(String suffix){
180 this.suffix = suffix;
181 }
182
183 public Media getMedia() {
184 return media;
185 }
186
187 /**
188 * @deprecated for internal (bidirectional) use only
189 * @param media
190 */
191 @Deprecated
192 protected void setMedia(Media media) {
193 this.media = media;
194 }
195
196
197 public List<MediaRepresentationPart> getParts(){
198 return this.mediaRepresentationParts;
199 }
200
201 @SuppressWarnings("deprecation")
202 public void addRepresentationPart(MediaRepresentationPart mediaRepresentationPart){
203 if (mediaRepresentationPart != null){
204 this.getParts().add(mediaRepresentationPart);
205 mediaRepresentationPart.setMediaRepresentation(this);
206 }
207 }
208 @SuppressWarnings("deprecation")
209 public void removeRepresentationPart(MediaRepresentationPart representationPart){
210 this.getParts().remove(representationPart);
211 if (representationPart != null){
212 representationPart.setMediaRepresentation(null);
213 }
214 }
215
216 //************************* CLONE **************************/
217 /* (non-Javadoc)
218 * @see java.lang.Object#clone()
219 */
220 @Override
221 public Object clone() throws CloneNotSupportedException{
222 MediaRepresentation result = (MediaRepresentation)super.clone();
223
224 //media representations
225 result.mediaRepresentationParts = new ArrayList<MediaRepresentationPart>();
226 for (MediaRepresentationPart mediaRepresentationPart: this.mediaRepresentationParts){
227 result.mediaRepresentationParts.add((MediaRepresentationPart)mediaRepresentationPart.clone());
228 }
229 //media
230 //this.getMedia().addRepresentation(result);
231 this.setMedia(null);
232
233 //no changes to: mimeType, suffix
234 return result;
235 }
236
237
238
239 }