remove model changing validations fix Delete_Orphan #3379
[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.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;
33
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;
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 * @created 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)
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 //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>();
95
96
97
98 /**
99 * Factory method
100 * @return
101 */
102 public static MediaRepresentation NewInstance(){
103 logger.debug("NewInstance");
104 return new MediaRepresentation();
105 }
106
107 /**
108 * Factory method which sets the mime type and the suffix
109 * @return
110 */
111 public static MediaRepresentation NewInstance(String mimeType, String suffix){
112 MediaRepresentation result = new MediaRepresentation();
113 result.setMimeType(mimeType);
114 result.setSuffix(suffix);
115 return result;
116 }
117
118 /**
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
122 * @return
123 * @throws IllegalAccessException
124 * @throws InstantiationException
125 */
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())){
128 return null;
129 }
130 MediaRepresentationPart part;
131 if (clazz != null){
132 try {
133 Constructor<T> constr = clazz.getDeclaredConstructor();
134 constr.setAccessible(true);
135 part = constr.newInstance();
136 } catch (Exception e) {
137 throw new RuntimeException(e);
138 }
139 part.setUri(uri);
140 part.setSize(size);
141 }else{
142 part = MediaRepresentationPart.NewInstance(uri, size);
143 }
144 MediaRepresentation result = new MediaRepresentation();
145 result.setMimeType(mimeType);
146 result.setSuffix(suffix);
147 result.addRepresentationPart(part);
148 return result;
149 }
150
151
152 protected MediaRepresentation(){
153 super();
154 }
155
156 /*************** getter /setter *************************************/
157
158
159 public String getMimeType(){
160 return this.mimeType;
161 }
162
163 /**
164 *
165 * @param mimeType mimeType
166 */
167 public void setMimeType(String mimeType){
168 this.mimeType = mimeType;
169 }
170
171
172 public String getSuffix(){
173 return this.suffix;
174 }
175
176 /**
177 *
178 * @param mimeType mimeType
179 */
180 public void setSuffix(String suffix){
181 this.suffix = suffix;
182 }
183
184 public Media getMedia() {
185 return media;
186 }
187
188 /**
189 * @deprecated for internal (bidirectional) use only
190 * @param media
191 */
192 @Deprecated
193 protected void setMedia(Media media) {
194 this.media = media;
195 }
196
197
198 public List<MediaRepresentationPart> getParts(){
199 return this.mediaRepresentationParts;
200 }
201
202 @SuppressWarnings("deprecation")
203 public void addRepresentationPart(MediaRepresentationPart mediaRepresentationPart){
204 if (mediaRepresentationPart != null){
205 this.getParts().add(mediaRepresentationPart);
206 mediaRepresentationPart.setMediaRepresentation(this);
207 }
208 }
209 @SuppressWarnings("deprecation")
210 public void removeRepresentationPart(MediaRepresentationPart representationPart){
211 this.getParts().remove(representationPart);
212 if (representationPart != null){
213 representationPart.setMediaRepresentation(null);
214 }
215 }
216
217 //************************* CLONE **************************/
218 /* (non-Javadoc)
219 * @see java.lang.Object#clone()
220 */
221 @Override
222 public Object clone() throws CloneNotSupportedException{
223 MediaRepresentation result = (MediaRepresentation)super.clone();
224
225 //media representations
226 result.mediaRepresentationParts = new ArrayList<MediaRepresentationPart>();
227 for (MediaRepresentationPart mediaRepresentationPart: this.mediaRepresentationParts){
228 result.mediaRepresentationParts.add((MediaRepresentationPart)mediaRepresentationPart.clone());
229 }
230 //media
231 //this.getMedia().addRepresentation(result);
232 this.setMedia(null);
233
234 //no changes to: mimeType, suffix
235 return result;
236 }
237
238
239
240 }