PropertyDto.java

/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package org.opencastproject.assetmanager.impl.persistence;

import org.opencastproject.assetmanager.api.Property;
import org.opencastproject.assetmanager.api.PropertyId;
import org.opencastproject.assetmanager.api.Value;
import org.opencastproject.assetmanager.api.Version;
import org.opencastproject.assetmanager.impl.RuntimeTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

@Entity(name = "Property")
@Table(name = "oc_assets_properties", indexes = {
    @Index(name = "IX_oc_assets_properties_val_date", columnList = ("val_date")),
    @Index(name = "IX_oc_assets_properties_val_long", columnList = ("val_long")),
    @Index(name = "IX_oc_assets_properties_val_string", columnList = ("val_string")),
    @Index(name = "IX_oc_assets_properties_val_bool", columnList = ("val_bool")),
    @Index(name = "IX_oc_assets_properties_mediapackage_id", columnList = ("mediapackage_id")),
    @Index(name = "IX_oc_assets_properties_namespace", columnList = ("namespace")),
    @Index(name = "IX_oc_assets_properties_property_name", columnList = ("property_name")) })
@NamedQueries({
    @NamedQuery(name = "Property.selectByMediaPackageAndNamespace", query = "select p from Property p where "
            + "p.mediaPackageId = :mediaPackageId "
            + "AND (:namespace IS NULL OR p.namespace = :namespace)"),
    @NamedQuery(name = "Property.delete", query = "delete from Property p where p.mediaPackageId = :mediaPackageId"),
    @NamedQuery(name = "Property.deleteByNamespace", query = "delete from Property p "
            + "where p.mediaPackageId = :mediaPackageId and p.namespace = :namespace"),
    @NamedQuery(
        name = "Property.countProperties",
        query = "SELECT COUNT(p) FROM Property p "
    ),
})
public class PropertyDto {
  private static final Logger logger = LoggerFactory.getLogger(PropertyDto.class);

  /** Surrogate key. */
  @Id
  @GeneratedValue
  @Column(name = "id")
  private Long id;

  /** Part 1 of the business key. */
  @Column(name = "mediapackage_id", nullable = false, length = 128)
  private String mediaPackageId;

  /** Part 2 of the business key. */
  @Column(name = "namespace", nullable = false, length = 128)
  private String namespace;

  /** Part 3 of the business key. */
  @Column(name = "property_name", nullable = false, length = 128)
  private String propertyName;

  @Column(name = "val_string", nullable = true)
  private String stringValue;

  @Column(name = "val_date", nullable = true)
  @Temporal(TemporalType.TIMESTAMP)
  private Date dateValue;

  @Column(name = "val_long", nullable = true)
  private Long longValue;

  @Column(name = "val_bool", nullable = true)
  private Boolean boolValue;

  public static PropertyDto mk(Property property) {
    final PropertyDto dto = new PropertyDto();
    dto.mediaPackageId = property.getId().getMediaPackageId();
    dto.namespace = property.getId().getNamespace();
    dto.propertyName = property.getId().getName();
    setValue(dto, property.getValue());
    return dto;
  }

  public Property toProperty() {
    final PropertyId id = new PropertyId(mediaPackageId, namespace, propertyName);
    if (stringValue != null) {
      return new Property(id, Value.mk(stringValue));
    } else if (dateValue != null) {
      return new Property(id, Value.mk(dateValue));
    } else if (longValue != null) {
      return new Property(id, Value.mk(longValue));
    } else if (boolValue != null) {
      return new Property(id, Value.mk(boolValue));
    } else {
      throw new RuntimeException("Bug. At least one of the value columns must be non null.");
    }
  }

  public PropertyDto update(Value value) {
    final PropertyDto dto = new PropertyDto();
    dto.id = id;
    dto.mediaPackageId = mediaPackageId;
    dto.namespace = namespace;
    dto.propertyName = propertyName;
    setValue(dto, value);
    return dto;
  }

  private static void setValue(final PropertyDto dto, final Value value) {
    value.decompose(
        new Function<String, Void>() {
          @Override public Void apply(String a) {
            dto.stringValue = a;
            return null;
          }
        },
        new Function<Date, Void>() {
          @Override public Void apply(Date a) {
            dto.dateValue = a;
            return null;
          }
        },
        new Function<Long, Void>() {
          @Override public Void apply(Long a) {
            dto.longValue = a;
            return null;
          }
        },
        new Function<Boolean, Void>() {
          @Override public Void apply(Boolean a) {
            dto.boolValue = a;
            return null;
          }
        },
        new Function<Version, Void>() {
          @Override public Void apply(Version a) {
            dto.longValue = RuntimeTypes.convert(a).value();
            return null;
          }
        });
  }

  public static Function<EntityManager, Integer> deleteQuery(final String mediaPackageId) {
    return deleteQuery(mediaPackageId, null);
  }

  public static Function<EntityManager, Integer> deleteQuery(final String mediaPackageId, final String namespace) {
    return em -> {
      TypedQuery<PropertyDto> query;
      if (namespace == null) {
        query = em.createNamedQuery("Property.delete", PropertyDto.class)
            .setParameter("mediaPackageId", mediaPackageId);
      } else {
        query = em.createNamedQuery("Property.deleteByNamespace", PropertyDto.class)
            .setParameter("mediaPackageId", mediaPackageId)
            .setParameter("namespace", namespace);
      }
      logger.debug("Executing query {}", query);
      return query.executeUpdate();
    };
  }

  public static Function<EntityManager, List<Property>> selectQuery(final String mediaPackageId,
      final String namespace) {
    return em -> {
      TypedQuery<PropertyDto> query = em.createNamedQuery("Property.selectByMediaPackageAndNamespace",
              PropertyDto.class)
          .setParameter("mediaPackageId", mediaPackageId)
          .setParameter("namespace", namespace);
      logger.debug("Executing query {}", query);
      return query.getResultList().parallelStream().map(PropertyDto::toProperty).collect(Collectors.toList());
    };
  }

  /**
   * Count assets in the asset manager
   *
   * @return Number of assts
   */
  public static Function<EntityManager, Long> countPropertiesQuery() {
    return em -> {
      TypedQuery<Long> query;
      query = em.createNamedQuery("Property.countProperties", Long.class);
      return query.getSingleResult();
    };
  }
}