All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencastproject.assetmanager.util.WorkflowPropertiesUtil Maven / Gradle / Ivy

The newest version!
/*
 * 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.util;

import static org.opencastproject.assetmanager.api.AssetManager.DEFAULT_OWNER;
import static org.opencastproject.mediapackage.MediaPackageElements.XACML_POLICY_EPISODE;
import static org.opencastproject.mediapackage.MediaPackageElements.XACML_POLICY_SERIES;
import static org.opencastproject.systems.OpencastConstants.WORKFLOW_PROPERTIES_NAMESPACE;

import org.opencastproject.assetmanager.api.AssetManager;
import org.opencastproject.assetmanager.api.Property;
import org.opencastproject.assetmanager.api.PropertyId;
import org.opencastproject.assetmanager.api.Value;
import org.opencastproject.assetmanager.api.query.AQueryBuilder;
import org.opencastproject.assetmanager.api.query.ARecord;
import org.opencastproject.assetmanager.api.query.AResult;
import org.opencastproject.mediapackage.Attachment;
import org.opencastproject.mediapackage.Catalog;
import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.mediapackage.MediaPackageElement;
import org.opencastproject.mediapackage.MediaPackageElementFlavor;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Utility class to store and retrieve Workflow Properties (which are stored in specially prefixed Asset Manager
 * properties)
 */
public final class WorkflowPropertiesUtil {
  private static final Set SECURITY_FLAVORS = new HashSet<>(
          Arrays.asList(XACML_POLICY_EPISODE, XACML_POLICY_SERIES));

  private WorkflowPropertiesUtil() {
  }

  /**
   * Retrieve latest properties for a set of event ids
   * @param assetManager The Asset Manager to use
   * @param eventIds Collection of event IDs (can be a set, but doesn't have to be)
   * @return A map mapping event IDs to key value pairs (which are themselves maps) representing the properties
   */
  public static Map> getLatestWorkflowPropertiesForEvents(final AssetManager assetManager,
          final Collection eventIds) {
    final AQueryBuilder query = assetManager.createQuery();
    final AResult result = query.select(query.snapshot(), query.propertiesOf(WORKFLOW_PROPERTIES_NAMESPACE))
            .where(query.mediaPackageIds(eventIds.toArray(new String[0])).and(query.version().isLatest())).run();
    final Map> workflowProperties = new HashMap<>(eventIds.size());
    for (final ARecord record : result.getRecords()) {
      final List recordProps = record.getProperties();
      final Map eventMap = new HashMap<>(recordProps.size());
      for (final Property property : recordProps) {
        eventMap.put(property.getId().getName(), property.getValue().get(Value.STRING));
      }
      final String eventId = record.getMediaPackageId();
      workflowProperties.put(eventId, eventMap);
    }
    return workflowProperties;
  }

  /**
   * Retrieve the latest properties for a single media package
   * @param assetManager The Asset Manager to use
   * @param mediaPackageId The media package to query
   * @return A list of properties represented by a Map
   */
  public static Map getLatestWorkflowProperties(final AssetManager assetManager,
          final String mediaPackageId) {
    return assetManager.selectProperties(mediaPackageId, WORKFLOW_PROPERTIES_NAMESPACE)
            .parallelStream()
            .collect(Collectors.toMap(
                    p -> p.getId().getName(),
                    p -> p.getValue().get(Value.STRING)));
  }

  /**
   * Store selected properties for a media package
   * @param assetManager The Asset Manager to use
   * @param mediaPackage The media package to store properties relative to
   * @param properties A list of properties represented by a Map
   */
  public static void storeProperties(final AssetManager assetManager, final MediaPackage mediaPackage,
          final Map properties) {

    // Properties can only be created if a snapshot exists. Hence, we create a snapshot if there is none right now.
    // Although, to avoid lots of potentially slow IO operations, we archive a slimmer version of the media package,
    // containing only metadata and security attachments.
    if (!assetManager.snapshotExists(mediaPackage.getIdentifier().toString())) {
      MediaPackage simplifiedMediaPackage = (MediaPackage) mediaPackage.clone();
      for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element instanceof Catalog) {
          continue;
        }
        if (element instanceof Attachment && SECURITY_FLAVORS.contains(element.getFlavor())) {
          continue;
        }
        simplifiedMediaPackage.remove(element);
      }

      assetManager.takeSnapshot(DEFAULT_OWNER, simplifiedMediaPackage);
    }

    // Store all properties
    for (final Map.Entry entry : properties.entrySet()) {
      final PropertyId propertyId = PropertyId
              .mk(mediaPackage.getIdentifier().toString(), WORKFLOW_PROPERTIES_NAMESPACE, entry.getKey());
      final Property property = Property.mk(propertyId, Value.mk(entry.getValue()));
      assetManager.setProperty(property);
    }
  }

  public static void storeProperty(final AssetManager assetManager, final MediaPackage mediaPackage,
          final String name, final String value) {
    storeProperties(assetManager, mediaPackage, Collections.singletonMap(name, value));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy