Search in sources :

Example 66 with MediaPackageElement

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

the class OaiPmhPublicationServiceImpl method updateMetadata.

protected Publication updateMetadata(Job job, MediaPackage mediaPackage, String repository, Set<String> flavors, Set<String> tags, boolean checkAvailability) throws PublicationException {
    final Set<MediaPackageElementFlavor> parsedFlavors = new HashSet<>();
    for (String flavor : flavors) {
        parsedFlavors.add(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    final MediaPackage filteredMp;
    final SearchResult result = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mediaPackage).isDeleted(false).build());
    if (result.size() == 1) {
        // apply tags and flavors to the current media package
        try {
            logger.debug("filter elements with flavors {} and tags {} on media package {}", StringUtils.join(flavors, ", "), StringUtils.join(tags, ", "), MediaPackageParser.getAsXml(mediaPackage));
            filteredMp = filterMediaPackage(mediaPackage, parsedFlavors, tags);
        } catch (MediaPackageException e) {
            throw new PublicationException("Error filtering media package", e);
        }
    } else if (result.size() == 0) {
        logger.info(format("Skipping update of media package %s since it is not currently published to %s", mediaPackage, repository));
        return null;
    } else {
        final String msg = format("More than one media package with id %s found", mediaPackage.getIdentifier().compact());
        logger.warn(msg);
        throw new PublicationException(msg);
    }
    // re-distribute elements to download
    Set<String> elementIdsToDistribute = new HashSet<>();
    for (MediaPackageElement mpe : filteredMp.getElements()) {
        // do not distribute publications
        if (MediaPackageElement.Type.Publication == mpe.getElementType())
            continue;
        elementIdsToDistribute.add(mpe.getIdentifier());
    }
    if (elementIdsToDistribute.isEmpty()) {
        logger.debug("The media package {} does not contain any elements to update. " + "Skip OAI-PMH metadata update operation for repository {}", mediaPackage.getIdentifier().compact(), repository);
        return null;
    }
    logger.debug("distribute elements {}", StringUtils.join(elementIdsToDistribute, ", "));
    final List<MediaPackageElement> distributedElements = new ArrayList<>();
    try {
        Job distJob = downloadDistributionService.distribute(getPublicationChannelName(repository), filteredMp, elementIdsToDistribute, checkAvailability);
        if (job == null)
            throw new PublicationException("The distribution service can not handle this type of media package elements.");
        if (!waitForJobs(job, serviceRegistry, distJob).isSuccess()) {
            throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()));
        }
        if (distJob.getPayload() != null) {
            for (MediaPackageElement mpe : MediaPackageElementParser.getArrayFromXml(distJob.getPayload())) {
                distributedElements.add(mpe);
            }
        }
    } catch (DistributionException | MediaPackageException e) {
        throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()), e);
    }
    // update elements (URLs)
    for (MediaPackageElement e : filteredMp.getElements()) {
        if (MediaPackageElement.Type.Publication.equals(e.getElementType()))
            continue;
        filteredMp.remove(e);
    }
    for (MediaPackageElement e : distributedElements) {
        filteredMp.add(e);
    }
    MediaPackage publishedMp = merge(filteredMp, removeMatchingNonExistantElements(filteredMp, (MediaPackage) result.getItems().get(0).getMediaPackage().clone(), parsedFlavors, tags));
    // Does the media package have a title and track?
    if (!MediaPackageSupport.isPublishable(publishedMp)) {
        throw new PublicationException("Media package does not meet criteria for publication");
    }
    // Publish the media package to OAI-PMH
    try {
        logger.debug(format("Updating metadata of media package %s in %s", publishedMp.getIdentifier().compact(), repository));
        oaiPmhDatabase.store(publishedMp, repository);
    } catch (OaiPmhDatabaseException e) {
        throw new PublicationException(format("Media package %s could not be updated", publishedMp.getIdentifier().compact()));
    }
    // retract orphaned elements from download distribution
    // orphaned elements are all those elements to which the updated media package no longer refers (in terms of element uri)
    Map<URI, MediaPackageElement> elementUriMap = new Hashtable<>();
    for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
        for (MediaPackageElement mpe : oaiPmhSearchResultItem.getMediaPackage().getElements()) {
            if (MediaPackageElement.Type.Publication == mpe.getElementType() || null == mpe.getURI())
                continue;
            elementUriMap.put(mpe.getURI(), mpe);
        }
    }
    for (MediaPackageElement publishedMpe : publishedMp.getElements()) {
        if (MediaPackageElement.Type.Publication == publishedMpe.getElementType())
            continue;
        if (elementUriMap.containsKey(publishedMpe.getURI()))
            elementUriMap.remove(publishedMpe.getURI());
    }
    Set<String> orphanedElementIds = new HashSet<>();
    for (MediaPackageElement orphanedMpe : elementUriMap.values()) {
        orphanedElementIds.add(orphanedMpe.getIdentifier());
    }
    if (!orphanedElementIds.isEmpty()) {
        for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
            try {
                Job retractJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhSearchResultItem.getMediaPackage(), orphanedElementIds);
                if (retractJob != null) {
                    if (!waitForJobs(job, serviceRegistry, retractJob).isSuccess())
                        logger.warn("The download distribution retract job for the orphaned elements from media package {} does not end successfully", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact());
                }
            } catch (DistributionException e) {
                logger.warn("Unable to retract orphaned elements from download distribution service for the media package {} channel {}", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact(), getPublicationChannelName(repository), e);
            }
        }
    }
    // return the publication
    String publicationChannel = getPublicationChannelName(repository);
    for (Publication p : mediaPackage.getPublications()) {
        if (StringUtils.equals(publicationChannel, p.getChannel()))
            return p;
    }
    return null;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Publication(org.opencastproject.mediapackage.Publication) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet)

Example 67 with MediaPackageElement

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

the class OaiPmhPublicationServiceImpl method filterMediaPackage.

/**
 * Creates a clone of the media package and removes those elements that do not match the flavor and tags filter
 * criteria.
 *
 * @param mediaPackage
 *          the media package
 * @param flavors
 *          the flavors
 * @param tags
 *          the tags
 * @return the filtered media package
 */
private MediaPackage filterMediaPackage(MediaPackage mediaPackage, Set<MediaPackageElementFlavor> flavors, Set<String> tags) throws MediaPackageException {
    if (flavors.isEmpty() && tags.isEmpty())
        throw new IllegalArgumentException("Flavors or tags parameter must be set");
    MediaPackage filteredMediaPackage = (MediaPackage) mediaPackage.clone();
    // The list of elements to keep
    List<MediaPackageElement> keep = new ArrayList<>();
    SimpleElementSelector selector = new SimpleElementSelector();
    // Filter elements
    for (MediaPackageElementFlavor flavor : flavors) {
        selector.addFlavor(flavor);
    }
    for (String tag : tags) {
        selector.addTag(tag);
    }
    keep.addAll(selector.select(mediaPackage, true));
    // Keep publications
    for (Publication p : filteredMediaPackage.getPublications()) keep.add(p);
    // Fix references and flavors
    for (MediaPackageElement element : filteredMediaPackage.getElements()) {
        if (!keep.contains(element)) {
            logger.debug("Removing {} '{}' from media package '{}'", element.getElementType().toString().toLowerCase(), element.getIdentifier(), filteredMediaPackage.getIdentifier().toString());
            filteredMediaPackage.remove(element);
            continue;
        }
        // Is the element referencing anything?
        MediaPackageReference reference = element.getReference();
        if (reference != null) {
            Map<String, String> referenceProperties = reference.getProperties();
            MediaPackageElement referencedElement = mediaPackage.getElementByReference(reference);
            // if we are distributing the referenced element, everything is fine. Otherwise...
            if (referencedElement != null && !keep.contains(referencedElement)) {
                // Follow the references until we find a flavor
                MediaPackageElement parent = null;
                while ((parent = mediaPackage.getElementByReference(reference)) != null) {
                    if (parent.getFlavor() != null && element.getFlavor() == null) {
                        element.setFlavor(parent.getFlavor());
                    }
                    if (parent.getReference() == null)
                        break;
                    reference = parent.getReference();
                }
                // Done. Let's cut the path but keep references to the mediapackage itself
                if (reference != null && reference.getType().equals(MediaPackageReference.TYPE_MEDIAPACKAGE))
                    element.setReference(reference);
                else if (reference != null && (referenceProperties == null || referenceProperties.size() == 0))
                    element.clearReference();
                else {
                    // Ok, there is more to that reference than just pointing at an element. Let's keep the original,
                    // you never know.
                    referencedElement.setURI(null);
                    referencedElement.setChecksum(null);
                }
            }
        }
    }
    return filteredMediaPackage;
}
Also used : MediaPackageReference(org.opencastproject.mediapackage.MediaPackageReference) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 68 with MediaPackageElement

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

