Search in sources :

Example 46 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class TagByDublinCoreTermWOH method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    String configuredSourceFlavors = StringUtils.trimToEmpty(currentOperation.getConfiguration(SOURCE_FLAVORS_PROPERTY));
    String configuredSourceTags = StringUtils.trimToEmpty(currentOperation.getConfiguration(SOURCE_TAGS_PROPERTY));
    String configuredCatalog = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCCATALOG_PROPERTY));
    String configuredDCTerm = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCTERM_PROPERTY));
    String configuredDefaultValue = StringUtils.trimToNull(currentOperation.getConfiguration(DEFAULT_VALUE_PROPERTY));
    String configuredMatchValue = StringUtils.trimToEmpty(currentOperation.getConfiguration(MATCH_VALUE_PROPERTY));
    String configuredTargetFlavor = StringUtils.trimToNull(currentOperation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String configuredTargetTags = StringUtils.trimToEmpty(currentOperation.getConfiguration(TARGET_TAGS_PROPERTY));
    boolean copy = BooleanUtils.toBoolean(currentOperation.getConfiguration(COPY_PROPERTY));
    String[] sourceTags = StringUtils.split(configuredSourceTags, ",");
    String[] targetTags = StringUtils.split(configuredTargetTags, ",");
    String[] sourceFlavors = StringUtils.split(configuredSourceFlavors, ",");
    SimpleElementSelector elementSelector = new SimpleElementSelector();
    for (String flavor : sourceFlavors) {
        elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceTags) {
        elementSelector.addTag(tag);
    }
    List<String> removeTags = new ArrayList<>();
    List<String> addTags = new ArrayList<>();
    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);
        }
    }
    // Find Catalog
    Catalog[] catalogs = mediaPackage.getCatalogs(new MediaPackageElementFlavor("dublincore", StringUtils.lowerCase(configuredCatalog)));
    if (catalogs != null && catalogs.length > 0) {
        Boolean foundValue = false;
        EName dcterm = new EName(TERMS_NS_URI, configuredDCTerm);
        // Find DCTerm
        for (Catalog catalog : catalogs) {
            DublinCoreCatalog dc = DublinCoreUtil.loadDublinCore(workspace, catalog);
            // Match Value
            List<DublinCoreValue> values = dc.get(dcterm);
            if (values.isEmpty()) {
                // Use default
                if (configuredDefaultValue != null) {
                    foundValue = configuredDefaultValue.equals(configuredMatchValue);
                }
            } else {
                foundValue = values.contains(DublinCoreValue.mk(configuredMatchValue));
            }
        }
        if (foundValue) {
            if (copy) {
                logger.info("Retagging mediapackage elements as a copy");
            } else {
                logger.info("Retagging mediapackage elements");
            }
            Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, false);
            for (MediaPackageElement e : elements) {
                MediaPackageElement element = e;
                if (copy) {
                    element = (MediaPackageElement) e.clone();
                    element.setIdentifier(null);
                    // use the same URI as the original
                    element.setURI(e.getURI());
                }
                if (configuredTargetFlavor != null) {
                    element.setFlavor(MediaPackageElementFlavor.parseFlavor(configuredTargetFlavor));
                }
                if (overrideTags.size() > 0) {
                    element.clearTags();
                    for (String tag : overrideTags) {
                        element.addTag(tag);
                    }
                } else {
                    for (String tag : removeTags) {
                        element.removeTag(tag.substring(MINUS.length()));
                    }
                    for (String tag : addTags) {
                        element.addTag(tag.substring(PLUS.length()));
                    }
                }
                if (copy) {
                    mediaPackage.addDerived(element, e);
                }
            }
        }
    // if foundValue
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : EName(org.opencastproject.mediapackage.EName) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) ArrayList(java.util.ArrayList) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 47 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class ZipWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    if (workflowInstance == null) {
        throw new WorkflowOperationException("Invalid workflow instance");
    }
    final MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    final WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    if (currentOperation == null) {
        throw new WorkflowOperationException("Cannot get current workflow operation");
    }
    String flavors = currentOperation.getConfiguration(INCLUDE_FLAVORS_PROPERTY);
    final List<MediaPackageElementFlavor> flavorsToZip = new ArrayList<MediaPackageElementFlavor>();
    MediaPackageElementFlavor targetFlavor = DEFAULT_ARCHIVE_FLAVOR;
    // Read the target flavor
    String targetFlavorOption = currentOperation.getConfiguration(TARGET_FLAVOR_PROPERTY);
    try {
        targetFlavor = targetFlavorOption == null ? DEFAULT_ARCHIVE_FLAVOR : MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
        logger.trace("Using '{}' as the target flavor for the zip archive of recording {}", targetFlavor, mediaPackage);
    } catch (IllegalArgumentException e) {
        throw new WorkflowOperationException("Flavor '" + targetFlavorOption + "' is not valid", e);
    }
    // Read the target tags
    String targetTagsOption = StringUtils.trimToEmpty(currentOperation.getConfiguration(TARGET_TAGS_PROPERTY));
    String[] targetTags = StringUtils.split(targetTagsOption, ",");
    // If the configuration does not specify flavors, just zip them all
    if (flavors == null) {
        flavorsToZip.add(MediaPackageElementFlavor.parseFlavor("*/*"));
    } else {
        for (String flavor : asList(flavors)) {
            flavorsToZip.add(MediaPackageElementFlavor.parseFlavor(flavor));
        }
    }
    logger.info("Archiving mediapackage {} in workflow {}", mediaPackage, workflowInstance);
    String compressProperty = currentOperation.getConfiguration(COMPRESS_PROPERTY);
    boolean compress = compressProperty == null ? false : Boolean.valueOf(compressProperty);
    // Zip the contents of the mediapackage
    File zip = null;
    try {
        logger.info("Creating zipped archive of recording {}", mediaPackage);
        zip = zip(mediaPackage, flavorsToZip, compress);
    } catch (Exception e) {
        throw new WorkflowOperationException("Unable to create a zip archive from mediapackage " + mediaPackage, e);
    }
    // Get the collection for storing the archived mediapackage
    String configuredCollectionId = currentOperation.getConfiguration(ZIP_COLLECTION_PROPERTY);
    String collectionId = configuredCollectionId == null ? DEFAULT_ZIP_COLLECTION : configuredCollectionId;
    // Add the zip as an attachment to the mediapackage
    logger.info("Moving zipped archive of recording {} to the working file repository collection '{}'", mediaPackage, collectionId);
    InputStream in = null;
    URI uri = null;
    try {
        in = new FileInputStream(zip);
        uri = workspace.putInCollection(collectionId, mediaPackage.getIdentifier().compact() + ".zip", in);
        logger.info("Zipped archive of recording {} is available from {}", mediaPackage, uri);
    } catch (FileNotFoundException e) {
        throw new WorkflowOperationException("zip file " + zip + " not found", e);
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    Attachment attachment = (Attachment) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(uri, Type.Attachment, targetFlavor);
    try {
        attachment.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, zip));
    } catch (IOException e) {
        throw new WorkflowOperationException(e);
    }
    attachment.setMimeType(MimeTypes.ZIP);
    // Apply the target tags
    for (String tag : targetTags) {
        attachment.addTag(tag);
        logger.trace("Tagging the archive of recording '{}' with '{}'", mediaPackage, tag);
    }
    attachment.setMimeType(MimeTypes.ZIP);
    // The zip file is safely in the archive, so it's now safe to attempt to remove the original zip
    try {
        FileUtils.forceDelete(zip);
    } catch (Exception e) {
        throw new WorkflowOperationException(e);
    }
    mediaPackage.add(attachment);
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) File(java.io.File)

