View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.mediapackage;
23  
24  import org.opencastproject.util.MimeType;
25  
26  import java.net.URI;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.xml.bind.annotation.XmlAccessType;
31  import javax.xml.bind.annotation.XmlAccessorType;
32  import javax.xml.bind.annotation.XmlAttribute;
33  import javax.xml.bind.annotation.XmlElement;
34  import javax.xml.bind.annotation.XmlElementWrapper;
35  import javax.xml.bind.annotation.XmlRootElement;
36  import javax.xml.bind.annotation.XmlType;
37  import javax.xml.bind.annotation.adapters.XmlAdapter;
38  
39  @XmlAccessorType(XmlAccessType.NONE)
40  @XmlType(name = "publication", namespace = "http://mediapackage.opencastproject.org")
41  @XmlRootElement(name = "publication", namespace = "http://mediapackage.opencastproject.org")
42  public class PublicationImpl extends AbstractMediaPackageElement implements Publication {
43    /** Serial version UID */
44    private static final long serialVersionUID = 11151970L;
45  
46    @XmlAttribute(name = "channel", required = true)
47    private String channel;
48  
49    @XmlElementWrapper(name = "media")
50    @XmlElement(name = "track")
51    private final List<Track> tracks = new ArrayList<Track>();
52  
53    @XmlElementWrapper(name = "attachments")
54    @XmlElement(name = "attachment")
55    private final List<Attachment> attachments = new ArrayList<Attachment>();
56  
57    @XmlElementWrapper(name = "metadata")
58    @XmlElement(name = "catalog")
59    private final List<Catalog> catalogs = new ArrayList<Catalog>();
60  
61    /** JAXB constructor */
62    public PublicationImpl() {
63      this.elementType = Type.Publication;
64    }
65  
66    public PublicationImpl(String id, String channel, URI uri, MimeType mimeType) {
67      this();
68      setURI(uri);
69      setIdentifier(id);
70      setMimeType(mimeType);
71      this.channel = channel;
72    }
73  
74    public static Publication publication(String id, String channel, URI uri, MimeType mimeType) {
75      return new PublicationImpl(id, channel, uri, mimeType);
76    }
77  
78    @Override
79    public String getChannel() {
80      return channel;
81    }
82  
83    @Override
84    public Track[] getTracks() {
85      return tracks.toArray(new Track[tracks.size()]);
86    }
87  
88    @Override
89    public void addTrack(Track track) {
90      // Check (uniqueness of) track identifier
91      String id = track.getIdentifier();
92      if (id == null) {
93        track.generateIdentifier();
94      }
95      tracks.add(track);
96    }
97  
98    @Override
99    public Attachment[] getAttachments() {
100     return attachments.toArray(new Attachment[attachments.size()]);
101   }
102 
103   @Override
104   public void addAttachment(Attachment attachment) {
105     // Check (uniqueness of) attachment identifier
106     String id = attachment.getIdentifier();
107     if (id == null) {
108       attachment.generateIdentifier();
109     }
110     attachments.add(attachment);
111   }
112 
113   @Override
114   public void removeAttachmentById(String attachmentId) {
115     attachments.removeIf(a -> a.getIdentifier().equals(attachmentId));
116   }
117 
118   @Override
119   public Catalog[] getCatalogs() {
120     return catalogs.toArray(new Catalog[catalogs.size()]);
121   }
122 
123   @Override
124   public void addCatalog(Catalog catalog) {
125     // Check (uniqueness of) catalog identifier
126     String id = catalog.getIdentifier();
127     if (id == null) {
128       catalog.generateIdentifier();
129     }
130     catalogs.add(catalog);
131   }
132 
133   @Override
134   public void clearTracks() {
135     tracks.clear();
136   }
137 
138   @Override
139   public void setFlavor(MediaPackageElementFlavor flavor) {
140     throw new UnsupportedOperationException("Unable to set the flavor of publications.");
141   }
142 
143   /**
144    * Adds a {@link MediaPackageElement} to this publication by determining its type.
145    *
146    * @param publication
147    *          The {@link Publication} to add the {@link MediaPackageElement} to.
148    * @param element
149    *          The {@link MediaPackageElement} to add. If it is not a {@link Attachment}, {@link Catalog} or
150    *          {@link Track} it will not be added to the {@link Publication}.
151    */
152   public static void addElementToPublication(Publication publication, MediaPackageElement element) {
153     if (MediaPackageElement.Type.Track.equals(element.getElementType())) {
154       publication.addTrack((Track) element);
155     } else if (MediaPackageElement.Type.Catalog.equals(element.getElementType())) {
156       publication.addCatalog((Catalog) element);
157     } else if (MediaPackageElement.Type.Attachment.equals(element.getElementType())) {
158       publication.addAttachment((Attachment) element);
159     }
160   }
161 
162   /** JAXB adapter */
163   public static class Adapter extends XmlAdapter<PublicationImpl, Publication> {
164     @Override
165     public PublicationImpl marshal(Publication e) throws Exception {
166       return (PublicationImpl) e;
167     }
168 
169     @Override
170     public Publication unmarshal(PublicationImpl e) throws Exception {
171       return e;
172     }
173   }
174 }