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.series.impl.persistence;
23  
24  import java.util.Collections;
25  import java.util.Date;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import javax.persistence.Access;
30  import javax.persistence.AccessType;
31  import javax.persistence.CollectionTable;
32  import javax.persistence.Column;
33  import javax.persistence.ElementCollection;
34  import javax.persistence.Entity;
35  import javax.persistence.GeneratedValue;
36  import javax.persistence.GenerationType;
37  import javax.persistence.Id;
38  import javax.persistence.IdClass;
39  import javax.persistence.Index;
40  import javax.persistence.JoinColumn;
41  import javax.persistence.Lob;
42  import javax.persistence.MapKeyColumn;
43  import javax.persistence.NamedQueries;
44  import javax.persistence.NamedQuery;
45  import javax.persistence.Table;
46  import javax.persistence.Temporal;
47  import javax.persistence.TemporalType;
48  import javax.persistence.UniqueConstraint;
49  
50  /**
51   * Enitity object for storing series in persistence storage. Series ID is stored as primary key, DUBLIN_CORE field is
52   * used to store serialized Dublin core and ACCESS_CONTROL field is used to store information about access control
53   * rules.
54   *
55   */
56  @Entity(name = "SeriesEntity")
57  @IdClass(SeriesEntityId.class)
58  @Access(AccessType.FIELD)
59  @Table(name = "oc_series",
60      indexes = {
61          @Index(name = "IX_oc_series_modified_date", columnList = ("modified_date")),
62      }
63  )
64  @NamedQueries({
65      @NamedQuery(
66          name = "Series.findAll",
67          query = "select s from SeriesEntity s where s.deletionDate is null"
68      ),
69      @NamedQuery(
70          name = "Series.getCount",
71          query = "select COUNT(s) from SeriesEntity s where s.deletionDate is null"
72      ),
73      @NamedQuery(
74          name = "seriesById",
75          query = "select s from SeriesEntity as s where s.seriesId=:seriesId and s.organization=:organization"
76      ),
77      @NamedQuery(
78          name = "Series.getAllModifiedSince",
79          query = "select s from SeriesEntity as s "
80              + "where s.modifiedDate >= :since and s.organization=:organization "
81              + "order by s.modifiedDate asc"
82      ),
83      @NamedQuery(
84          name = "Series.getAllModifiedInRange",
85          query = "select s from SeriesEntity as s "
86              + "where s.modifiedDate >= :from and s.modifiedDate < :to and s.organization=:organization "
87              + "order by s.modifiedDate asc"
88      ),
89  })
90  public class SeriesEntity {
91  
92    /** Series ID, primary key */
93    @Id
94    @GeneratedValue(strategy = GenerationType.AUTO)
95    @Column(name = "id", length = 128)
96    protected String seriesId;
97  
98    /** Series ID, primary key */
99    @Id
100   @Column(name = "organization", length = 128)
101   protected String organization;
102 
103   /** Serialized Dublin core */
104   @Lob
105   @Column(name = "dublin_core", length = 65535)
106   protected String dublinCoreXML;
107 
108   /** Serialized access control */
109   @Lob
110   @Column(name = "access_control", length = 65535)
111   protected String accessControl;
112 
113   @Column(name = "modified_date", nullable = false)
114   @Temporal(TemporalType.TIMESTAMP)
115   protected Date modifiedDate = new Date();
116 
117   @Column(name = "deletion_date")
118   @Temporal(TemporalType.TIMESTAMP)
119   protected Date deletionDate = null;
120 
121   @Lob
122   @ElementCollection(targetClass = String.class)
123   @MapKeyColumn(name = "name", nullable = false)
124   @Column(name = "value", length = 65535)
125   @CollectionTable(name = "oc_series_property", uniqueConstraints = {
126       @UniqueConstraint(name = "UNQ_series_properties", columnNames = {"series", "organization", "name"})
127       }, joinColumns = {
128           @JoinColumn(name = "series", referencedColumnName = "id", nullable = false),
129           @JoinColumn(name = "organization", referencedColumnName = "organization", nullable = false) })
130   protected Map<String, String> properties;
131 
132   @ElementCollection
133   @MapKeyColumn(name = "type", length = 128, nullable = false)
134   @Column(name = "data")
135   @CollectionTable(name = "oc_series_elements", uniqueConstraints = {
136       @UniqueConstraint(name = "UNQ_series_elements", columnNames = {"series", "organization", "type"})
137       }, joinColumns = {
138       @JoinColumn(name = "series", referencedColumnName = "id", nullable = false),
139       @JoinColumn(name = "organization", referencedColumnName = "organization", nullable = false) })
140   protected Map<String, byte[]> elements;
141 
142   /**
143    * Default constructor without any import.
144    */
145   public SeriesEntity() {
146   }
147 
148   /**
149    * Returns series ID.
150    *
151    * @return series ID
152    */
153   public String getSeriesId() {
154     return seriesId;
155   }
156 
157   /**
158    * Sets series ID. ID length limit is 128 characters.
159    *
160    * @param seriesId
161    */
162   public void setSeriesId(String seriesId) {
163     if (seriesId == null) {
164       throw new IllegalArgumentException("Series id can't be null");
165     }
166     if (seriesId.length() > 128) {
167       throw new IllegalArgumentException("Series id can't be longer than 128 characters");
168     }
169     this.seriesId = seriesId;
170   }
171 
172   /**
173    * Returns serialized Dublin core.
174    *
175    * @return serialized Dublin core
176    */
177   public String getDublinCoreXML() {
178     return dublinCoreXML;
179   }
180 
181   /**
182    * Sets serialized Dublin core.
183    *
184    * @param dublinCoreXML
185    *          serialized Dublin core
186    */
187   public void setSeries(String dublinCoreXML) {
188     this.dublinCoreXML = dublinCoreXML;
189   }
190 
191   /**
192    * Returns serialized access control
193    *
194    * @return serialized access control
195    */
196   public String getAccessControl() {
197     return accessControl;
198   }
199 
200   /**
201    * Sets serialized access control.
202    *
203    * @param accessControl
204    *          serialized access control
205    */
206   public void setAccessControl(String accessControl) {
207     this.accessControl = accessControl;
208   }
209 
210   /**
211    * @return the organization
212    */
213   public String getOrganization() {
214     return organization;
215   }
216 
217   /**
218    * @param organization
219    *          the organization to set
220    */
221   public void setOrganization(String organization) {
222     this.organization = organization;
223   }
224 
225   public Date getModifiedDate() {
226     return this.modifiedDate;
227   }
228 
229   public void setModifiedDate(Date date) {
230     this.modifiedDate = date;
231   }
232 
233   public Date getDeletionDate() {
234     return this.deletionDate;
235   }
236 
237   public void setDeletionDate(Date date) {
238     this.deletionDate = date;
239   }
240 
241   public boolean isDeleted() {
242     return this.deletionDate != null;
243   }
244 
245   public Map<String, String> getProperties() {
246     return new TreeMap<String, String>(properties);
247   }
248 
249   public void setProperties(Map<String, String> properties) {
250     this.properties = properties;
251   }
252 
253   public Map<String, byte[]> getElements() {
254     return Collections.unmodifiableMap(elements);
255   }
256 
257   public void addElement(String type, byte[] data) {
258     elements.put(type, data);
259   }
260 
261   public void removeElement(String type) {
262     elements.remove(type);
263   }
264 }