Example 48 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class CleanupWorkflowOperationHandlerTest method createWorkflowInstance.

private WorkflowInstance createWorkflowInstance(Map<String, String> configuration, MediaPackage mp) {
    WorkflowOperationInstance wfOpInst = new WorkflowOperationInstanceImpl();
    if (configuration != null) {
        for (String confKey : configuration.keySet()) {
            wfOpInst.setConfiguration(confKey, configuration.get(confKey));
        }
    }
    wfOpInst.setId(1L);
    wfOpInst.setState(WorkflowOperationInstance.OperationState.RUNNING);
    WorkflowInstance wfInst = EasyMock.createNiceMock(WorkflowInstance.class);
    EasyMock.expect(wfInst.getMediaPackage()).andReturn(mp).anyTimes();
    EasyMock.expect(wfInst.getCurrentOperation()).andReturn(wfOpInst).anyTimes();
    EasyMock.expect(wfInst.getOperations()).andReturn(Arrays.asList(wfOpInst)).anyTimes();
    EasyMock.replay(wfInst);
    return wfInst;
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Example 49 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class CleanupWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    cleanUpJobArgument(workflowInstance);
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    String flavors = currentOperation.getConfiguration(PRESERVE_FLAVOR_PROPERTY);
    final List<MediaPackageElementFlavor> flavorsToPreserve = new ArrayList<MediaPackageElementFlavor>();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String delayStr = currentOperation.getConfiguration(DELAY);
    int delay = 1;
    if (delayStr != null) {
        try {
            delay = Integer.parseInt(delayStr);
        } catch (NumberFormatException e) {
            logger.warn("Invalid value '{}' for delay in workflow operation configuration (should be integer)", delayStr);
        }
    }
    if (delay > 0) {
        try {
            logger.debug("Sleeping {}s before removing workflow files", delay);
            Thread.sleep(delay * 1000);
        } catch (InterruptedException e) {
        // ignore
        }
    }
    // If the configuration does not specify flavors, remove them all
    for (String flavor : asList(flavors)) flavorsToPreserve.add(MediaPackageElementFlavor.parseFlavor(flavor));
    List<MediaPackageElement> elementsToRemove = new ArrayList<>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (!isPreserved(element, flavorsToPreserve))
            elementsToRemove.add(element);
    }
    List<String> externalBaseUrls = null;
    if (deleteExternal) {
        externalBaseUrls = getAllWorkingFileRepositoryUrls();
        externalBaseUrls.remove(workspace.getBaseUri().toString());
    }
    for (MediaPackageElement elementToRemove : elementsToRemove) {
        if (deleteExternal) {
            // cleanup external working file repositories
            for (String repository : externalBaseUrls) {
                logger.debug("Removing {} from repository {}", elementToRemove.getURI(), repository);
                try {
                    removeElementFromRepository(elementToRemove, repository);
                } catch (TrustedHttpClientException ex) {
                    logger.debug("Removing media package element {} from repository {} failed: {}", elementToRemove.getURI(), repository, ex.getMessage());
                }
            }
        }
        // cleanup workspace and also the internal working file repository
        logger.debug("Removing {} from the workspace", elementToRemove.getURI());
        try {
            mediaPackage.remove(elementToRemove);
            workspace.delete(elementToRemove.getURI());
        } catch (NotFoundException ex) {
            logger.debug("Workspace doesn't contain element with Id '{}' from media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
        } catch (IOException ex) {
            logger.warn("Unable to remove element with Id '{}' from the media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Example 50 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class ConfigureByDublinCoreTermWOH method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    String configuredCatalog = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCCATALOG_PROPERTY));
    String configuredDCTerm = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCTERM_PROPERTY));
    String configuredDefaultValue = StringUtils.trimToNull(currentOperation.getConfiguration(DEFAULT_VALUE_PROPERTY));
    String configuredMatchValue = StringUtils.trimToEmpty(currentOperation.getConfiguration(MATCH_VALUE_PROPERTY));
    // Find Catalog
    Catalog[] catalogs = mediaPackage.getCatalogs(new MediaPackageElementFlavor("dublincore", StringUtils.lowerCase(configuredCatalog)));
    if (catalogs != null && catalogs.length > 0) {
        Boolean foundValue = false;
        EName dcterm = new EName(TERMS_NS_URI, configuredDCTerm);
        // Find DCTerm
        for (Catalog catalog : catalogs) {
            DublinCoreCatalog dc = DublinCoreUtil.loadDublinCore(workspace, catalog);
            // Match Value
            List<DublinCoreValue> values = dc.get(dcterm);
            if (values.isEmpty()) {
                // Use default
                if (configuredDefaultValue != null) {
                    foundValue = configuredDefaultValue.equals(configuredMatchValue);
                }
            } else {
                foundValue = values.contains(DublinCoreValue.mk(configuredMatchValue));
            }
        }
        if (foundValue) {
            Map<String, String> properties = new HashMap<>();
            for (String key : currentOperation.getConfigurationKeys()) {
                // Ignore this operations configuration
                if (DCCATALOG_PROPERTY.equals(key) || DCTERM_PROPERTY.equals(key) || DEFAULT_VALUE_PROPERTY.equals(key) || MATCH_VALUE_PROPERTY.equals(key)) {
                    continue;
                }
                String value = currentOperation.getConfiguration(key);
                properties.put(key, value);
                logger.info("Configuration key '{}' of workflow {} is set to value '{}'", key, workflowInstance.getId(), value);
            }
            return createResult(mediaPackage, properties, Action.CONTINUE, 0);
        }
    // if foundValue
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : EName(org.opencastproject.mediapackage.EName) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) HashMap(java.util.HashMap) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Aggregations

WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)104 ArrayList (java.util.ArrayList)51 MediaPackage (org.opencastproject.mediapackage.MediaPackage)48 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)37 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)33 Test (org.junit.Test)32 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)31 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)28 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)24 Job (org.opencastproject.job.api.Job)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)19 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)19 URI (java.net.URI)18 NotFoundException (org.opencastproject.util.NotFoundException)16 Track (org.opencastproject.mediapackage.Track)14 IOException (java.io.IOException)13 File (java.io.File)12 HashMap (java.util.HashMap)12 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10