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.assetmanager.impl.persistence;
22  
23  import org.opencastproject.util.MimeType;
24  
25  import java.util.Optional;
26  import java.util.function.Function;
27  
28  import javax.persistence.Column;
29  import javax.persistence.Entity;
30  import javax.persistence.EntityManager;
31  import javax.persistence.GeneratedValue;
32  import javax.persistence.GenerationType;
33  import javax.persistence.Id;
34  import javax.persistence.Index;
35  import javax.persistence.JoinColumn;
36  import javax.persistence.ManyToOne;
37  import javax.persistence.NamedQueries;
38  import javax.persistence.NamedQuery;
39  import javax.persistence.Table;
40  import javax.persistence.TableGenerator;
41  import javax.persistence.TypedQuery;
42  
43  /** JPA DTO modeling the asset database table. */
44  @Entity(name = "Asset")
45  @Table(name = "oc_assets_asset", indexes = {
46      @Index(name = "IX_oc_assets_asset_checksum", columnList = ("checksum")),
47      @Index(name = "IX_oc_assets_asset_mediapackage_element_id", columnList = ("mediapackage_element_id")) })
48  @NamedQueries({
49      @NamedQuery(
50          name = "Asset.findMediumByMpIdMpeIdAndVersion",
51          query = "SELECT a, s.availability, s.organizationId FROM Asset a "
52              + "JOIN a.snapshot s "
53              + "WHERE s.mediaPackageId = :mpId "
54              + "AND a.mediaPackageElementId = :mpeId "
55              + "AND s.version = :version "
56              + "ORDER BY s.version DESC"
57      ),
58      @NamedQuery(
59          name = "Asset.findByChecksumStorageIdAndOrganizationId",
60          query = "SELECT a FROM Asset a "
61              + "INNER JOIN a.snapshot s "
62              + "WHERE a.checksum = :checksum "
63              + "AND a.storageId = :storageId "
64              + "AND s.organizationId = :orgId "
65      ),
66      @NamedQuery(
67          name = "Asset.updateStorageIdBySnapshot",
68          query = "UPDATE Asset a SET a.storageId = :storageId "
69              + "WHERE a.snapshot = :snapshot "
70      ),
71      @NamedQuery(
72          name = "Asset.updateStorageIdBySnapshotAndMpElementId",
73          query = "UPDATE Asset a SET a.storageId = :storageId "
74              + "WHERE a.snapshot = :snapshot "
75              + "AND a.mediaPackageElementId = :mediaPackageElementId "
76      ),
77      @NamedQuery(
78          name = "Asset.countAssets",
79          query = "SELECT COUNT(a) FROM Asset a "
80      ),
81  })
82  // Maintain own generator to support database migrations from Archive to AssetManager
83  // The generator's initial value has to be set after the data migration.
84  // Otherwise duplicate key errors will most likely happen.
85  @TableGenerator(name = "seq_oc_assets_asset", initialValue = 0, allocationSize = 50)
86  public class AssetDto {
87  
88    @Id
89    @GeneratedValue(strategy = GenerationType.TABLE, generator = "seq_oc_assets_asset")
90    @Column(name = "id")
91    private Long id;
92  
93    // foreign key referencing SnapshotDto.id
94    @ManyToOne(targetEntity = SnapshotDto.class)
95    @JoinColumn(name = "snapshot_id", referencedColumnName = "id", nullable = false)
96    private SnapshotDto snapshot;
97  
98    @Column(name = "mediapackage_element_id", nullable = false, length = 128)
99    private String mediaPackageElementId;
100 
101   @Column(name = "checksum", nullable = false, length = 64)
102   private String checksum;
103 
104   @Column(name = "mime_type", nullable = true, length = 255)
105   private String mimeType;
106 
107   @Column(name = "size", nullable = false)
108   private Long size;
109 
110   @Column(name = "storage_id", nullable = false, length = 256)
111   private String storageId;
112 
113   /**
114    * Create a new DTO.
115    */
116   public static AssetDto mk(
117       String mediaPackageElementId,
118       SnapshotDto snapshot,
119       String checksum,
120       Optional<MimeType> mimeType,
121       String storeageId,
122       long size
123   ) {
124     final AssetDto dto = new AssetDto();
125     dto.snapshot = snapshot;
126     dto.mediaPackageElementId = mediaPackageElementId;
127     dto.checksum = checksum;
128     dto.mimeType = mimeType.isPresent() ? mimeType.get().toString() : null;
129     dto.storageId = storeageId;
130     dto.size = size;
131     return dto;
132   }
133 
134   public Long getId() {
135     return id;
136   }
137 
138   public String getMediaPackageElementId() {
139     return mediaPackageElementId;
140   }
141 
142   public String getChecksum() {
143     return checksum;
144   }
145 
146   public Optional<MimeType> getMimeType() {
147     return Conversions.toMimeType(mimeType);
148   }
149 
150   public Long getSize() {
151     return size;
152   }
153 
154   public String getStorageId() {
155     return storageId;
156   }
157 
158   void setStorageId(String storage) {
159     this.storageId = storage;
160   }
161 
162   public SnapshotDto getSnapshot() {
163     return snapshot;
164   }
165 
166   public void setSnapshot(SnapshotDto snapshot) {
167     this.snapshot = snapshot;
168   }
169 
170   /**
171    * Count assets in the asset manager
172    *
173    * @return Number of assts
174    */
175   public static Function<EntityManager, Long> countAssetsQuery() {
176     return em -> {
177       TypedQuery<Long> query;
178       query = em.createNamedQuery("Asset.countAssets", Long.class);
179       return query.getSingleResult();
180     };
181   }
182 
183 }