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