Project

General

Profile

Download (6.68 KB) Statistics
| Branch: | Tag: | Revision:
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.util.ArrayList;
15
import java.util.List;
16

    
17
import javax.persistence.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.Inheritance;
20
import javax.persistence.InheritanceType;
21
import javax.persistence.JoinColumn;
22
import javax.persistence.ManyToOne;
23
import javax.persistence.OneToMany;
24
import javax.persistence.OrderColumn;
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.envers.Audited;
38

    
39
import eu.etaxonomy.cdm.common.URI;
40
import eu.etaxonomy.cdm.model.common.VersionableEntity;
41

    
42
/**
43
 * A media representation is basically anything having a <a
44
 * href="http://iana.org/assignments/media-types/">MIME Media Type</a>. A media
45
 * representation consists of one or more parts. Each of them having the same
46
 * MIME Type, file suffix (if existing) and quality (more or less).
47
 * E.g. a list of jpg files that represent a scanned article of multiple pages.
48
 *
49
 * @author m.doering
50
 * @since 08-Nov-2007 13:06:34
51
 */
52
@XmlAccessorType(XmlAccessType.FIELD)
53
@XmlType(name = "MediaRepresentation", propOrder = {
54
	"mimeType",
55
    "suffix",
56
    "media",
57
    "mediaRepresentationParts"
58
})
59
@Entity
60
@Audited
61
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
62
public class MediaRepresentation extends VersionableEntity {
63

    
64
    private static final long serialVersionUID = -1520078266008619806L;
65
	private static final Logger logger = Logger.getLogger(MediaRepresentation.class);
66

    
67
	//http://www.iana.org/assignments/media-types
68
	@XmlElement(name = "MimeType")
69
	private String mimeType;
70

    
71
	//the file suffix (e.g. jpg, tif, mov)
72
	@XmlElement(name = "Suffix")
73
	private String suffix;
74

    
75
	@XmlElement(name = "Media")
76
	@XmlIDREF
77
	@XmlSchemaType(name = "IDREF")
78
	@ManyToOne(fetch = FetchType.LAZY)
79
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
80
	private Media media;
81

    
82
	@XmlElementWrapper(name = "MediaRepresentationParts")
83
    @XmlElements({
84
        @XmlElement(name = "AudioFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = AudioFile.class),
85
        @XmlElement(name = "ImageFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = ImageFile.class),
86
        @XmlElement(name = "MovieFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = MovieFile.class)
87
    })
88
    @OneToMany (fetch= FetchType.LAZY, orphanRemoval=true)
89
	@OrderColumn(name="sortIndex")
90
	@JoinColumn (name = "representation_id",  nullable=false)
91
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.REFRESH})
92
	private List<MediaRepresentationPart> mediaRepresentationParts = new ArrayList<>();
93

    
94
//********************* FACTORY ***********************************************/
95

    
96
	/**
97
	 * Factory method
98
	 * @return
99
	 */
100
	public static MediaRepresentation NewInstance(){
101
		logger.debug("NewInstance");
102
		return new MediaRepresentation();
103
	}
104

    
105
	/**
106
	 * Factory method which sets the mime type and the suffix
107
	 * @return
108
	 */
109
	public static MediaRepresentation NewInstance(String mimeType, String suffix){
110
		MediaRepresentation result  = new MediaRepresentation();
111
		result.setMimeType(mimeType);
112
		result.setSuffix(suffix);
113
		return result;
114
	}
115

    
116
	/**
117
	 * Factory method which creates a new media representation and adds a media representation part
118
	 * for the {@link URI uri} and the given size.
119
	 * Returns <code>null</code> if uri is empty
120
	 * @return
121
	 * @throws IllegalAccessException
122
	 * @throws InstantiationException
123
	 */
124
	public static<T extends MediaRepresentationPart> MediaRepresentation NewInstance(String mimeType, String suffix, URI uri, Integer size, Class<T> clazz) {
125
		if (uri == null || isBlank(uri.toString())){
126
			return null;
127
		}
128
		MediaRepresentationPart part;
129
		if (clazz != null){
130
			try {
131
				Constructor<T> constr = clazz.getDeclaredConstructor();
132
				constr.setAccessible(true);
133
				part = constr.newInstance();
134
			} catch (Exception e) {
135
				throw new RuntimeException(e);
136
			}
137
			part.setUri(uri);
138
			part.setSize(size);
139
		}else{
140
			part = MediaRepresentationPart.NewInstance(uri, size);
141
		}
142
		MediaRepresentation result  = new MediaRepresentation();
143
		result.setMimeType(mimeType);
144
		result.setSuffix(suffix);
145
		result.addRepresentationPart(part);
146
		return result;
147
	}
148

    
149
// ************************ CONSTRUCTOR *********************************/
150

    
151
	protected MediaRepresentation(){
152
		super();
153
	}
154

    
155
//***************  Getter /Setter *************************************/
156

    
157

    
158
	public String getMimeType(){
159
		return this.mimeType;
160
	}
161
	public void setMimeType(String mimeType){
162
		this.mimeType = mimeType;
163
	}
164

    
165

    
166
	public String getSuffix(){
167
		return this.suffix;
168
	}
169
	public void setSuffix(String suffix){
170
		this.suffix = suffix;
171
	}
172

    
173
	public Media getMedia() {
174
		return media;
175
	}
176

    
177
	/**
178
	 * @deprecated for internal (bidirectional) use only
179
	 * @param media
180
	 */
181
	@Deprecated
182
	protected void setMedia(Media media) {
183
		this.media = media;
184
	}
185

    
186

    
187
	public List<MediaRepresentationPart> getParts(){
188
		return this.mediaRepresentationParts;
189
	}
190

    
191
	@SuppressWarnings("deprecation")
192
	public void addRepresentationPart(MediaRepresentationPart mediaRepresentationPart){
193
		if (mediaRepresentationPart != null){
194
			this.getParts().add(mediaRepresentationPart);
195
			mediaRepresentationPart.setMediaRepresentation(this);
196
		}
197
	}
198
	@SuppressWarnings("deprecation")
199
	public void removeRepresentationPart(MediaRepresentationPart representationPart){
200
		this.getParts().remove(representationPart);
201
		if (representationPart != null){
202
			representationPart.setMediaRepresentation(null);
203
		}
204
	}
205

    
206
//************************* CLONE **************************/
207

    
208
	@Override
209
	public MediaRepresentation clone() throws CloneNotSupportedException{
210
		MediaRepresentation result = (MediaRepresentation)super.clone();
211

    
212
		//media representations
213
		result.mediaRepresentationParts = new ArrayList<>();
214
		for (MediaRepresentationPart mediaRepresentationPart: this.mediaRepresentationParts){
215
			result.mediaRepresentationParts.add(mediaRepresentationPart.clone());
216
		}
217
		//media
218
		//this.getMedia().addRepresentation(result);
219
		this.setMedia(null);
220

    
221
		//no changes to: mimeType, suffix
222
		return result;
223
	}
224
}
(10-10/16)