the class YouTubeV3PublicationServiceImpl method publish.

/**
 * Publishes the element to the publication channel and returns a reference to the published version of the element.
 *
 * @param job
 *          the associated job
 * @param mediaPackage
 *          the mediapackage
 * @param elementId
 *          the mediapackage element id to publish
 * @return the published element
 * @throws PublicationException
 *           if publication fails
 */
private Publication publish(final Job job, final MediaPackage mediaPackage, final String elementId) throws PublicationException {
    if (mediaPackage == null) {
        throw new IllegalArgumentException("Mediapackage must be specified");
    } else if (elementId == null) {
        throw new IllegalArgumentException("Mediapackage ID must be specified");
    }
    final MediaPackageElement element = mediaPackage.getElementById(elementId);
    if (element == null) {
        throw new IllegalArgumentException("Mediapackage element must be specified");
    }
    if (element.getIdentifier() == null) {
        throw new IllegalArgumentException("Mediapackage element must have an identifier");
    }
    if (element.getMimeType().toString().matches("text/xml")) {
        throw new IllegalArgumentException("Mediapackage element cannot be XML");
    }
    try {
        // create context strategy for publication
        final YouTubePublicationAdapter c = new YouTubePublicationAdapter(mediaPackage, workspace);
        final File file = workspace.get(element.getURI());
        final String episodeName = c.getEpisodeName();
        final UploadProgressListener operationProgressListener = new UploadProgressListener(mediaPackage, file);
        final String privacyStatus = makeVideosPrivate ? "private" : "public";
        final VideoUpload videoUpload = new VideoUpload(truncateTitleToMaxFieldLength(episodeName, false), c.getEpisodeDescription(), privacyStatus, file, operationProgressListener, tags);
        final Video video = youTubeService.addVideoToMyChannel(videoUpload);
        final int timeoutMinutes = 60;
        final long startUploadMilliseconds = new Date().getTime();
        while (!operationProgressListener.isComplete()) {
            Thread.sleep(POLL_MILLISECONDS);
            final long howLongWaitingMinutes = (new Date().getTime() - startUploadMilliseconds) / 60000;
            if (howLongWaitingMinutes > timeoutMinutes) {
                throw new PublicationException("Upload to YouTube exceeded " + timeoutMinutes + " minutes for episode " + episodeName);
            }
        }
        String playlistName = StringUtils.trimToNull(truncateTitleToMaxFieldLength(mediaPackage.getSeriesTitle(), true));
        playlistName = (playlistName == null) ? this.defaultPlaylist : playlistName;
        final Playlist playlist;
        final Playlist existingPlaylist = youTubeService.getMyPlaylistByTitle(playlistName);
        if (existingPlaylist == null) {
            playlist = youTubeService.createPlaylist(playlistName, c.getContextDescription(), mediaPackage.getSeries());
        } else {
            playlist = existingPlaylist;
        }
        youTubeService.addPlaylistItem(playlist.getId(), video.getId());
        // Create new publication element
        final URL url = new URL("http://www.youtube.com/watch?v=" + video.getId());
        return PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_NAME, url.toURI(), MimeTypes.parseMimeType(MIME_TYPE));
    } catch (Exception e) {
        logger.error("failed publishing to Youtube", e);
        logger.warn("Error publishing {}, {}", element, e.getMessage());
        if (e instanceof PublicationException) {
            throw (PublicationException) e;
        } else {
            throw new PublicationException("YouTube publish failed on job: " + ToStringBuilder.reflectionToString(job, ToStringStyle.MULTI_LINE_STYLE), e);
        }
    }
}
Also used : PublicationException(org.opencastproject.publication.api.PublicationException) Date(java.util.Date) URL(java.net.URL) PublicationException(org.opencastproject.publication.api.PublicationException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Playlist(com.google.api.services.youtube.model.Playlist) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Video(com.google.api.services.youtube.model.Video) File(java.io.File)

Example 69 with MediaPackageElement

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

the class CloneWorkflowOperationHandler method copyElement.

private MediaPackageElement copyElement(MediaPackageElement element) throws WorkflowOperationException {
    String elementId = UUID.randomUUID().toString();
    MediaPackageElement newElement = (MediaPackageElement) element.clone();
    newElement.setIdentifier(elementId);
    File sourceFile = null;
    String toFileName = null;
    try {
        URI sourceURI = element.getURI();
        sourceFile = workspace.get(sourceURI);
        toFileName = elementId;
        String extension = FilenameUtils.getExtension(sourceFile.getName());
        if (!"".equals(extension))
            toFileName += "." + extension;
        logger.debug("Start copying element {} to target {}.", sourceFile.getPath(), toFileName);
        URI newUri = workspace.put(element.getMediaPackage().getIdentifier().toString(), newElement.getIdentifier(), toFileName, workspace.read(sourceURI));
        newElement.setURI(newUri);
        newElement.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, workspace.get(newUri)));
        logger.debug("Element {} copied to target {}.", sourceFile.getPath(), toFileName);
    } catch (IOException e) {
        throw new WorkflowOperationException("Unable to copy " + sourceFile.getPath() + " to " + toFileName + ".", e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException("Unable to find " + element.getURI() + " in the workspace", e);
    }
    return newElement;
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI)

