StaticMetadataServiceMediaPackageImpl.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.metadata;

import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.metadata.api.MetadataValue;
import org.opencastproject.metadata.api.StaticMetadata;
import org.opencastproject.metadata.api.StaticMetadataService;
import org.opencastproject.metadata.api.util.Interval;
import org.opencastproject.util.data.NonEmptyList;
import org.opencastproject.workspace.api.Workspace;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * This service provides {@link org.opencastproject.metadata.api.StaticMetadata} for a given mediapackage, based on the
 * information in the media package itself.
 *
 * todo unit tests will follow
 */
@Component(
    immediate = true,
    service = StaticMetadataService.class,
    property = {
        "service.description=Static Metadata Service, media package based",
        "priority=99"
    }
)
public class StaticMetadataServiceMediaPackageImpl implements StaticMetadataService {

  private static final Logger logger = LoggerFactory.getLogger(StaticMetadataServiceMediaPackageImpl.class);

  // a low default priority
  protected int priority = 99;

  protected Workspace workspace = null;

  @Reference
  public void setWorkspace(Workspace workspace) {
    this.workspace = workspace;
  }

  @Activate
  public void activate(@SuppressWarnings("rawtypes") Map properties) {
    logger.debug("activate()");
    if (properties != null) {
      String priorityString = (String) properties.get(PRIORITY_KEY);
      if (priorityString != null) {
        try {
          priority = Integer.parseInt(priorityString);
        } catch (NumberFormatException e) {
          logger.warn("Unable to set priority to {}", priorityString);
          throw e;
        }
      }
    }
  }

  /**
   * {@inheritDoc}
   *
   * @see org.opencastproject.metadata.api.MetadataService#getMetadata(org.opencastproject.mediapackage.MediaPackage)
   */
  @Override
  public StaticMetadata getMetadata(final MediaPackage mp) {
    return new StaticMetadata() {
      @Override
      public Optional<String> getId() {
        return Optional.ofNullable(mp.getIdentifier().toString());
      }

      @Override
      public Optional<Long> getExtent() {
        return Optional.ofNullable(mp.getDuration());
      }

      @Override
      public Optional<String> getLanguage() {
        return Optional.empty();
      }

      @Override
      public Optional<String> getIsPartOf() {
        return Optional.ofNullable(mp.getSeries());
      }

      @Override
      public Optional<String> getReplaces() {
        return Optional.empty();
      }

      @Override
      public Optional<String> getType() {
        return Optional.empty();
      }

      @Override
      public Optional<Interval> getAvailable() {
        return Optional.empty();
      }

      @Override
      public Optional<Date[]> getTemporalPeriod() {
        return Optional.empty();
      }

      @Override
      public Optional<Date> getTemporalInstant() {
        return Optional.empty();
      }

      @Override
      public Optional<Long> getTemporalDuration() {
        return Optional.empty();
      }

      @Override
      public NonEmptyList<MetadataValue<String>> getTitles() {
        if (mp.getTitle() != null) {
          return new NonEmptyList<MetadataValue<String>>(new MetadataValue(mp.getTitle(), "title"));
        } else {
          throw new IllegalArgumentException("MediaPackage " + mp + " does not contain a title");
        }
      }

      @Override
      public List<MetadataValue<String>> getSubjects() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getCreators() {
        return strings2MetadataValues(mp.getCreators(), "creator");
      }

      @Override
      public List<MetadataValue<String>> getPublishers() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getContributors() {
        return strings2MetadataValues(mp.getContributors(), "contributor");
      }

      @Override
      public List<MetadataValue<String>> getDescription() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getRightsHolders() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getSpatials() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getAccessRights() {
        return Collections.emptyList();
      }

      @Override
      public List<MetadataValue<String>> getLicenses() {
        if (mp.getLicense() != null) {
          return Arrays.asList(new MetadataValue<String>(mp.getLicense(), "license"));
        } else {
          return Collections.emptyList();
        }
      }
    };
  }

  /**
   * {@inheritDoc}
   *
   * @see org.opencastproject.metadata.api.MetadataService#getPriority()
   */
  @Override
  public int getPriority() {
    return priority;
  }

  /**
   * @param values
   *          may be null
   * @param valueName
   *          the name of the returned {@link MetadataValue}
   */
  private static List<MetadataValue<String>> strings2MetadataValues(final String[] values, final String valueName) {
    if (values == null) {
      return Collections.emptyList();
    }
    return Arrays.stream(values)
        .map(s -> new MetadataValue<>(s, valueName))
        .toList();
  }

}