Search in sources :

Example 1 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class SimpleElementSelectorTest method setUp.

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    selector = new SimpleElementSelector();
    selector.addFlavor(PRESENTATION_SOURCE);
    selector.addFlavor(PRESENTER_SOURCE);
    setUpPreliminaries();
}
Also used : SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) Before(org.junit.Before)

Example 2 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class OaiPmhUpdatedEventHandler method handleEvent.

public void handleEvent(AssetManagerItem.TakeSnapshot snapshotItem) {
    if (!propagateEpisode) {
        logger.trace("Skipping automatic propagation of episode meta data to OAI-PMH since it is turned off.");
        return;
    }
    // An episode or its ACL has been updated. Construct the MediaPackage and publish it to OAI-PMH.
    logger.debug("Handling update event for media package {}", snapshotItem.getMediapackage().getIdentifier().compact());
    // We must be an administrative user to make a query to the OaiPmhPublicationService
    final User prevUser = securityService.getUser();
    final Organization prevOrg = securityService.getOrganization();
    try {
        securityService.setUser(SecurityUtil.createSystemUser(systemAccount, prevOrg));
        // Check weather the media package contains elements to republish
        MediaPackage snapshotMp = snapshotItem.getMediapackage();
        SimpleElementSelector mpeSelector = new SimpleElementSelector();
        for (String flavor : flavors) {
            mpeSelector.addFlavor(flavor);
        }
        for (String tag : tags) {
            mpeSelector.addTag(tag);
        }
        Collection<MediaPackageElement> elementsToUpdate = mpeSelector.select(snapshotMp, true);
        if (elementsToUpdate == null || elementsToUpdate.isEmpty()) {
            logger.debug("The media package {} does not contain any elements matching the given flavors and tags", snapshotMp.getIdentifier().compact());
            return;
        }
        SearchResult result = oaiPmhPersistence.search(QueryBuilder.query().mediaPackageId(snapshotMp).isDeleted(false).build());
        for (SearchResultItem searchResultItem : result.getItems()) {
            try {
                Job job = oaiPmhPublicationService.updateMetadata(snapshotMp, searchResultItem.getRepository(), flavors, tags, false);
            // we don't want to wait for job completion here because it will block the message queue
            } catch (Exception e) {
                logger.error("Unable to update OAI-PMH publication for the media package {} in repository {}", snapshotItem.getMediapackage().getIdentifier().compact(), searchResultItem.getRepository(), e);
            }
        }
    } finally {
        securityService.setOrganization(prevOrg);
        securityService.setUser(prevUser);
    }
}
Also used : User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) Job(org.opencastproject.job.api.Job) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 3 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class ConfigurablePublishWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    RequireUtil.notNull(workflowInstance, "workflowInstance");
    final MediaPackage mp = workflowInstance.getMediaPackage();
    final WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
    final String channelId = StringUtils.trimToEmpty(op.getConfiguration(CHANNEL_ID_KEY));
    if ("".equals(channelId)) {
        throw new WorkflowOperationException("Unable to publish this mediapackage as the configuration key " + CHANNEL_ID_KEY + " is missing. Unable to determine where to publish these elements.");
    }
    final String urlPattern = StringUtils.trimToEmpty(op.getConfiguration(URL_PATTERN));
    MimeType mimetype = null;
    String mimetypeString = StringUtils.trimToEmpty(op.getConfiguration(MIME_TYPE));
    if (!"".equals(mimetypeString)) {
        try {
            mimetype = MimeTypes.parseMimeType(mimetypeString);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Unable to parse the provided configuration for " + MIME_TYPE, e);
        }
    }
    final boolean withPublishedElements = BooleanUtils.toBoolean(StringUtils.trimToEmpty(op.getConfiguration(WITH_PUBLISHED_ELEMENTS)));
    boolean checkAvailability = BooleanUtils.toBoolean(StringUtils.trimToEmpty(op.getConfiguration(CHECK_AVAILABILITY)));
    if (getPublications(mp, channelId).size() > 0) {
        final String rePublishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
        switch(rePublishStrategy) {
            case ("fail"):
                // fail is a dummy function for further distribution strategies
                fail(mp);
                break;
            case ("merge"):
                // nothing to do here. other publication strategies can be added to this list later on
                break;
            default:
                retract(mp, channelId);
        }
    }
    String mode = StringUtils.trimToEmpty(op.getConfiguration(MODE));
    if ("".equals(mode)) {
        mode = DEFAULT_MODE;
    } else if (!ArrayUtils.contains(KNOWN_MODES, mode)) {
        logger.error("Unknown value for configuration key mode: '{}'", mode);
        throw new IllegalArgumentException("Unknown value for configuration key mode");
    }
    final String[] sourceFlavors = StringUtils.split(StringUtils.trimToEmpty(op.getConfiguration(SOURCE_FLAVORS)), ",");
    final String[] sourceTags = StringUtils.split(StringUtils.trimToEmpty(op.getConfiguration(SOURCE_TAGS)), ",");
    String publicationUUID = UUID.randomUUID().toString();
    Publication publication = PublicationImpl.publication(publicationUUID, channelId, null, null);
    // Configure the element selector
    final SimpleElementSelector selector = new SimpleElementSelector();
    for (String flavor : sourceFlavors) {
        selector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceTags) {
        selector.addTag(tag);
    }
    if (sourceFlavors.length > 0 || sourceTags.length > 0) {
        if (!withPublishedElements) {
            Set<MediaPackageElement> elements = distribute(selector.select(mp, false), mp, channelId, mode, checkAvailability);
            if (elements.size() > 0) {
                for (MediaPackageElement element : elements) {
                    // Make sure the mediapackage is prompted to create a new identifier for this element
                    element.setIdentifier(null);
                    PublicationImpl.addElementToPublication(publication, element);
                }
            } else {
                logger.info("No element found for distribution in media package '{}'", mp);
                return createResult(mp, Action.CONTINUE);
            }
        } else {
            List<MediaPackageElement> publishedElements = new ArrayList<>();
            for (Publication alreadyPublished : mp.getPublications()) {
                publishedElements.addAll(Arrays.asList(alreadyPublished.getAttachments()));
                publishedElements.addAll(Arrays.asList(alreadyPublished.getCatalogs()));
                publishedElements.addAll(Arrays.asList(alreadyPublished.getTracks()));
            }
            for (MediaPackageElement element : selector.select(publishedElements, false)) {
                PublicationImpl.addElementToPublication(publication, element);
            }
        }
    }
    if (!"".equals(urlPattern)) {
        publication.setURI(populateUrlWithVariables(urlPattern, mp, publicationUUID));
    }
    if (mimetype != null) {
        publication.setMimeType(mimetype);
    }
    mp.add(publication);
    return createResult(mp, Action.CONTINUE);
}
Also used : ArrayList(java.util.ArrayList) Publication(org.opencastproject.mediapackage.Publication) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MimeType(org.opencastproject.util.MimeType) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 4 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector 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 5 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector 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

SimpleElementSelector (org.opencastproject.mediapackage.selector.SimpleElementSelector)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)15 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)15 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)8 ArrayList (java.util.ArrayList)7 Publication (org.opencastproject.mediapackage.Publication)7 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)7 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)5 Job (org.opencastproject.job.api.Job)4 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)4 URI (java.net.URI)3 HashSet (java.util.HashSet)3 MalformedURLException (java.net.MalformedURLException)2 Hashtable (java.util.Hashtable)2 Test (org.junit.Test)2 Attachment (org.opencastproject.mediapackage.Attachment)2 Catalog (org.opencastproject.mediapackage.Catalog)2 MediaPackageBuilder (org.opencastproject.mediapackage.MediaPackageBuilder)2 MediaPackageReference (org.opencastproject.mediapackage.MediaPackageReference)2 Track (org.opencastproject.mediapackage.Track)2