| 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.lang.reflect.InvocationTargetException; |
|---|
| 15 | import java.net.URI; |
|---|
| 16 | import java.util.ArrayList; |
|---|
| 17 | import java.util.List; |
|---|
| 18 | |
|---|
| 19 | import javax.persistence.Entity; |
|---|
| 20 | import javax.persistence.FetchType; |
|---|
| 21 | import javax.persistence.Inheritance; |
|---|
| 22 | import javax.persistence.InheritanceType; |
|---|
| 23 | import javax.persistence.JoinColumn; |
|---|
| 24 | import javax.persistence.ManyToOne; |
|---|
| 25 | import javax.persistence.OneToMany; |
|---|
| 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.annotations.IndexColumn; |
|---|
| 39 | import org.hibernate.envers.Audited; |
|---|
| 40 | |
|---|
| 41 | import eu.etaxonomy.cdm.common.CdmUtils; |
|---|
| 42 | import eu.etaxonomy.cdm.model.common.VersionableEntity; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * A media representation is basically anything having a <a |
|---|
| 46 | * href="http://iana.org/assignments/media-types/">MIME Media Type</a>. A media |
|---|
| 47 | * representation consists of one or more parts. Each of them having the same |
|---|
| 48 | * MIME Type, file suffix (if existing) and quality (more or less). |
|---|
| 49 | * E.g. a list of jpg files that represent a scanned article of multiple pages. |
|---|
| 50 | * |
|---|
| 51 | * @author m.doering |
|---|
| 52 | * @version 1.0 |
|---|
| 53 | * @created 08-Nov-2007 13:06:34 |
|---|
| 54 | */ |
|---|
| 55 | @XmlAccessorType(XmlAccessType.FIELD) |
|---|
| 56 | @XmlType(name = "MediaRepresentation", propOrder = { |
|---|
| 57 | "mimeType", |
|---|
| 58 | "suffix", |
|---|
| 59 | "media", |
|---|
| 60 | "mediaRepresentationParts" |
|---|
| 61 | }) |
|---|
| 62 | @Entity |
|---|
| 63 | @Audited |
|---|
| 64 | @Inheritance(strategy=InheritanceType.SINGLE_TABLE) |
|---|
| 65 | public class MediaRepresentation extends VersionableEntity implements Cloneable{ |
|---|
| 66 | private static final long serialVersionUID = -1520078266008619806L; |
|---|
| 67 | private static final Logger logger = Logger.getLogger(MediaRepresentation.class); |
|---|
| 68 | |
|---|
| 69 | //http://www.iana.org/assignments/media-types |
|---|
| 70 | @XmlElement(name = "MimeType") |
|---|
| 71 | private String mimeType; |
|---|
| 72 | |
|---|
| 73 | //the file suffix (e.g. jpg, tif, mov) |
|---|
| 74 | @XmlElement(name = "Suffix") |
|---|
| 75 | private String suffix; |
|---|
| 76 | |
|---|
| 77 | @XmlElement(name = "Media") |
|---|
| 78 | @XmlIDREF |
|---|
| 79 | @XmlSchemaType(name = "IDREF") |
|---|
| 80 | @ManyToOne(fetch = FetchType.LAZY) |
|---|
| 81 | @Cascade(CascadeType.SAVE_UPDATE) |
|---|
| 82 | private Media media; |
|---|
| 83 | |
|---|
| 84 | @XmlElementWrapper(name = "MediaRepresentationParts") |
|---|
| 85 | @XmlElements({ |
|---|
| 86 | @XmlElement(name = "AudioFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = AudioFile.class), |
|---|
| 87 | @XmlElement(name = "ImageFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = ImageFile.class), |
|---|
| 88 | @XmlElement(name = "MovieFile", namespace = "http://etaxonomy.eu/cdm/model/media/1.0", type = MovieFile.class) |
|---|
| 89 | }) |
|---|
| 90 | @OneToMany (cascade = {javax.persistence.CascadeType.ALL}, fetch= FetchType.LAZY) |
|---|
| 91 | @IndexColumn(name="sortIndex", base = 0) |
|---|
| 92 | @JoinColumn (name = "representation_id", nullable=false) |
|---|
| 93 | @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN, 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 | } |
|---|