Search in sources :

Example 26 with Publication

use of org.opencastproject.mediapackage.Publication in project opencast by opencast.

the class ConfigurableWorkflowOperationHandlerBase method retract.

public void retract(MediaPackage mp, final String channelId) throws WorkflowOperationException {
    assert ((mp != null) && (channelId != null));
    final List<Publication> publications = getPublications(mp, channelId);
    if (publications.size() > 0) {
        int retractedElementsCount = 0;
        for (Publication publication : publications) {
            retractedElementsCount += retractPublicationElements(channelId, publication, mp);
            mp.remove(publication);
        }
        logger.info("Successfully retracted {} publications and retracted {} elements from publication channel '{}'", publications.size(), retractedElementsCount, channelId);
    } else {
        logger.info("No publications for channel {} found for media package {}", channelId, mp.getIdentifier());
    }
}
Also used : Publication(org.opencastproject.mediapackage.Publication)

Example 27 with Publication

use of org.opencastproject.mediapackage.Publication in project opencast by opencast.

the class PublishOaiPmhWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running distribution workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    // Check which tags have been configured
    String downloadTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_TAGS));
    String downloadFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_FLAVORS));
    String streamingTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_TAGS));
    String streamingFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_FLAVORS));
    boolean checkAvailability = option(workflowInstance.getCurrentOperation().getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
    String repository = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(REPOSITORY));
    Opt<String> externalChannel = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_CHANNEL_NAME);
    Opt<String> externalTempalte = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_TEMPLATE);
    Opt<MimeType> externalMimetype = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_MIME_TYPE).bind(MimeTypes.toMimeType);
    if (repository == null)
        throw new IllegalArgumentException("No repository has been specified");
    String[] sourceDownloadTags = StringUtils.split(downloadTags, ",");
    String[] sourceDownloadFlavors = StringUtils.split(downloadFlavors, ",");
    String[] sourceStreamingTags = StringUtils.split(streamingTags, ",");
    String[] sourceStreamingFlavors = StringUtils.split(streamingFlavors, ",");
    if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
        logger.warn("No tags or flavors have been specified, so nothing will be published to the engage");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    final SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
    for (String flavor : sourceDownloadFlavors) {
        downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceDownloadTags) {
        downloadElementSelector.addTag(tag);
    }
    final Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
    final Collection<MediaPackageElement> streamingElements;
    if (distributeStreaming) {
        final SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
        for (String flavor : sourceStreamingFlavors) {
            streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        }
        for (String tag : sourceStreamingTags) {
            streamingElementSelector.addTag(tag);
        }
        streamingElements = streamingElementSelector.select(mediaPackage, false);
    } else {
        streamingElements = list();
    }
    try {
        Set<String> downloadElementIds = new HashSet<>();
        Set<String> streamingElementIds = new HashSet<>();
        // Look for elements matching the tag
        for (MediaPackageElement elem : downloadElements) {
            downloadElementIds.add(elem.getIdentifier());
        }
        for (MediaPackageElement elem : streamingElements) {
            streamingElementIds.add(elem.getIdentifier());
        }
        // Also distribute the security configuration
        // -----
        // This was removed in the meantime by a fix for MH-8515, but could now be used again.
        // -----
        Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
        if (securityAttachments != null && securityAttachments.length > 0) {
            for (Attachment a : securityAttachments) {
                downloadElementIds.add(a.getIdentifier());
                streamingElementIds.add(a.getIdentifier());
            }
        }
        Job publishJob = null;
        try {
            publishJob = publicationService.publish(mediaPackage, repository, downloadElementIds, streamingElementIds, checkAvailability);
        } catch (MediaPackageException e) {
            throw new WorkflowOperationException("Error parsing media package", e);
        } catch (PublicationException e) {
            throw new WorkflowOperationException("Error parsing media package", e);
        }
        // Wait until the publication job has returned
        if (!waitForStatus(publishJob).isSuccess())
            throw new WorkflowOperationException("Mediapackage " + mediaPackage.getIdentifier() + " could not be published to OAI-PMH repository " + repository);
        // The job has passed
        Job job = serviceRegistry.getJob(publishJob.getId());
        // If there is no payload, then the item has not been published.
        if (job.getPayload() == null) {
            logger.warn("Publish to OAI-PMH repository '{}' failed, no payload from publication job: {}", repository, job);
            return createResult(mediaPackage, Action.CONTINUE);
        }
        Publication newElement = null;
        try {
            newElement = (Publication) MediaPackageElementParser.getFromXml(job.getPayload());
        } catch (MediaPackageException e) {
            throw new WorkflowOperationException(e);
        }
        if (newElement == null) {
            logger.warn("Publication to OAI-PMH repository '{}' failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", repository, job.getPayload(), job.toString());
            return createResult(mediaPackage, Action.CONTINUE);
        }
        for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(newElement.getChannel()).toFn())) {
            mediaPackage.remove(existingPublication);
        }
        mediaPackage.add(newElement);
        if (externalChannel.isSome() && externalMimetype.isSome() && externalTempalte.isSome()) {
            String template = externalTempalte.get().replace("{event}", mediaPackage.getIdentifier().compact());
            if (StringUtils.isNotBlank(mediaPackage.getSeries()))
                template = template.replace("{series}", mediaPackage.getSeries());
            Publication externalElement = PublicationImpl.publication(UUID.randomUUID().toString(), externalChannel.get(), URI.create(template), externalMimetype.get());
            for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(externalChannel.get()).toFn())) {
                mediaPackage.remove(existingPublication);
            }
            mediaPackage.add(externalElement);
        }
        logger.debug("Publication to OAI-PMH repository '{}' operation completed", repository);
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) Publication(org.opencastproject.mediapackage.Publication) Attachment(org.opencastproject.mediapackage.Attachment) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MimeType(org.opencastproject.util.MimeType) PublicationException(org.opencastproject.publication.api.PublicationException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet)

