TagByDublinCoreTermWOH extends ResumableWorkflowOperationHandlerBase { @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); } } 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); for (Catalog catalog : catalogs) { DublinCoreCatalog dc = DublinCoreUtil.loadDublinCore(workspace, catalog); List<DublinCoreValue> values = dc.get(dcterm); if (values.isEmpty()) { 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); 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); } } } } return createResult(mediaPackage, Action.CONTINUE); }  void setWorkspace(Workspace workspace); @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context); static final String SOURCE_FLAVORS_PROPERTY; static final String SOURCE_TAGS_PROPERTY; static final String TARGET_FLAVOR_PROPERTY; static final String TARGET_TAGS_PROPERTY; static final String DCCATALOG_PROPERTY; static final String DCTERM_PROPERTY; static final String DEFAULT_VALUE_PROPERTY; static final String MATCH_VALUE_PROPERTY; static final String COPY_PROPERTY; }
@Test public void testMatchPresentDCTerm() throws Exception { operation.setConfiguration(TagByDublinCoreTermWOH.SOURCE_FLAVORS_PROPERTY, "dublincore/*"); operation.setConfiguration(TagByDublinCoreTermWOH.DCCATALOG_PROPERTY, "episode"); operation.setConfiguration(TagByDublinCoreTermWOH.DCTERM_PROPERTY, "publisher"); operation.setConfiguration(TagByDublinCoreTermWOH.MATCH_VALUE_PROPERTY, "University of Opencast"); operation.setConfiguration(TagByDublinCoreTermWOH.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagByDublinCoreTermWOH.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Catalog catalog = resultingMediapackage.getCatalog("catalog-1"); Assert.assertEquals("tag1", catalog.getTags()[0]); Assert.assertEquals("tag2", catalog.getTags()[1]); }
@Test public void testMatchDefaultDCTerm() throws Exception { operation.setConfiguration(TagByDublinCoreTermWOH.SOURCE_FLAVORS_PROPERTY, "dublincore/*"); operation.setConfiguration(TagByDublinCoreTermWOH.DCCATALOG_PROPERTY, "episode"); operation.setConfiguration(TagByDublinCoreTermWOH.DCTERM_PROPERTY, "source"); operation.setConfiguration(TagByDublinCoreTermWOH.DEFAULT_VALUE_PROPERTY, "Timbuktu"); operation.setConfiguration(TagByDublinCoreTermWOH.MATCH_VALUE_PROPERTY, "Timbuktu"); operation.setConfiguration(TagByDublinCoreTermWOH.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagByDublinCoreTermWOH.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Catalog catalog = resultingMediapackage.getCatalog("catalog-1"); Assert.assertEquals(2, catalog.getTags().length); Assert.assertEquals("tag1", catalog.getTags()[0]); Assert.assertEquals("tag2", catalog.getTags()[1]); }
@Test public void testMisMatchDefaultDCTerm() throws Exception { operation.setConfiguration(TagByDublinCoreTermWOH.SOURCE_FLAVORS_PROPERTY, "dublincore/*"); operation.setConfiguration(TagByDublinCoreTermWOH.DCCATALOG_PROPERTY, "episode"); operation.setConfiguration(TagByDublinCoreTermWOH.DCTERM_PROPERTY, "source"); operation.setConfiguration(TagByDublinCoreTermWOH.DEFAULT_VALUE_PROPERTY, "Cairo"); operation.setConfiguration(TagByDublinCoreTermWOH.MATCH_VALUE_PROPERTY, "Timbuktu"); operation.setConfiguration(TagByDublinCoreTermWOH.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagByDublinCoreTermWOH.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Catalog catalog = resultingMediapackage.getCatalog("catalog-1"); Assert.assertEquals(1, catalog.getTags().length); Assert.assertEquals("archive", catalog.getTags()[0]); }
@Test public void testMissingNoDefaultDCTerm() throws Exception { operation.setConfiguration(TagByDublinCoreTermWOH.SOURCE_FLAVORS_PROPERTY, "dublincore/*"); operation.setConfiguration(TagByDublinCoreTermWOH.DCCATALOG_PROPERTY, "episode"); operation.setConfiguration(TagByDublinCoreTermWOH.DCTERM_PROPERTY, "source"); operation.setConfiguration(TagByDublinCoreTermWOH.MATCH_VALUE_PROPERTY, "Timbuktu"); operation.setConfiguration(TagByDublinCoreTermWOH.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagByDublinCoreTermWOH.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Catalog catalog = resultingMediapackage.getCatalog("catalog-1"); Assert.assertEquals(1, catalog.getTags().length); Assert.assertEquals("archive", catalog.getTags()[0]); }
@Test public void testNoMatchConfiguredDCTerm() throws Exception { operation.setConfiguration(TagByDublinCoreTermWOH.SOURCE_FLAVORS_PROPERTY, "dublincore/*"); operation.setConfiguration(TagByDublinCoreTermWOH.DCCATALOG_PROPERTY, "episode"); operation.setConfiguration(TagByDublinCoreTermWOH.DCTERM_PROPERTY, "source"); operation.setConfiguration(TagByDublinCoreTermWOH.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagByDublinCoreTermWOH.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Catalog catalog = resultingMediapackage.getCatalog("catalog-1"); Assert.assertEquals(1, catalog.getTags().length); Assert.assertEquals("archive", catalog.getTags()[0]); }