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.message.broker.api.series;
23  
24  import org.opencastproject.message.broker.api.MessageItem;
25  import org.opencastproject.metadata.dublincore.DublinCore;
26  import org.opencastproject.metadata.dublincore.DublinCoreCatalog;
27  import org.opencastproject.metadata.dublincore.DublinCoreXmlFormat;
28  import org.opencastproject.security.api.AccessControlList;
29  import org.opencastproject.security.api.AccessControlParser;
30  
31  import java.io.IOException;
32  import java.io.Serializable;
33  
34  /**
35   * {@link Serializable} class that represents all of the possible messages sent through a SeriesService queue.
36   */
37  public final class SeriesItem implements MessageItem, Serializable {
38  
39    private static final long serialVersionUID = 3275142857854793612L;
40  
41    private final Type type;
42    private final String seriesId;
43    private final String series;
44    private final String acl;
45    private final String propertyName;
46    private final String propertyValue;
47    private final String element;
48    private final String elementType;
49    private final String overrideEpisodeAcl;
50  
51    public enum Type {
52      UpdateCatalog, UpdateElement, UpdateAcl, Delete
53    };
54  
55    /**
56     * @param series
57     *          The series to update.
58     * @return Builds {@link SeriesItem} for updating a series.
59     */
60    public static SeriesItem updateCatalog(DublinCoreCatalog series) {
61      return new SeriesItem(Type.UpdateCatalog, null, series, null, null, null, null, null, null);
62    }
63  
64    /**
65     * @param seriesId
66     *          The unique id for the series to update.
67     * @param type
68     *          The type of series element.
69     * @param data
70     *          The series element data.
71     * @return Builds {@link SeriesItem} for updating series element.
72     */
73    public static SeriesItem updateElement(String seriesId, String type, String data) {
74      return new SeriesItem(Type.UpdateElement, seriesId, null, null, null, null, type, data, null);
75    }
76  
77    /**
78     * @param seriesId
79     *          The unique id for the series to update.
80     * @param acl
81     *          The new access control list to update to.
82     * @param overrideEpisodeAcl
83     *          Whether to override the episode ACL.
84     * @return Builds {@link SeriesItem} for updating the access control list of a series.
85     */
86    public static SeriesItem updateAcl(String seriesId, AccessControlList acl, boolean overrideEpisodeAcl) {
87      return new SeriesItem(Type.UpdateAcl, seriesId, null, AccessControlParser.toJsonSilent(acl), null, null, null,
88              null, overrideEpisodeAcl);
89    }
90  
91    /**
92     * @param seriesId
93     *          The unique id of the series to delete.
94     * @return Builds {@link SeriesItem} for deleting a series.
95     */
96    public static SeriesItem delete(String seriesId) {
97      return new SeriesItem(Type.Delete, seriesId, null, null, null, null, null, null, null);
98    }
99  
100   /**
101    * Constructor to build an {@link SeriesItem} with given parameters.
102    *
103    * @param type
104    *          The update type.
105    * @param seriesId
106    *          The series ID to update. If you provide both, the seriesId and the series DublinCore catalog,
107    *          the seriesId must match the value of {@link DublinCore#PROPERTY_IDENTIFIER}.
108    * @param series
109    *          The series DublinCore catalog to update. The value of {@link DublinCore#PROPERTY_IDENTIFIER} must match
110    *          the value of seriesId if you provide both.
111    * @param acl
112    *          The series ACL to update. Note: the series ID must be also provided.
113    * @param propertyName
114    *          The name of the series property to update. Note: the series ID and property value must be also provided.
115    * @param propertyValue
116    *          The value of the series property to update. Note: the series ID and property name must be also provided.
117    * @param elementType
118    *          The type of the series element to update. Note: the series ID and element must be also provided.
119    * @param element
120    *          The series element to update. Note: the series ID and element type must be also provided.
121    *
122    * @throws IllegalStateException
123    *          If the series ID and the series are not provided or the series ID and the value of
124    *          {@link DublinCore#PROPERTY_IDENTIFIER} in the series catalog does not match.
125    */
126   private SeriesItem(Type type, String seriesId, DublinCoreCatalog series, String acl, String propertyName,
127           String propertyValue, String elementType, String element, Boolean overrideEpisodeAcl) {
128     if (seriesId != null && series != null && !seriesId.equals(series.getFirst(DublinCore.PROPERTY_IDENTIFIER))) {
129       throw new IllegalStateException("Provided series ID and dublincore series ID does not match");
130     }
131 
132     this.type = type;
133     if (series != null) {
134       try {
135         this.series = series.toXmlString();
136       } catch (IOException e) {
137         throw new IllegalStateException(e);
138       }
139     } else {
140       this.series = null;
141     }
142     if (seriesId != null) {
143       this.seriesId = seriesId;
144     } else if (series != null) {
145       this.seriesId = series.getFirst(DublinCore.PROPERTY_IDENTIFIER);
146     } else {
147       throw new IllegalStateException("Neither series nor series ID is provided");
148     }
149     this.acl = acl;
150     this.propertyName = propertyName;
151     this.propertyValue = propertyValue;
152     this.elementType = elementType;
153     this.element = element;
154     this.overrideEpisodeAcl = overrideEpisodeAcl == null ? null : overrideEpisodeAcl.toString();
155   }
156 
157   @Override
158   public String getId() {
159     return seriesId;
160   }
161 
162   public Type getType() {
163     return type;
164   }
165 
166   public String getSeriesId() {
167     return seriesId;
168   }
169 
170   public DublinCoreCatalog getMetadata() {
171     return DublinCoreXmlFormat.readOpt(series).orNull();
172   }
173 
174   public DublinCoreCatalog getExtendedMetadata() {
175     try {
176       return DublinCoreXmlFormat.read(element);
177     } catch (Exception ex) {
178       return null;
179     }
180   }
181 
182   public AccessControlList getAcl() {
183     try {
184       return acl == null ? null : AccessControlParser.parseAcl(acl);
185     } catch (Exception e) {
186       throw new IllegalStateException();
187     }
188   }
189 
190   public String getPropertyName() {
191     return propertyName;
192   }
193 
194   public String getPropertyValue() {
195     return propertyValue;
196   }
197 
198 
199   public String getElement() {
200     return element;
201   }
202 
203   public String getElementType() {
204     return elementType;
205   }
206 
207   public Boolean getOverrideEpisodeAcl() {
208     return overrideEpisodeAcl == null ? null : Boolean.parseBoolean(overrideEpisodeAcl);
209   }
210 }