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.assetmanager.api.Property;
24  import org.opencastproject.assetmanager.api.PropertyId;
25  import org.opencastproject.assetmanager.api.Value;
26  import org.opencastproject.assetmanager.api.Version;
27  import org.opencastproject.assetmanager.impl.RuntimeTypes;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.util.Date;
33  import java.util.List;
34  import java.util.function.Function;
35  import java.util.stream.Collectors;
36  
37  import javax.persistence.Column;
38  import javax.persistence.Entity;
39  import javax.persistence.EntityManager;
40  import javax.persistence.GeneratedValue;
41  import javax.persistence.Id;
42  import javax.persistence.Index;
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.TypedQuery;
49  
50  @Entity(name = "Property")
51  @Table(name = "oc_assets_properties", indexes = {
52      @Index(name = "IX_oc_assets_properties_val_date", columnList = ("val_date")),
53      @Index(name = "IX_oc_assets_properties_val_long", columnList = ("val_long")),
54      @Index(name = "IX_oc_assets_properties_val_string", columnList = ("val_string")),
55      @Index(name = "IX_oc_assets_properties_val_bool", columnList = ("val_bool")),
56      @Index(name = "IX_oc_assets_properties_mediapackage_id", columnList = ("mediapackage_id")),
57      @Index(name = "IX_oc_assets_properties_namespace", columnList = ("namespace")),
58      @Index(name = "IX_oc_assets_properties_property_name", columnList = ("property_name")) })
59  @NamedQueries({
60      @NamedQuery(name = "Property.selectByMediaPackageAndNamespace", query = "select p from Property p where "
61              + "p.mediaPackageId = :mediaPackageId and p.namespace = :namespace"),
62      @NamedQuery(name = "Property.delete", query = "delete from Property p where p.mediaPackageId = :mediaPackageId"),
63      @NamedQuery(name = "Property.deleteByNamespace", query = "delete from Property p "
64              + "where p.mediaPackageId = :mediaPackageId and p.namespace = :namespace")})
65  public class PropertyDto {
66    private static final Logger logger = LoggerFactory.getLogger(PropertyDto.class);
67  
68    /** Surrogate key. */
69    @Id
70    @GeneratedValue
71    @Column(name = "id")
72    private Long id;
73  
74    /** Part 1 of the business key. */
75    @Column(name = "mediapackage_id", nullable = false, length = 128)
76    private String mediaPackageId;
77  
78    /** Part 2 of the business key. */
79    @Column(name = "namespace", nullable = false, length = 128)
80    private String namespace;
81  
82    /** Part 3 of the business key. */
83    @Column(name = "property_name", nullable = false, length = 128)
84    private String propertyName;
85  
86    @Column(name = "val_string", nullable = true)
87    private String stringValue;
88  
89    @Column(name = "val_date", nullable = true)
90    @Temporal(TemporalType.TIMESTAMP)
91    private Date dateValue;
92  
93    @Column(name = "val_long", nullable = true)
94    private Long longValue;
95  
96    @Column(name = "val_bool", nullable = true)
97    private Boolean boolValue;
98  
99    public static PropertyDto mk(Property property) {
100     final PropertyDto dto = new PropertyDto();
101     dto.mediaPackageId = property.getId().getMediaPackageId();
102     dto.namespace = property.getId().getNamespace();
103     dto.propertyName = property.getId().getName();
104     setValue(dto, property.getValue());
105     return dto;
106   }
107 
108   public Property toProperty() {
109     final PropertyId id = new PropertyId(mediaPackageId, namespace, propertyName);
110     if (stringValue != null) {
111       return new Property(id, Value.mk(stringValue));
112     } else if (dateValue != null) {
113       return new Property(id, Value.mk(dateValue));
114     } else if (longValue != null) {
115       return new Property(id, Value.mk(longValue));
116     } else if (boolValue != null) {
117       return new Property(id, Value.mk(boolValue));
118     } else {
119       throw new RuntimeException("Bug. At least one of the value columns must be non null.");
120     }
121   }
122 
123   public PropertyDto update(Value value) {
124     final PropertyDto dto = new PropertyDto();
125     dto.id = id;
126     dto.mediaPackageId = mediaPackageId;
127     dto.namespace = namespace;
128     dto.propertyName = propertyName;
129     setValue(dto, value);
130     return dto;
131   }
132 
133   private static void setValue(final PropertyDto dto, final Value value) {
134     value.decompose(
135         new Function<String, Void>() {
136           @Override public Void apply(String a) {
137             dto.stringValue = a;
138             return null;
139           }
140         },
141         new Function<Date, Void>() {
142           @Override public Void apply(Date a) {
143             dto.dateValue = a;
144             return null;
145           }
146         },
147         new Function<Long, Void>() {
148           @Override public Void apply(Long a) {
149             dto.longValue = a;
150             return null;
151           }
152         },
153         new Function<Boolean, Void>() {
154           @Override public Void apply(Boolean a) {
155             dto.boolValue = a;
156             return null;
157           }
158         },
159         new Function<Version, Void>() {
160           @Override public Void apply(Version a) {
161             dto.longValue = RuntimeTypes.convert(a).value();
162             return null;
163           }
164         });
165   }
166 
167   public static Function<EntityManager, Integer> deleteQuery(final String mediaPackageId) {
168     return deleteQuery(mediaPackageId, null);
169   }
170 
171   public static Function<EntityManager, Integer> deleteQuery(final String mediaPackageId, final String namespace) {
172     return em -> {
173       TypedQuery<PropertyDto> query;
174       if (namespace == null) {
175         query = em.createNamedQuery("Property.delete", PropertyDto.class)
176             .setParameter("mediaPackageId", mediaPackageId);
177       } else {
178         query = em.createNamedQuery("Property.deleteByNamespace", PropertyDto.class)
179             .setParameter("mediaPackageId", mediaPackageId)
180             .setParameter("namespace", namespace);
181       }
182       logger.debug("Executing query {}", query);
183       return query.executeUpdate();
184     };
185   }
186 
187   public static Function<EntityManager, List<Property>> selectQuery(final String mediaPackageId,
188       final String namespace) {
189     return em -> {
190       TypedQuery<PropertyDto> query = em.createNamedQuery("Property.selectByMediaPackageAndNamespace",
191               PropertyDto.class)
192           .setParameter("mediaPackageId", mediaPackageId)
193           .setParameter("namespace", namespace);
194       logger.debug("Executing query {}", query);
195       return query.getResultList().parallelStream().map(PropertyDto::toProperty).collect(Collectors.toList());
196     };
197   }
198 }