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  
27  import javax.persistence.Column;
28  import javax.persistence.Entity;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.GenerationType;
31  import javax.persistence.Id;
32  import javax.persistence.Index;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.ManyToOne;
35  import javax.persistence.Table;
36  import javax.persistence.TableGenerator;
37  
38  /** JPA DTO modeling the asset database table. */
39  @Entity(name = "Asset")
40  @Table(name = "oc_assets_asset", indexes = {
41      @Index(name = "IX_oc_assets_asset_checksum", columnList = ("checksum")),
42      @Index(name = "IX_oc_assets_asset_mediapackage_element_id", columnList = ("mediapackage_element_id")) })
43  // Maintain own generator to support database migrations from Archive to AssetManager
44  // The generator's initial value has to be set after the data migration.
45  // Otherwise duplicate key errors will most likely happen.
46  @TableGenerator(name = "seq_oc_assets_asset", initialValue = 0, allocationSize = 50)
47  public class AssetDto {
48  
49    @Id
50    @GeneratedValue(strategy = GenerationType.TABLE, generator = "seq_oc_assets_asset")
51    @Column(name = "id")
52    private Long id;
53  
54    // foreign key referencing SnapshotDto.id
55    @ManyToOne(targetEntity = SnapshotDto.class)
56    @JoinColumn(name = "snapshot_id", referencedColumnName = "id", nullable = false)
57    private SnapshotDto snapshot;
58  
59    @Column(name = "mediapackage_element_id", nullable = false, length = 128)
60    private String mediaPackageElementId;
61  
62    @Column(name = "checksum", nullable = false, length = 64)
63    private String checksum;
64  
65    @Column(name = "mime_type", nullable = true, length = 255)
66    private String mimeType;
67  
68    @Column(name = "size", nullable = false)
69    private Long size;
70  
71    @Column(name = "storage_id", nullable = false, length = 256)
72    private String storageId;
73  
74    /**
75     * Create a new DTO.
76     */
77    public static AssetDto mk(
78        String mediaPackageElementId,
79        SnapshotDto snapshot,
80        String checksum,
81        Optional<MimeType> mimeType,
82        String storeageId,
83        long size
84    ) {
85      final AssetDto dto = new AssetDto();
86      dto.snapshot = snapshot;
87      dto.mediaPackageElementId = mediaPackageElementId;
88      dto.checksum = checksum;
89      dto.mimeType = mimeType.isPresent() ? mimeType.get().toString() : null;
90      dto.storageId = storeageId;
91      dto.size = size;
92      return dto;
93    }
94  
95    public Long getId() {
96      return id;
97    }
98  
99    public String getMediaPackageElementId() {
100     return mediaPackageElementId;
101   }
102 
103   public String getChecksum() {
104     return checksum;
105   }
106 
107   public Optional<MimeType> getMimeType() {
108     return Conversions.toMimeType(mimeType);
109   }
110 
111   public Long getSize() {
112     return size;
113   }
114 
115   public String getStorageId() {
116     return storageId;
117   }
118 
119   void setStorageId(String storage) {
120     this.storageId = storage;
121   }
122 
123   public SnapshotDto getSnapshot() {
124     return snapshot;
125   }
126 
127   public void setSnapshot(SnapshotDto snapshot) {
128     this.snapshot = snapshot;
129   }
130 }