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

org.opencastproject.message.broker.api.assetmanager.AssetManagerItem Maven / Gradle / Ivy

There is a newer version: 16.4
Show 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.message.broker.api.assetmanager;

import static com.entwinemedia.fn.Prelude.chuck;

import org.opencastproject.mediapackage.Catalog;
import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.mediapackage.MediaPackageElements;
import org.opencastproject.mediapackage.MediaPackageException;
import org.opencastproject.mediapackage.MediaPackageParser;
import org.opencastproject.message.broker.api.MessageItem;
import org.opencastproject.metadata.dublincore.DublinCoreCatalog;
import org.opencastproject.metadata.dublincore.DublinCores;
import org.opencastproject.security.api.AccessControlList;
import org.opencastproject.security.api.AccessControlParser;
import org.opencastproject.util.RequireUtil;
import org.opencastproject.workspace.api.Workspace;

import com.entwinemedia.fn.Fn;
import com.entwinemedia.fn.data.Opt;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import javax.annotation.ParametersAreNonnullByDefault;

/**
 * {@link Serializable} class that represents all of the possible messages sent through an AssetManager queue.
 */
@ParametersAreNonnullByDefault
public abstract class AssetManagerItem implements MessageItem, Serializable {
  private static final long serialVersionUID = 5440420510139202434L;

  public enum Type {
    Update, Delete
  }

  // common fields

  private final String mediaPackageId;
  private final long date;

  private AssetManagerItem(String mediaPackageId, Date date) {
    this.mediaPackageId = RequireUtil.notNull(mediaPackageId, "mediaPackageId");
    this.date = RequireUtil.notNull(date, "date").getTime();
  }

  public abstract Type getType();

  public abstract  A decompose(Fn takeSnapshot,
          Fn deleteEpisode);

  public final Date getDate() {
    return new Date(date);
  }

  @Override
  public final String getId() {
    return mediaPackageId;
  }

  /*
   * ------------------------------------------------------------------------------------------------------------------
   */

  /**
   * An event for taking a snapshot of a media package.
   */
  public static final class TakeSnapshot extends AssetManagerItem {
    private static final long serialVersionUID = 3530625835200867594L;

    private final String mediapackage;
    private final String acl;
    private final long version;
    private final String episodeDublincore;

    private TakeSnapshot(String mediaPackageId, String mediapackage, String episodeDublincore, String acl, long version,
            Date date) {
      super(mediaPackageId, date);
      this.mediapackage = mediapackage;
      this.episodeDublincore = episodeDublincore;
      this.acl = acl;
      this.version = version;
    }

    @Override
    public  A decompose(Fn takeSnapshot,
            Fn deleteEpisode) {
      return takeSnapshot.apply(this);
    }

    @Override
    public Type getType() {
      return Type.Update;
    }

    public MediaPackage getMediapackage() {
      try {
        return MediaPackageParser.getFromXml(mediapackage);
      } catch (MediaPackageException e) {
        return chuck(e);
      }
    }

    public AccessControlList getAcl() {
      return AccessControlParser.parseAclSilent(acl);
    }

    public Opt getEpisodeDublincore() {
      if (episodeDublincore == null) {
        return Opt.none();
      }

      try (InputStream is = IOUtils.toInputStream(episodeDublincore, "UTF-8")) {
        return Opt.some(DublinCores.read(is));
      } catch (IOException e) {
        return chuck(e);
      }
    }

    public long getVersion() {
      return version;
    }

    //

    public static final Fn getMediaPackage = new Fn() {
      @Override
      public MediaPackage apply(TakeSnapshot a) {
        return a.getMediapackage();
      }
    };

    public static final Fn> getEpisodeDublincore
        = new Fn>() {
          @Override
          public Opt apply(TakeSnapshot a) {
            return a.getEpisodeDublincore();
          }
        };

    public static final Fn getAcl = new Fn() {
      @Override
      public AccessControlList apply(TakeSnapshot a) {
        return a.getAcl();
      }
    };

  }

  /*
   * ------------------------------------------------------------------------------------------------------------------
   */

  /*
   * ------------------------------------------------------------------------------------------------------------------
   */

  /**
   * A event that will be sent when all versions of a media package (aka the whole episode) have been deleted.
   */
  public static final class DeleteEpisode extends AssetManagerItem {
    private static final long serialVersionUID = -4906056424740181256L;

    private DeleteEpisode(String mediaPackageId, Date date) {
      super(mediaPackageId, date);
    }

    @Override
    public  A decompose(Fn takeSnapshot,
            Fn deleteEpisode) {
      return deleteEpisode.apply(this);
    }

    @Override
    public Type getType() {
      return Type.Delete;
    }

    public String getMediaPackageId() {
      return getId();
    }

    public static final Fn getMediaPackageId = new Fn() {
      @Override
      public String apply(DeleteEpisode a) {
        return a.getMediaPackageId();
      }
    };
  }

  /*
   * ------------------------------------------------------------------------------------------------------------------
   */

  //
  // constructor methods
  //

  /**
   * @param workspace
   *          The workspace
   * @param mp
   *          The media package to update.
   * @param acl
   *          The access control list of the media package to update.
   * @param version
   *          The version of the media package.
   * @param date
   *          The modification date.
   * @return Builds a {@link AssetManagerItem} for taking a media package snapshot.
   */
  public static TakeSnapshot add(Workspace workspace, MediaPackage mp, AccessControlList acl, long version, Date date) {
    String dcXml = null;
    for (Catalog catalog: mp.getCatalogs(MediaPackageElements.EPISODE)) {
      try (InputStream in = workspace.read(catalog.getURI())) {
        dcXml = IOUtils.toString(in, StandardCharsets.UTF_8);
      } catch (Exception e) {
        throw new IllegalStateException(String.format("Unable to load dublin core catalog for event '%s'",
                mp.getIdentifier()), e);
      }
    }
    return new TakeSnapshot(mp.getIdentifier().toString(), MediaPackageParser.getAsXml(mp), dcXml,
            AccessControlParser.toJsonSilent(acl), version, date);
  }

  /**
   * @param mediaPackageId
   *          The unique id of the media package to delete.
   * @param date
   *          The modification date.
   * @return Builds {@link AssetManagerItem} for deleting an episode from the asset manager.
   */
  public static AssetManagerItem deleteEpisode(String mediaPackageId, Date date) {
    return new DeleteEpisode(mediaPackageId, date);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy