use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.
the class OaiPmhRepositoryTest method searchResultItem.
private SearchResultItem searchResultItem(String id, Date modified, boolean deleted) {
final String seriesDcXml = IoSupport.loadFileFromClassPathAsString("/series-dublincore.xml").get();
final String episodeDcXml = IoSupport.loadFileFromClassPathAsString("/episode-dublincore.xml").get();
final DublinCoreCatalog seriesDc = DublinCores.read(IOUtils.toInputStream(seriesDcXml));
final DublinCoreCatalog episodeDc = DublinCores.read(IOUtils.toInputStream(episodeDcXml));
final String mpXml = IoSupport.loadFileFromClassPathAsString("/manifest-full.xml").get();
final String xacml = IoSupport.loadFileFromClassPathAsString("/xacml.xml").get();
//
SearchResultItem item = EasyMock.createNiceMock(SearchResultItem.class);
EasyMock.expect(item.getModificationDate()).andReturn(modified).anyTimes();
EasyMock.expect(item.getId()).andReturn(id).anyTimes();
EasyMock.expect(item.isDeleted()).andReturn(deleted).anyTimes();
EasyMock.expect(item.getMediaPackageXml()).andReturn(mpXml).anyTimes();
SearchResultElementItem episodeDcElement = EasyMock.createNiceMock(SearchResultElementItem.class);
EasyMock.expect(episodeDcElement.getType()).andReturn("catalog").anyTimes();
EasyMock.expect(episodeDcElement.getFlavor()).andReturn("dublincore/episode").anyTimes();
EasyMock.expect(episodeDcElement.getXml()).andReturn(episodeDcXml).anyTimes();
EasyMock.expect(episodeDcElement.isEpisodeDublinCore()).andReturn(true).anyTimes();
EasyMock.expect(episodeDcElement.isSeriesDublinCore()).andReturn(false).anyTimes();
try {
EasyMock.expect(episodeDcElement.asDublinCore()).andReturn(episodeDc).anyTimes();
} catch (OaiPmhDatabaseException ex) {
}
SearchResultElementItem seriesDcElement = EasyMock.createNiceMock(SearchResultElementItem.class);
EasyMock.expect(seriesDcElement.getType()).andReturn("catalog").anyTimes();
EasyMock.expect(seriesDcElement.getFlavor()).andReturn("dublincore/series").anyTimes();
EasyMock.expect(seriesDcElement.getXml()).andReturn(seriesDcXml).anyTimes();
EasyMock.expect(seriesDcElement.isEpisodeDublinCore()).andReturn(false).anyTimes();
EasyMock.expect(seriesDcElement.isSeriesDublinCore()).andReturn(true).anyTimes();
try {
EasyMock.expect(seriesDcElement.asDublinCore()).andReturn(seriesDc).anyTimes();
} catch (OaiPmhDatabaseException ex) {
}
SearchResultElementItem securityXacmlElement = EasyMock.createNiceMock(SearchResultElementItem.class);
EasyMock.expect(securityXacmlElement.getType()).andReturn("catalog").anyTimes();
EasyMock.expect(securityXacmlElement.getFlavor()).andReturn("security/xacml+series").anyTimes();
EasyMock.expect(securityXacmlElement.getXml()).andReturn(xacml).anyTimes();
EasyMock.expect(securityXacmlElement.isEpisodeDublinCore()).andReturn(false).anyTimes();
EasyMock.expect(securityXacmlElement.isSeriesDublinCore()).andReturn(false).anyTimes();
try {
EasyMock.expect(securityXacmlElement.asDublinCore()).andThrow(new OaiPmhDatabaseException("this is not a dublincore catalog")).anyTimes();
} catch (OaiPmhDatabaseException ex) {
}
EasyMock.expect(item.getElements()).andReturn(Collections.list(episodeDcElement, seriesDcElement, securityXacmlElement)).anyTimes();
try {
EasyMock.expect(item.getEpisodeDublinCore()).andReturn(episodeDc).anyTimes();
EasyMock.expect(item.getSeriesDublinCore()).andReturn(seriesDc).anyTimes();
} catch (OaiPmhDatabaseException ex) {
}
EasyMock.replay(item, episodeDcElement, seriesDcElement, securityXacmlElement);
return item;
}
use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandler method copyDublinCore.
private MediaPackage copyDublinCore(MediaPackage source, MediaPackageElement sourceDublinCore, MediaPackage destination, List<String> removeTags, List<String> addTags, List<String> overrideTags, List<URI> temporaryFiles) throws WorkflowOperationException {
final DublinCoreCatalog destinationDublinCore = DublinCoreUtil.loadEpisodeDublinCore(workspace, source).get();
destinationDublinCore.setIdentifier(null);
destinationDublinCore.setURI(sourceDublinCore.getURI());
destinationDublinCore.set(DublinCore.PROPERTY_TITLE, destination.getTitle());
try (InputStream inputStream = IOUtils.toInputStream(destinationDublinCore.toXmlString(), "UTF-8")) {
final String elementId = UUID.randomUUID().toString();
final URI newUrl = workspace.put(destination.getIdentifier().compact(), elementId, "dublincore.xml", inputStream);
temporaryFiles.add(newUrl);
final MediaPackageElement mpe = destination.add(newUrl, MediaPackageElement.Type.Catalog, MediaPackageElements.EPISODE);
mpe.setIdentifier(elementId);
for (String tag : sourceDublinCore.getTags()) {
mpe.addTag(tag);
}
updateTags(mpe, removeTags, addTags, overrideTags);
} catch (IOException e) {
throw new WorkflowOperationException(e);
}
return destination;
}
use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.
the class SeriesWorkflowOperationHandler 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 series workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
Opt<String> optSeries = getOptConfig(workflowInstance.getCurrentOperation(), SERIES_PROPERTY);
Opt<String> optAttachFlavors = getOptConfig(workflowInstance.getCurrentOperation(), ATTACH_PROPERTY);
Boolean applyAcl = getOptConfig(workflowInstance.getCurrentOperation(), APPLY_ACL_PROPERTY).map(toBoolean).getOr(false);
Opt<String> optCopyMetadata = getOptConfig(workflowInstance.getCurrentOperation(), COPY_METADATA_PROPERTY);
String defaultNamespace = getOptConfig(workflowInstance.getCurrentOperation(), DEFAULT_NS_PROPERTY).getOr(DublinCore.TERMS_NS_URI);
logger.debug("Using default namespace: '{}'", defaultNamespace);
if (optSeries.isSome() && !optSeries.get().equals(mediaPackage.getSeries())) {
logger.info("Changing series id from '{}' to '{}'", StringUtils.trimToEmpty(mediaPackage.getSeries()), optSeries.get());
mediaPackage.setSeries(optSeries.get());
}
String seriesId = mediaPackage.getSeries();
if (seriesId == null) {
logger.info("No series set, skip operation");
return createResult(mediaPackage, Action.SKIP);
}
DublinCoreCatalog series;
try {
series = seriesService.getSeries(seriesId);
} catch (NotFoundException e) {
logger.info("No series with the identifier '{}' found, skip operation", seriesId);
return createResult(mediaPackage, Action.SKIP);
} catch (UnauthorizedException e) {
logger.warn("Not authorized to get series with identifier '{}' found, skip operation", seriesId);
return createResult(mediaPackage, Action.SKIP);
} catch (SeriesException e) {
logger.error("Unable to get series with identifier '{}', skip operation: {}", seriesId, ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException(e);
}
mediaPackage.setSeriesTitle(series.getFirst(DublinCore.PROPERTY_TITLE));
// Process extra metadata
HashSet<EName> extraMetadata = new HashSet<>();
if (optCopyMetadata.isSome()) {
for (String strEName : optCopyMetadata.get().split(",+\\s*")) try {
if (!strEName.isEmpty()) {
extraMetadata.add(EName.fromString(strEName, defaultNamespace));
}
} catch (IllegalArgumentException iae) {
logger.warn("Ignoring incorrect dublincore metadata property: '{}'", strEName);
}
}
// Update the episode catalog
for (Catalog episodeCatalog : mediaPackage.getCatalogs(MediaPackageElements.EPISODE)) {
DublinCoreCatalog episodeDublinCore = DublinCoreUtil.loadDublinCore(workspace, episodeCatalog);
// Make sure the MP catalog has bindings defined
episodeDublinCore.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(DublinCore.TERMS_NS_PREFIX, DublinCore.TERMS_NS_URI)));
episodeDublinCore.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(DublinCore.ELEMENTS_1_1_NS_PREFIX, DublinCore.ELEMENTS_1_1_NS_URI)));
episodeDublinCore.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(DublinCores.OC_PROPERTY_NS_PREFIX, DublinCores.OC_PROPERTY_NS_URI)));
episodeDublinCore.set(DublinCore.PROPERTY_IS_PART_OF, seriesId);
for (EName property : extraMetadata) {
if (!episodeDublinCore.hasValue(property) && series.hasValue(property)) {
episodeDublinCore.set(property, series.get(property));
}
}
try (InputStream in = IOUtils.toInputStream(episodeDublinCore.toXmlString(), "UTF-8")) {
String filename = FilenameUtils.getName(episodeCatalog.getURI().toString());
URI uri = workspace.put(mediaPackage.getIdentifier().toString(), episodeCatalog.getIdentifier(), filename, in);
episodeCatalog.setURI(uri);
// setting the URI to a new source so the checksum will most like be invalid
episodeCatalog.setChecksum(null);
} catch (Exception e) {
logger.error("Unable to update episode catalog isPartOf field: {}", ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
// Attach series catalogs
if (optAttachFlavors.isSome()) {
// Remove existing series catalogs
AbstractMediaPackageElementSelector<Catalog> catalogSelector = new CatalogSelector();
String[] seriesFlavors = StringUtils.split(optAttachFlavors.get(), ",");
for (String flavor : seriesFlavors) {
if ("*".equals(flavor)) {
catalogSelector.addFlavor("*/*");
} else {
catalogSelector.addFlavor(flavor);
}
}
for (Catalog c : catalogSelector.select(mediaPackage, false)) {
if (MediaPackageElements.SERIES.equals(c.getFlavor()) || "series".equals(c.getFlavor().getSubtype())) {
mediaPackage.remove(c);
}
}
List<SeriesCatalogUIAdapter> adapters = getSeriesCatalogUIAdapters();
for (String flavorString : seriesFlavors) {
MediaPackageElementFlavor flavor;
if ("*".equals(flavorString)) {
flavor = MediaPackageElementFlavor.parseFlavor("*/*");
} else {
flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
}
for (SeriesCatalogUIAdapter a : adapters) {
MediaPackageElementFlavor adapterFlavor = MediaPackageElementFlavor.parseFlavor(a.getFlavor());
if (flavor.matches(adapterFlavor)) {
if (MediaPackageElements.SERIES.eq(a.getFlavor())) {
addDublinCoreCatalog(series, MediaPackageElements.SERIES, mediaPackage);
} else {
try {
Opt<byte[]> seriesElementData = seriesService.getSeriesElementData(seriesId, adapterFlavor.getType());
if (seriesElementData.isSome()) {
DublinCoreCatalog catalog = DublinCores.read(new ByteArrayInputStream(seriesElementData.get()));
addDublinCoreCatalog(catalog, adapterFlavor, mediaPackage);
} else {
logger.warn("No extended series catalog found for flavor '{}' and series '{}', skip adding catalog", adapterFlavor.getType(), seriesId);
}
} catch (SeriesException e) {
logger.error("Unable to load extended series metadata for flavor {}", adapterFlavor.getType());
throw new WorkflowOperationException(e);
}
}
}
}
}
}
if (applyAcl) {
try {
AccessControlList acl = seriesService.getSeriesAccessControl(seriesId);
if (acl != null)
authorizationService.setAcl(mediaPackage, AclScope.Series, acl);
} catch (Exception e) {
logger.error("Unable to update series ACL: {}", ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
return createResult(mediaPackage, Action.CONTINUE);
}
use of org.opencastproject.metadata.dublincore.DublinCoreCatalog 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);
}
use of org.opencastproject.metadata.dublincore.DublinCoreCatalog 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);
}
Aggregations