Example 70 with MediaPackageElement

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

the class DuplicateEventWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, final JobContext context) throws WorkflowOperationException {
    final MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    final WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    final String configuredSourceFlavors = trimToEmpty(operation.getConfiguration(SOURCE_FLAVORS_PROPERTY));
    final String configuredSourceTags = trimToEmpty(operation.getConfiguration(SOURCE_TAGS_PROPERTY));
    final String configuredTargetTags = trimToEmpty(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    final int numberOfEvents = Integer.parseInt(operation.getConfiguration(NUMBER_PROPERTY));
    final String configuredPropertyNamespaces = trimToEmpty(operation.getConfiguration(PROPERTY_NAMESPACES_PROPERTY));
    int maxNumberOfEvents = MAX_NUMBER_DEFAULT;
    if (operation.getConfiguration(MAX_NUMBER_PROPERTY) != null) {
        maxNumberOfEvents = Integer.parseInt(operation.getConfiguration(MAX_NUMBER_PROPERTY));
    }
    if (numberOfEvents > maxNumberOfEvents) {
        throw new WorkflowOperationException("Number of events to create exceeds the maximum of " + maxNumberOfEvents + ". Aborting.");
    }
    logger.info("Creating {} new media packages from media package with id {}.", numberOfEvents, mediaPackage.getIdentifier());
    final String[] sourceTags = split(configuredSourceTags, ",");
    final String[] targetTags = split(configuredTargetTags, ",");
    final String[] sourceFlavors = split(configuredSourceFlavors, ",");
    final String[] propertyNamespaces = split(configuredPropertyNamespaces, ",");
    final String copyNumberPrefix = trimToEmpty(operation.getConfiguration(COPY_NUMBER_PREFIX_PROPERTY));
    final SimpleElementSelector elementSelector = new SimpleElementSelector();
    for (String flavor : sourceFlavors) {
        elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    final List<String> removeTags = new ArrayList<>();
    final List<String> addTags = new ArrayList<>();
    final List<String> overrideTags = new ArrayList<>();
    for (String tag : targetTags) {
        if (tag.startsWith(MINUS)) {
            removeTags.add(tag);
        } else if (tag.startsWith(PLUS)) {
            addTags.add(tag);
        } else {
            overrideTags.add(tag);
        }
    }
    for (String tag : sourceTags) {
        elementSelector.addTag(tag);
    }
    // Filter elements to copy based on input tags and input flavors
    final Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, false);
    final Collection<Publication> internalPublications = new HashSet<>();
    for (MediaPackageElement e : mediaPackage.getElements()) {
        if (e instanceof Publication && InternalPublicationChannel.CHANNEL_ID.equals(((Publication) e).getChannel())) {
            internalPublications.add((Publication) e);
        }
        if (MediaPackageElements.EPISODE.equals(e.getFlavor())) {
            // Remove episode DC since we will add a new one (with changed title)
            elements.remove(e);
        }
    }
    final MediaPackageElement[] originalEpisodeDc = mediaPackage.getElementsByFlavor(MediaPackageElements.EPISODE);
    if (originalEpisodeDc.length != 1) {
        throw new WorkflowOperationException("Media package " + mediaPackage.getIdentifier() + " has " + originalEpisodeDc.length + " episode dublin cores while it is expected to have exactly 1. Aborting.");
    }
    for (int i = 0; i < numberOfEvents; i++) {
        final List<URI> temporaryFiles = new ArrayList<>();
        MediaPackage newMp = null;
        try {
            // Clone the media package (without its elements)
            newMp = copyMediaPackage(mediaPackage, i + 1, copyNumberPrefix);
            // Create and add new episode dublin core with changed title
            newMp = copyDublinCore(mediaPackage, originalEpisodeDc[0], newMp, removeTags, addTags, overrideTags, temporaryFiles);
            // Clone regular elements
            for (final MediaPackageElement e : elements) {
                final MediaPackageElement element = (MediaPackageElement) e.clone();
                updateTags(element, removeTags, addTags, overrideTags);
                newMp.add(element);
            }
            // Clone internal publications
            for (final Publication originalPub : internalPublications) {
                copyPublication(originalPub, mediaPackage, newMp, removeTags, addTags, overrideTags, temporaryFiles);
            }
            assetManager.takeSnapshot(AssetManager.DEFAULT_OWNER, newMp);
            // Clone properties of media package
            for (String namespace : propertyNamespaces) {
                copyProperties(namespace, mediaPackage, newMp);
            }
        } finally {
            cleanup(temporaryFiles, Optional.ofNullable(newMp));
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) URI(java.net.URI) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) HashSet(java.util.HashSet)

Aggregations

MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)153 MediaPackage (org.opencastproject.mediapackage.MediaPackage)72 NotFoundException (org.opencastproject.util.NotFoundException)50 Job (org.opencastproject.job.api.Job)49 URI (java.net.URI)48 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)39 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)39 Test (org.junit.Test)36 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)27 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)25 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)24 File (java.io.File)21 Track (org.opencastproject.mediapackage.Track)21 DistributionException (org.opencastproject.distribution.api.DistributionException)20 InputStream (java.io.InputStream)19 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 HashSet (java.util.HashSet)18 URISyntaxException (java.net.URISyntaxException)16 Path (javax.ws.rs.Path)16