TagWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @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 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)); if (copy) { logger.info("Retagging mediapackage elements as a copy"); } else { logger.info("Retagging mediapackage elements"); } 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)); } List<String> removeTags = new ArrayList<String>(); List<String> addTags = new ArrayList<String>(); List<String> overrideTags = new ArrayList<String>(); 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); } 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) { MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(configuredTargetFlavor); String targetFlavorType = targetFlavor.getType(); String targetFlavorSubtype = targetFlavor.getSubtype(); if (MediaPackageElementFlavor.WILDCARD.equals(targetFlavorType)) { targetFlavorType = element.getFlavor().getType(); } if (MediaPackageElementFlavor.WILDCARD.equals(targetFlavorSubtype)) { targetFlavorSubtype = element.getFlavor().getSubtype(); } String targetFlavorStr = targetFlavorType + MediaPackageElementFlavor.SEPARATOR + targetFlavorSubtype; element.setFlavor(MediaPackageElementFlavor.parseFlavor(targetFlavorStr)); } 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); }  @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 COPY_PROPERTY; }
@Test public void testTargetFlavourWithSubtypeWildcard() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(mp); operation.setConfiguration(TagWorkflowOperationHandler.SOURCE_FLAVORS_PROPERTY, "*/source"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "wildcard_test/*"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_TAGS_PROPERTY, "tag1, tag2"); operation.setConfiguration(TagWorkflowOperationHandler.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Track track1 = resultingMediapackage.getTrack("track-1"); Track track2 = resultingMediapackage.getTrack("track-2"); Assert.assertEquals("wildcard_test/source", track1.getFlavor().toString()); Assert.assertEquals("wildcard_test/source", track2.getFlavor().toString()); }
@Test public void testTargetFlavourWithTypeAndSubtypeWildcard() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(mp); operation.setConfiguration(TagWorkflowOperationHandler.SOURCE_FLAVORS_PROPERTY, "*/source"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "*/*"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_TAGS_PROPERTY, "tag1, tag2"); operation.setConfiguration(TagWorkflowOperationHandler.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Track track1 = resultingMediapackage.getTrack("track-1"); Track track2 = resultingMediapackage.getTrack("track-2"); Assert.assertEquals("presentation/source", track1.getFlavor().toString()); Assert.assertEquals("presenter/source", track2.getFlavor().toString()); }
@Test public void testAllTagsFlavors() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(mp); operation.setConfiguration(TagWorkflowOperationHandler.SOURCE_FLAVORS_PROPERTY, "presenter/source"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "presenter/test"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_TAGS_PROPERTY, "tag1,tag2"); operation.setConfiguration(TagWorkflowOperationHandler.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Track track = resultingMediapackage.getTrack("track-2"); Assert.assertEquals("presenter/test", track.getFlavor().toString()); Assert.assertEquals("tag1", track.getTags()[0]); Assert.assertEquals("tag2", track.getTags()[1]); instance = new WorkflowInstanceImpl(); ops = new ArrayList<WorkflowOperationInstance>(); operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(resultingMediapackage); operation.setConfiguration(TagWorkflowOperationHandler.SOURCE_TAGS_PROPERTY, "tag1"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "presenter/source"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_TAGS_PROPERTY, "-tag1,+tag3"); operation.setConfiguration(TagWorkflowOperationHandler.COPY_PROPERTY, "false"); result = operationHandler.start(instance, null); resultingMediapackage = result.getMediaPackage(); track = resultingMediapackage.getTrack("track-2"); Assert.assertEquals("presenter/source", track.getFlavor().toString()); Assert.assertEquals("tag2", track.getTags()[0]); Assert.assertEquals("tag3", track.getTags()[1]); }
@Test public void testTargetFlavourWithTypeWildcard() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(mp); operation.setConfiguration(TagWorkflowOperationHandler.SOURCE_FLAVORS_PROPERTY, "*/source"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "*/wildcard_test"); operation.setConfiguration(TagWorkflowOperationHandler.TARGET_TAGS_PROPERTY, "tag1, tag2"); operation.setConfiguration(TagWorkflowOperationHandler.COPY_PROPERTY, "false"); WorkflowOperationResult result = operationHandler.start(instance, null); MediaPackage resultingMediapackage = result.getMediaPackage(); Track track1 = resultingMediapackage.getTrack("track-1"); Track track2 = resultingMediapackage.getTrack("track-2"); Assert.assertEquals("presentation/wildcard_test", track1.getFlavor().toString()); Assert.assertEquals("presenter/wildcard_test", track2.getFlavor().toString()); }