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  package org.opencastproject.oaipmh.persistence;
22  
23  import org.opencastproject.mediapackage.Attachment;
24  import org.opencastproject.mediapackage.Catalog;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  
32  import javax.persistence.CascadeType;
33  import javax.persistence.Column;
34  import javax.persistence.Entity;
35  import javax.persistence.FetchType;
36  import javax.persistence.Id;
37  import javax.persistence.IdClass;
38  import javax.persistence.JoinColumn;
39  import javax.persistence.JoinColumns;
40  import javax.persistence.Lob;
41  import javax.persistence.NamedQueries;
42  import javax.persistence.NamedQuery;
43  import javax.persistence.OneToMany;
44  import javax.persistence.Table;
45  import javax.persistence.Temporal;
46  import javax.persistence.TemporalType;
47  import javax.persistence.UniqueConstraint;
48  
49  @Entity(name = "OaiPmhEntity")
50  @IdClass(OaiPmhEntityId.class)
51  @Table(name = "oc_oaipmh", uniqueConstraints = @UniqueConstraint(columnNames = { "modification_date" }))
52  @NamedQueries({ @NamedQuery(name = "OaiPmh.findById",
53          query = "SELECT o FROM OaiPmhEntity o "
54                  + "WHERE o.mediaPackageId=:mediaPackageId"
55                  + " AND o.repositoryId=:repository"
56                  + " AND o.organization=:organization") })
57  public class OaiPmhEntity {
58  
59    /** media package id, primary key */
60    @Id
61    @Column(name = "mp_id", length = 64)
62    private String mediaPackageId;
63  
64    /** Organization id */
65    @Id
66    @Column(name = "organization", length = 96)
67    protected String organization;
68  
69    /** Repository id */
70    @Id
71    @Column(name = "repo_id", length = 12)
72    private String repositoryId;
73  
74    /** Series id */
75    @Column(name = "series_id",length = 128)
76    private String series;
77  
78    /** Flag indicating deletion. */
79    @Column(name = "deleted")
80    private boolean deleted = false;
81  
82    /** The last modification date */
83    @Column(
84        name = "modification_date")
85    @Temporal(TemporalType.TIMESTAMP)
86    private Date modificationDate;
87  
88    /** Serialized media package */
89    @Lob
90    @Column(name = "mediapackage_xml", length = 65535, nullable = false)
91    private String mediaPackageXML;
92  
93    /** List of serialized media package element entities */
94    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
95    @JoinColumns({
96      @JoinColumn(name = "mp_id", referencedColumnName = "mp_id", nullable = false, table = "oc_oaipmh_elements", insertable = false, updatable = false),
97      @JoinColumn(name = "organization", referencedColumnName = "organization", nullable = false, table = "oc_oaipmh_elements", insertable = false, updatable = false),
98      @JoinColumn(name = "repo_id", referencedColumnName = "repo_id", nullable = false, table = "oc_oaipmh_elements", insertable = false, updatable = false)
99    })
100   private List<OaiPmhElementEntity> mediaPackageElements = new ArrayList<>();
101 
102   /**
103    * Default constructor without any import.
104    */
105   public OaiPmhEntity() {
106     this.modificationDate = new Date();
107   }
108 
109   /**
110    * Returns media package id.
111    * 
112    * @return media package id
113    */
114   public String getMediaPackageId() {
115     return mediaPackageId;
116   }
117 
118   /**
119    * Sets media package id. Id length limit is 128 charachters.
120    * 
121    * @param mediaPackageId
122    */
123   public void setMediaPackageId(String mediaPackageId) {
124     this.mediaPackageId = mediaPackageId;
125     this.modificationDate = new Date();
126   }
127 
128   /**
129    * @return the organization
130    */
131   public String getOrganization() {
132     return organization;
133   }
134 
135   /**
136    * @param organization
137    *          the organization to set
138    */
139   public void setOrganization(String organization) {
140     this.organization = organization;
141     this.modificationDate = new Date();
142   }
143 
144   /**
145    * @return the deletion flag
146    */
147   public boolean isDeleted() {
148     return deleted;
149   }
150 
151   /**
152    * Sets the deletion flag
153    * 
154    * @param deleted
155    *          the deletion flag
156    */
157   public void setDeleted(boolean deleted) {
158     this.deleted = deleted;
159     this.modificationDate = new Date();
160   }
161 
162   /**
163    * @return the series identifier
164    */
165   public String getSeries() {
166     return series;
167   }
168 
169   /**
170    * Sets the series identifier
171    * 
172    * @param series
173    *          the series identifier
174    */
175   public void setSeries(String series) {
176     this.series = series;
177     this.modificationDate = new Date();
178   }
179 
180   /**
181    * @return the modification date
182    */
183   public Date getModificationDate() {
184     return modificationDate;
185   }
186 
187   /**
188    * Returns serialized media package.
189    * 
190    * @return serialized media package
191    */
192   public String getMediaPackageXML() {
193     return mediaPackageXML;
194   }
195 
196   /**
197    * Sets serialized media package
198    * 
199    * @param mediaPackageXML
200    */
201   public void setMediaPackageXML(String mediaPackageXML) {
202     this.mediaPackageXML = mediaPackageXML;
203     this.modificationDate = new Date();
204   }
205 
206   /**
207    * @return the repository id
208    */
209   public String getRepositoryId() {
210     return repositoryId;
211   }
212 
213   /**
214    * Sets the repository id
215    * 
216    * @param repositoryId
217    */
218   public void setRepositoryId(String repositoryId) {
219     this.repositoryId = repositoryId;
220     this.modificationDate = new Date();
221   }
222 
223   /**
224    * @return serialized media package attachment entities
225    */
226   public List<OaiPmhElementEntity> getAttachments() {
227     return getMediaPackageElementsOfType(Attachment.TYPE.name());
228   }
229 
230   /**
231    * @return serialized media package catalog entities
232    */
233   public List<OaiPmhElementEntity> getCatalogs() {
234     return getMediaPackageElementsOfType(Catalog.TYPE.name());
235   }
236 
237   /**
238    * A list of serialized media package element entities, filtered by given type
239    *
240    * @param elementType type of media package element to filter on
241    * @return serialized media package elements of given type
242    */
243   private List<OaiPmhElementEntity> getMediaPackageElementsOfType(String elementType) {
244     // as we do not expect to many media package elements per media package, we can filter them in java
245     List<OaiPmhElementEntity> filteredElements = new ArrayList<>();
246     for (OaiPmhElementEntity element : mediaPackageElements) {
247       if (StringUtils.equals(elementType, element.getElementType()))
248         filteredElements.add(element);
249     }
250     return filteredElements;
251   }
252 
253   /**
254    * @return all serialized media package element entities
255    */
256   public List<OaiPmhElementEntity> getMediaPackageElements() {
257     return mediaPackageElements;
258   }
259 
260   /**
261    * Add an serialized media package element
262    *
263    * @param mediaPackageElementEntity serialized media package element to add
264    */
265   public void addMediaPackageElement(OaiPmhElementEntity mediaPackageElementEntity) {
266     mediaPackageElements.add(mediaPackageElementEntity);
267     mediaPackageElementEntity.setOaiPmhEntity(this);
268     this.modificationDate = new Date();
269   }
270 
271   /**
272    * Remove media package element entity from the list of elements
273    *
274    * @param mediaPackageElementEntity serialized media package element entity to remove
275    */
276   public void removeMediaPackageElement(OaiPmhElementEntity mediaPackageElementEntity) {
277     mediaPackageElements.remove(mediaPackageElementEntity);
278     this.modificationDate = new Date();
279   }
280 
281   /**
282    * Clear the list of media package element entities
283    */
284   public void removeAllMediaPackageElements() {
285     mediaPackageElements.clear();
286   }
287 }