Example 28 with Publication

use of org.opencastproject.mediapackage.Publication in project opencast by opencast.

the class PublishEngageWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running engage publication workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String downloadSourceTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_TAGS));
    String downloadTargetTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_TARGET_TAGS));
    String downloadSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_FLAVORS));
    String downloadTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(DOWNLOAD_TARGET_SUBFLAVOR));
    String streamingSourceTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_TAGS));
    String streamingTargetTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_TARGET_TAGS));
    String streamingSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_FLAVORS));
    String streamingTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(STREAMING_TARGET_SUBFLAVOR));
    String republishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
    boolean checkAvailability = option(op.getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
    String[] sourceDownloadTags = StringUtils.split(downloadSourceTags, ",");
    String[] targetDownloadTags = StringUtils.split(downloadTargetTags, ",");
    String[] sourceDownloadFlavors = StringUtils.split(downloadSourceFlavors, ",");
    String[] sourceStreamingTags = StringUtils.split(streamingSourceTags, ",");
    String[] targetStreamingTags = StringUtils.split(streamingTargetTags, ",");
    String[] sourceStreamingFlavors = StringUtils.split(streamingSourceFlavors, ",");
    if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
        logger.warn("No tags or flavors have been specified, so nothing will be published to the engage publication channel");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Parse the download target flavor
    MediaPackageElementFlavor downloadSubflavor = null;
    if (downloadTargetSubflavor != null) {
        try {
            downloadSubflavor = MediaPackageElementFlavor.parseFlavor(downloadTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Parse the streaming target flavor
    MediaPackageElementFlavor streamingSubflavor = null;
    if (streamingTargetSubflavor != null) {
        try {
            streamingSubflavor = MediaPackageElementFlavor.parseFlavor(streamingTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Configure the download element selector
    SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
    for (String flavor : sourceDownloadFlavors) {
        downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceDownloadTags) {
        downloadElementSelector.addTag(tag);
    }
    // Configure the streaming element selector
    SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
    for (String flavor : sourceStreamingFlavors) {
        streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceStreamingTags) {
        streamingElementSelector.addTag(tag);
    }
    // Select the appropriate elements for download and streaming
    Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
    Collection<MediaPackageElement> streamingElements = streamingElementSelector.select(mediaPackage, false);
    try {
        Set<String> downloadElementIds = new HashSet<String>();
        Set<String> streamingElementIds = new HashSet<String>();
        // Look for elements matching the tag
        for (MediaPackageElement elem : downloadElements) {
            downloadElementIds.add(elem.getIdentifier());
        }
        for (MediaPackageElement elem : streamingElements) {
            streamingElementIds.add(elem.getIdentifier());
        }
        // Also distribute the security configuration
        // -----
        // This was removed in the meantime by a fix for MH-8515, but could now be used again.
        // -----
        Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
        if (securityAttachments != null && securityAttachments.length > 0) {
            for (Attachment a : securityAttachments) {
                downloadElementIds.add(a.getIdentifier());
                streamingElementIds.add(a.getIdentifier());
            }
        }
        removePublicationElement(mediaPackage);
        switch(republishStrategy) {
            case ("merge"):
                // nothing to do here. other publication strategies can be added to this list later on
                break;
            default:
                retractFromEngage(mediaPackage);
        }
        List<Job> jobs = new ArrayList<Job>();
        // distribute Elements
        try {
            if (downloadElementIds.size() > 0) {
                Job job = downloadDistributionService.distribute(CHANNEL_ID, mediaPackage, downloadElementIds, checkAvailability);
                if (job != null) {
                    jobs.add(job);
                }
            }
            if (distributeStreaming) {
                for (String elementId : streamingElementIds) {
                    Job job = streamingDistributionService.distribute(CHANNEL_ID, mediaPackage, elementId);
                    if (job != null) {
                        jobs.add(job);
                    }
                }
            }
        } catch (DistributionException e) {
            throw new WorkflowOperationException(e);
        }
        if (jobs.size() < 1) {
            logger.info("No mediapackage element was found for distribution to engage");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait until all distribution jobs have returned
        if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the distribution jobs did not complete successfully");
        logger.debug("Distribute of mediapackage {} completed", mediaPackage);
        String engageUrlString = null;
        try {
            MediaPackage mediaPackageForSearch = getMediaPackageForSearchIndex(mediaPackage, jobs, downloadSubflavor, targetDownloadTags, downloadElementIds, streamingSubflavor, streamingElementIds, targetStreamingTags);
            // MH-10216, check if only merging into existing mediapackage
            removePublicationElement(mediaPackage);
            switch(republishStrategy) {
                case ("merge"):
                    // merge() returns merged mediapackage or null mediaPackage is not published
                    mediaPackageForSearch = merge(mediaPackageForSearch);
                    if (mediaPackageForSearch == null) {
                        logger.info("Skipping republish for {} since it is not currently published", mediaPackage.getIdentifier().toString());
                        return createResult(mediaPackage, Action.SKIP);
                    }
                    break;
                default:
            }
            if (!isPublishable(mediaPackageForSearch))
                throw new WorkflowOperationException("Media package does not meet criteria for publication");
            logger.info("Publishing media package {} to search index", mediaPackageForSearch);
            URL engageBaseUrl = null;
            engageUrlString = StringUtils.trimToNull(workflowInstance.getOrganization().getProperties().get(ENGAGE_URL_PROPERTY));
            if (engageUrlString != null) {
                engageBaseUrl = new URL(engageUrlString);
            } else {
                engageBaseUrl = serverUrl;
                logger.info("Using 'server.url' as a fallback for the non-existing organization level key '{}' for the publication url", ENGAGE_URL_PROPERTY);
            }
            // create the publication URI (used by Admin UI for event details link)
            URI engageUri = this.createEngageUri(engageBaseUrl.toURI(), mediaPackage);
            // Create new distribution element
            Publication publicationElement = PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_ID, engageUri, MimeTypes.parseMimeType("text/html"));
            mediaPackage.add(publicationElement);
            // Adding media package to the search index
            Job publishJob = null;
            try {
                publishJob = searchService.add(mediaPackageForSearch);
                if (!waitForStatus(publishJob).isSuccess()) {
                    throw new WorkflowOperationException("Mediapackage " + mediaPackageForSearch.getIdentifier() + " could not be published");
                }
            } catch (SearchException e) {
                throw new WorkflowOperationException("Error publishing media package", e);
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Error parsing media package", e);
            }
            logger.debug("Publishing of mediapackage {} completed", mediaPackage);
            return createResult(mediaPackage, Action.CONTINUE);
        } catch (MalformedURLException e) {
            logger.error("{} is malformed: {}", ENGAGE_URL_PROPERTY, engageUrlString);
            throw new WorkflowOperationException(e);
        } catch (Throwable t) {
            if (t instanceof WorkflowOperationException)
                throw (WorkflowOperationException) t;
            else
                throw new WorkflowOperationException(t);
        }
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) SearchException(org.opencastproject.search.api.SearchException) Attachment(org.opencastproject.mediapackage.Attachment) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) URI(java.net.URI) URL(java.net.URL) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) Publication(org.opencastproject.mediapackage.Publication) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException)

Example 29 with Publication

use of org.opencastproject.mediapackage.Publication in project opencast by opencast.

the class PublishYouTubeWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running youtube publication workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    // Check which tags have been configured
    String sourceTags = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-tags"));
    String sourceFlavors = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-flavors"));
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector;
    if (sourceTags == null && sourceFlavors == null) {
        logger.warn("No tags or flavor have been specified");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    elementSelector = new SimpleElementSelector();
    if (sourceFlavors != null) {
        for (String flavor : asList(sourceFlavors)) {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        }
    }
    if (sourceTags != null) {
        for (String tag : asList(sourceTags)) {
            elementSelector.addTag(tag);
        }
    }
    try {
        // Look for elements matching the tag
        final Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
        if (elements.size() > 1) {
            logger.warn("More than one element has been found for publishing to youtube: {}", elements);
            return createResult(mediaPackage, Action.SKIP);
        }
        if (elements.size() < 1) {
            logger.info("No mediapackage element was found for publishing");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        Job youtubeJob;
        try {
            Track track = mediaPackage.getTrack(elements.iterator().next().getIdentifier());
            youtubeJob = publicationService.publish(mediaPackage, track);
        } catch (PublicationException e) {
            throw new WorkflowOperationException(e);
        }
        // Wait until the youtube publication job has returned
        if (!waitForStatus(youtubeJob).isSuccess())
            throw new WorkflowOperationException("The youtube publication jobs did not complete successfully");
        // All the jobs have passed
        Job job = serviceRegistry.getJob(youtubeJob.getId());
        // If there is no payload, then the item has not been published.
        if (job.getPayload() == null) {
            logger.warn("Publish to youtube failed, no payload from publication job: {}", job);
            return createResult(mediaPackage, Action.CONTINUE);
        }
        Publication newElement = null;
        try {
            newElement = (Publication) MediaPackageElementParser.getFromXml(job.getPayload());
        } catch (MediaPackageException e) {
            throw new WorkflowOperationException(e);
        }
        if (newElement == null) {
            logger.warn("Publication to youtube failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", job.getPayload(), job);
            return createResult(mediaPackage, Action.CONTINUE);
        }
        mediaPackage.add(newElement);
        logger.debug("Publication to youtube operation completed");
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) Publication(org.opencastproject.mediapackage.Publication) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Example 30 with Publication

use of org.opencastproject.mediapackage.Publication in project opencast by opencast.

the class RetractEngageWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(WorkflowInstance, JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    try {
        List<Job> jobs = new ArrayList<Job>();
        SearchQuery query = new SearchQuery().withId(mediaPackage.getIdentifier().toString());
        SearchResult result = searchService.getByQuery(query);
        if (result.size() == 0) {
            logger.info("The search service doesn't know mediapackage {}", mediaPackage);
            return createResult(mediaPackage, Action.SKIP);
        } else if (result.size() > 1) {
            logger.warn("More than one mediapackage with id {} returned from search service", mediaPackage.getIdentifier());
            throw new WorkflowOperationException("More than one mediapackage with id " + mediaPackage.getIdentifier() + " found");
        } else {
            Set<String> retractElementIds = new HashSet<String>();
            MediaPackage searchMediaPackage = result.getItems()[0].getMediaPackage();
            logger.info("Retracting media package {} from download/streaming distribution channel", searchMediaPackage);
            for (MediaPackageElement element : searchMediaPackage.getElements()) {
                retractElementIds.add(element.getIdentifier());
            }
            if (retractElementIds.size() > 0) {
                Job retractDownloadDistributionJob = downloadDistributionService.retract(CHANNEL_ID, searchMediaPackage, retractElementIds);
                if (retractDownloadDistributionJob != null) {
                    jobs.add(retractDownloadDistributionJob);
                }
            }
            if (distributeStreaming) {
                for (MediaPackageElement element : searchMediaPackage.getElements()) {
                    if (distributeStreaming) {
                        Job retractStreamingJob = streamingDistributionService.retract(CHANNEL_ID, searchMediaPackage, element.getIdentifier());
                        if (retractStreamingJob != null) {
                            jobs.add(retractStreamingJob);
                        }
                    }
                }
            }
        }
        // Wait for retraction to finish
        if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
            throw new WorkflowOperationException("One of the download/streaming retract job did not complete successfully");
        }
        logger.debug("Retraction operation complete");
        logger.info("Removing media package {} from the search index", mediaPackage);
        Job deleteFromSearch = searchService.delete(mediaPackage.getIdentifier().toString());
        if (!waitForStatus(deleteFromSearch).isSuccess())
            throw new WorkflowOperationException("Removing media package from search did not complete successfully");
        logger.debug("Remove from search operation complete");
        // Remove publication element
        logger.info("Removing engage publication element from media package {}", mediaPackage);
        Publication[] publications = mediaPackage.getPublications();
        for (Publication publication : publications) {
            if (CHANNEL_ID.equals(publication.getChannel())) {
                mediaPackage.remove(publication);
                logger.debug("Remove engage publication element '{}' complete", publication);
            }
        }
        return createResult(mediaPackage, Action.CONTINUE);
    } catch (Throwable t) {
        throw new WorkflowOperationException(t);
    }
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) SearchResult(org.opencastproject.search.api.SearchResult) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job)

Aggregations

Publication (org.opencastproject.mediapackage.Publication)37 MediaPackage (org.opencastproject.mediapackage.MediaPackage)21 ArrayList (java.util.ArrayList)17 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)15 Job (org.opencastproject.job.api.Job)12 URI (java.net.URI)11 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)9 HashSet (java.util.HashSet)8 SimpleElementSelector (org.opencastproject.mediapackage.selector.SimpleElementSelector)7 PublicationException (org.opencastproject.publication.api.PublicationException)7 Test (org.junit.Test)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 Ignore (org.junit.Ignore)4 DistributionException (org.opencastproject.distribution.api.DistributionException)4 Attachment (org.opencastproject.mediapackage.Attachment)4 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)4 PublicationImpl (org.opencastproject.mediapackage.PublicationImpl)4 Track (org.opencastproject.mediapackage.Track)4 NotFoundException (org.opencastproject.util.NotFoundException)4 Event (org.opencastproject.index.service.impl.index.event.Event)3