use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class CopyWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Running copy workflow operation on workflow {}", workflowInstance.getId());
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
// Check which tags have been configured
String sourceTagsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_TAGS));
String sourceFlavorsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_FLAVORS));
String targetDirectoryOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_DIRECTORY));
Option<String> targetFilenameOption = Option.option(StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_FILENAME)));
StringBuilder sb = new StringBuilder();
sb.append("Parameters passed to copy workflow operation:");
sb.append("\n source-tags: ").append(sourceTagsOption);
sb.append("\n source-flavors: ").append(sourceFlavorsOption);
sb.append("\n target-directory: ").append(targetDirectoryOption);
sb.append("\n target-filename: ").append(targetFilenameOption);
logger.debug(sb.toString());
AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector = new SimpleElementSelector();
// Make sure either one of tags or flavors are provided
if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorsOption)) {
logger.info("No source tags or flavors have been specified, not matching anything");
return createResult(mediaPackage, Action.CONTINUE);
}
// Make the target filename and directory are provided
if (StringUtils.isBlank(targetDirectoryOption))
throw new WorkflowOperationException("No target directory has been set for the copy operation!");
// Select the source flavors
for (String flavor : asList(sourceFlavorsOption)) {
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
// Select the source tags
for (String tag : asList(sourceTagsOption)) {
elementSelector.addTag(tag);
}
// Look for elements matching the tag
Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
// Check the the number of element returned
if (elements.size() == 0) {
// If no one found, we skip the operation
return createResult(workflowInstance.getMediaPackage(), Action.SKIP);
} else if (elements.size() == 1) {
for (MediaPackageElement element : elements) {
logger.debug("Copy single element to: {}", targetDirectoryOption);
final String fileName;
if (targetFilenameOption.isSome()) {
fileName = targetFilenameOption.get();
} else {
fileName = FilenameUtils.getBaseName(element.getURI().toString());
}
String ext = FilenameUtils.getExtension(element.getURI().toString());
ext = ext.length() > 0 ? ".".concat(ext) : "";
File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName.concat(ext)));
copyElement(element, targetFile);
}
} else {
logger.debug("Copy multiple elements to: {}", targetDirectoryOption);
int i = 1;
for (MediaPackageElement element : elements) {
final String fileName;
if (targetFilenameOption.isSome()) {
fileName = String.format(targetFilenameOption.get(), i);
} else {
fileName = FilenameUtils.getBaseName(element.getURI().toString());
}
String ext = FilenameUtils.getExtension(element.getURI().toString());
ext = ext.length() > 0 ? ".".concat(ext) : "";
File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName + ext));
copyElement(element, targetFile);
i++;
}
}
return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class CopyWorkflowOperationHandler method copyElement.
private void copyElement(MediaPackageElement element, File targetFile) throws WorkflowOperationException {
File sourceFile;
try {
sourceFile = workspace.get(element.getURI());
} catch (NotFoundException e) {
throw new WorkflowOperationException("Unable to find " + element.getURI() + " in the workspace", e);
} catch (IOException e) {
throw new WorkflowOperationException("Error loading " + element.getURI() + " from the workspace", e);
}
logger.debug("Start copying element {} to target {}.", sourceFile.getPath(), targetFile.getPath());
try {
FileSupport.copy(sourceFile, targetFile);
} catch (IOException e) {
throw new WorkflowOperationException(format("Unable to copy %s to %s: %s", sourceFile.getPath(), targetFile.getPath(), e.getMessage()));
}
logger.debug("Element {} copied to target {}.", sourceFile.getPath(), targetFile.getPath());
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandler method copyPublication.
private void copyPublication(Publication sourcePublication, MediaPackage source, MediaPackage destination, List<String> removeTags, List<String> addTags, List<String> overrideTags, List<URI> temporaryFiles) throws WorkflowOperationException {
final String newPublicationId = UUID.randomUUID().toString();
final Publication newPublication = PublicationImpl.publication(newPublicationId, InternalPublicationChannel.CHANNEL_ID, null, null);
// re-distribute elements of publication to internal publication channel
final Collection<MediaPackageElement> sourcePubElements = new HashSet<>();
sourcePubElements.addAll(Arrays.asList(sourcePublication.getAttachments()));
sourcePubElements.addAll(Arrays.asList(sourcePublication.getCatalogs()));
sourcePubElements.addAll(Arrays.asList(sourcePublication.getTracks()));
for (final MediaPackageElement e : sourcePubElements) {
try {
// We first have to copy the media package element into the workspace
final MediaPackageElement element = (MediaPackageElement) e.clone();
try (InputStream inputStream = workspace.read(element.getURI())) {
final URI tmpUri = workspace.put(destination.getIdentifier().toString(), element.getIdentifier(), FilenameUtils.getName(element.getURI().toString()), inputStream);
temporaryFiles.add(tmpUri);
element.setIdentifier(null);
element.setURI(tmpUri);
}
// Now we can distribute it to the new media package
// Element has to be added before it can be distributed
destination.add(element);
final Job job = distributionService.distribute(InternalPublicationChannel.CHANNEL_ID, destination, element.getIdentifier());
final MediaPackageElement distributedElement = JobUtil.payloadAsMediaPackageElement(serviceRegistry).apply(job);
destination.remove(element);
updateTags(distributedElement, removeTags, addTags, overrideTags);
PublicationImpl.addElementToPublication(newPublication, distributedElement);
} catch (Exception exception) {
throw new WorkflowOperationException(exception);
}
}
// Using an altered copy of the source publication's URI is a bit hacky,
// but it works without knowing the URI pattern...
String publicationUri = sourcePublication.getURI().toString();
publicationUri = publicationUri.replace(source.getIdentifier().toString(), destination.getIdentifier().toString());
publicationUri = publicationUri.replace(sourcePublication.getIdentifier(), newPublicationId);
newPublication.setURI(URI.create(publicationUri));
destination.add(newPublication);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandler method copyMediaPackage.
private MediaPackage copyMediaPackage(MediaPackage source, long copyNumber, String copyNumberPrefix) throws WorkflowOperationException {
// We are not using MediaPackage.clone() here, since it does "too much" for us (e.g. copies all the attachments)
MediaPackage destination;
try {
destination = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
} catch (MediaPackageException e) {
logger.error("Failed to create media package " + e.getLocalizedMessage());
throw new WorkflowOperationException(e);
}
logger.info("Created mediapackage {}", destination);
destination.setDate(source.getDate());
destination.setSeries(source.getSeries());
destination.setSeriesTitle(source.getSeriesTitle());
destination.setDuration(source.getDuration());
destination.setLanguage(source.getLanguage());
destination.setLicense(source.getLicense());
destination.setTitle(source.getTitle() + " (" + copyNumberPrefix + " " + copyNumber + ")");
return destination;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ImportWorkflowPropertiesWOH method loadPropertiesFromXml.
static Properties loadPropertiesFromXml(Workspace workspace, URI uri) throws WorkflowOperationException {
final Properties properties = new Properties();
try {
File propertiesFile = workspace.get(uri);
try (InputStream is = new FileInputStream(propertiesFile)) {
properties.loadFromXML(is);
logger.debug("Properties loaded from {}", propertiesFile);
}
} catch (NotFoundException e) {
throw new WorkflowOperationException(e);
} catch (IOException e) {
throw new WorkflowOperationException(e);
}
return properties;
}
Aggregations