use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class LiveScheduleServiceImpl method replaceAndDistributeAcl.
MediaPackage replaceAndDistributeAcl(MediaPackage previousMp, AccessControlList acl) throws LiveScheduleException {
try {
// This is the mp from the search index
MediaPackage mp = (MediaPackage) previousMp.clone();
// Remove previous Acl from the mp
Attachment[] atts = mp.getAttachments(MediaPackageElements.XACML_POLICY_EPISODE);
if (atts.length > 0)
mp.remove(atts[0]);
// Attach current ACL to mp, acl will be created in the ws/wfr
authService.setAcl(mp, AclScope.Episode, acl);
atts = mp.getAttachments(MediaPackageElements.XACML_POLICY_EPISODE);
if (atts.length > 0) {
String aclId = atts[0].getIdentifier();
// Distribute new acl
Job distributionJob = downloadDistributionService.distribute(CHANNEL_ID, mp, aclId, false);
if (!waitForStatus(distributionJob).isSuccess())
throw new LiveScheduleException("Acl for live media package " + mp.getIdentifier() + " could not be distributed");
MediaPackageElement e = mp.getElementById(aclId);
// Cleanup workspace/wfr
mp.remove(e);
workspace.delete(e.getURI());
// Add distributed acl to mp
mp.add(MediaPackageElementParser.getFromXml(distributionJob.getPayload()));
}
return mp;
} catch (LiveScheduleException e) {
throw e;
} catch (Exception e) {
throw new LiveScheduleException(e);
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class AssetManagerSnapshotWorkflowOperationHandler method getMediaPackageForArchival.
protected MediaPackage getMediaPackageForArchival(MediaPackage current, List<String> tags, String[] sourceFlavors) throws MediaPackageException {
MediaPackage mp = (MediaPackage) current.clone();
Collection<MediaPackageElement> keep;
if (tags.isEmpty() && sourceFlavors.length < 1) {
keep = new ArrayList<>(Arrays.asList(current.getElementsByTags(tags)));
} else {
SimpleElementSelector simpleElementSelector = new SimpleElementSelector();
for (String flavor : sourceFlavors) {
simpleElementSelector.addFlavor(flavor);
}
for (String tag : tags) {
simpleElementSelector.addTag(tag);
}
keep = simpleElementSelector.select(current, false);
}
// Also archive the publication elements
for (Publication publication : current.getPublications()) {
keep.add(publication);
}
// Mark everything that is set for removal
List<MediaPackageElement> removals = new ArrayList<MediaPackageElement>();
for (MediaPackageElement element : mp.getElements()) {
if (!keep.contains(element)) {
removals.add(element);
}
}
// Fix references and flavors
for (MediaPackageElement element : mp.getElements()) {
if (removals.contains(element))
continue;
// Is the element referencing anything?
MediaPackageReference reference = element.getReference();
if (reference != null) {
Map<String, String> referenceProperties = reference.getProperties();
MediaPackageElement referencedElement = mp.getElementByReference(reference);
// if we are distributing the referenced element, everything is fine. Otherwise...
if (referencedElement != null && removals.contains(referencedElement)) {
// Follow the references until we find a flavor
MediaPackageElement parent;
while ((parent = current.getElementByReference(reference)) != null) {
if (parent.getFlavor() != null && element.getFlavor() == null) {
element.setFlavor(parent.getFlavor());
}
if (parent.getReference() == null) {
break;
}
reference = parent.getReference();
}
// Done. Let's cut the path but keep references to the mediapackage itself
if (reference != null && reference.getType().equals(MediaPackageReference.TYPE_MEDIAPACKAGE))
element.setReference(reference);
else if (reference != null && (referenceProperties == null || referenceProperties.size() == 0))
element.clearReference();
else {
// Ok, there is more to that reference than just pointing at an element. Let's keep the original,
// you never know.
removals.remove(referencedElement);
referencedElement.setURI(null);
referencedElement.setChecksum(null);
}
}
}
}
// Remove everything we don't want to add to publish
for (MediaPackageElement element : removals) {
mp.remove(element);
}
return mp;
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class AbstractAssetManagerSelectTest method testSelectProperties.
@Test
public void testSelectProperties() throws Exception {
final MediaPackage mp1 = mkMediaPackage();
final MediaPackageElement mpe = mkCatalog();
mp1.add(mpe);
am.takeSnapshot(OWNER, mp1);
//
assertEquals("No records should be found", 0, q.select(q.snapshot()).where(q.hasPropertiesOf("org.opencastproject.service")).run().getSize());
//
logger.info("Set property on first episode");
am.setProperty(Property.mk(PropertyId.mk(mp1.getIdentifier().toString(), "org.opencastproject.service", "count"), Value.mk(10L)));
assertEquals("One record should be found", 1, q.select(q.snapshot()).where(q.hasPropertiesOf("org.opencastproject.service")).run().getSize());
//
logger.info("Add another media package with some properties of the same namespace");
final MediaPackage mp2 = mkMediaPackage(mkCatalog());
am.takeSnapshot(OWNER, mp2);
am.setProperty(p.count.mk(mp2.getIdentifier().toString(), 20L));
am.setProperty(p.approved.mk(mp2.getIdentifier().toString(), true));
am.setProperty(p.start.mk(mp2.getIdentifier().toString(), new Date()));
//
logger.info("Add a 3rd media package without any properties");
am.takeSnapshot(OWNER, mkMediaPackage(mkCatalog()));
//
{
final AResult r = q.select(q.snapshot(), q.nothing()).where(p.hasPropertiesOfNamespace()).run();
assertEquals("Two records should be found", 2, r.getSize());
logger.info(r.getSearchTime() + "ms");
}
{
final AResult r = q.select(q.snapshot()).where(p.hasPropertiesOfNamespace()).run();
assertEquals("Two snapshots should be found", 2, r.getRecords().bind(getSnapshot).toList().size());
logger.info(r.getSearchTime() + "ms");
}
{
final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.hasPropertiesOfNamespace()).run();
assertEquals("Two snapshots should be found", 2, r.getRecords().bind(getSnapshot).toList().size());
logger.info(r.getSearchTime() + "ms");
}
{
final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.hasPropertiesOfNamespace()).run();
assertEquals("Two records with properties of the defined namespace should be found", 2, r.getSize());
logger.info(r.getSearchTime() + "ms");
}
{
final AResult r = q.select(q.snapshot(), p.allProperties()).run();
assertEquals("Three records should be found in total", 3, r.getSize());
logger.info(r.getSearchTime() + "ms");
}
{
final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.count.le(5L)).run();
assertEquals("No records should be found in total", 0, r.getSize());
}
{
final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.count.gt(5L)).run();
assertEquals("No records should be found in total", 2, r.getSize());
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class IndexServiceImplTest method testCreateEventInputNormalExpectsCreatedScheduledEvent.
@Test
public void testCreateEventInputNormalExpectsCreatedScheduledEvent() throws Exception {
String expectedTitle = "Test Event Creation";
String username = "akm220";
String org = "mh_default_org";
String[] creators = new String[] {};
Id mpId = new IdImpl("mp-id");
String testResourceLocation = "/events/create-scheduled-event.json";
JSONObject metadataJson = (JSONObject) parser.parse(IOUtils.toString(IndexServiceImplTest.class.getResourceAsStream(testResourceLocation)));
Capture<Catalog> result = EasyMock.newCapture();
Capture<String> mediapackageIdResult = EasyMock.newCapture();
Capture<String> catalogIdResult = EasyMock.newCapture();
Capture<String> filenameResult = EasyMock.newCapture();
Capture<InputStream> catalogResult = EasyMock.newCapture();
Capture<String> mediapackageTitleResult = EasyMock.newCapture();
SecurityService securityService = setupSecurityService(username, org);
Workspace workspace = EasyMock.createMock(Workspace.class);
EasyMock.expect(workspace.put(EasyMock.capture(mediapackageIdResult), EasyMock.capture(catalogIdResult), EasyMock.capture(filenameResult), EasyMock.capture(catalogResult))).andReturn(new URI("catalog.xml"));
EasyMock.replay(workspace);
// Create Common Event Catalog UI Adapter
CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = setupCommonCatalogUIAdapter(workspace).getA();
// Setup mediapackage.
MediaPackage mediapackage = EasyMock.createMock(MediaPackage.class);
mediapackage.add(EasyMock.capture(result));
EasyMock.expectLastCall();
EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {});
EasyMock.expect(mediapackage.getIdentifier()).andReturn(mpId).anyTimes();
EasyMock.expect(mediapackage.getCreators()).andReturn(creators);
mediapackage.addCreator("");
EasyMock.expectLastCall();
mediapackage.setTitle(EasyMock.capture(mediapackageTitleResult));
EasyMock.expectLastCall();
EasyMock.expect(mediapackage.getElements()).andReturn(new MediaPackageElement[] {}).anyTimes();
EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {}).anyTimes();
EasyMock.expect(mediapackage.getSeries()).andReturn(null).anyTimes();
mediapackage.setSeries(EasyMock.anyString());
EasyMock.expectLastCall();
EasyMock.replay(mediapackage);
IngestService ingestService = setupIngestService(mediapackage, Capture.<InputStream>newInstance());
// Setup Authorization Service
Tuple<MediaPackage, Attachment> returnValue = new Tuple<MediaPackage, Attachment>(mediapackage, null);
AuthorizationService authorizationService = EasyMock.createMock(AuthorizationService.class);
EasyMock.expect(authorizationService.setAcl(EasyMock.anyObject(MediaPackage.class), EasyMock.anyObject(AclScope.class), EasyMock.anyObject(AccessControlList.class))).andReturn(returnValue);
EasyMock.replay(authorizationService);
CaptureAgentStateService captureAgentStateService = setupCaptureAgentStateService();
Capture<Date> captureStart = EasyMock.newCapture();
Capture<Date> captureEnd = EasyMock.newCapture();
SchedulerService schedulerService = EasyMock.createNiceMock(SchedulerService.class);
schedulerService.addEvent(EasyMock.capture(captureStart), EasyMock.capture(captureEnd), EasyMock.anyString(), EasyMock.<Set<String>>anyObject(), EasyMock.anyObject(MediaPackage.class), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Opt<Boolean>>anyObject(), EasyMock.<Opt<String>>anyObject(), EasyMock.anyString());
EasyMock.expectLastCall().once();
EasyMock.replay(schedulerService);
// Run Test
IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
indexServiceImpl.setAuthorizationService(setupAuthorizationService(mediapackage));
indexServiceImpl.setIngestService(ingestService);
indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
indexServiceImpl.addCatalogUIAdapter(commonEventCatalogUIAdapter);
indexServiceImpl.setSecurityService(securityService);
indexServiceImpl.setUserDirectoryService(noUsersUserDirectoryService);
indexServiceImpl.setWorkspace(workspace);
indexServiceImpl.setCaptureAgentStateService(captureAgentStateService);
indexServiceImpl.setSchedulerService(schedulerService);
String scheduledEvent = indexServiceImpl.createEvent(metadataJson, mediapackage);
Assert.assertEquals(mediapackage.getIdentifier().compact(), scheduledEvent);
assertTrue("The catalog must be added to the mediapackage", result.hasCaptured());
assertEquals("The catalog should have been added to the correct mediapackage", mpId.toString(), mediapackageIdResult.getValue());
assertTrue("The catalog should have a new id", catalogIdResult.hasCaptured());
assertTrue("The catalog should have a new filename", filenameResult.hasCaptured());
assertTrue("The catalog should have been added to the input stream", catalogResult.hasCaptured());
assertTrue("The mediapackage should have had its title updated", catalogResult.hasCaptured());
assertEquals("The mediapackage title should have been updated.", expectedTitle, mediapackageTitleResult.getValue());
assertTrue("The catalog should have been created", catalogResult.hasCaptured());
assertTrue(captureStart.hasCaptured());
assertTrue(captureEnd.hasCaptured());
Assert.assertEquals(new Date(DateTimeSupport.fromUTC("2008-03-16T14:00:00Z")), captureStart.getValue());
Assert.assertEquals(new Date(DateTimeSupport.fromUTC("2008-03-16T14:01:00Z")), captureEnd.getValue());
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class IndexServiceImplTest method testCreateEventInputNormalExpectsCreatedRecurringEvent.
@Test
public void testCreateEventInputNormalExpectsCreatedRecurringEvent() throws Exception {
String expectedTitle = "Test Event Creation";
String username = "akm220";
String org = "mh_default_org";
String[] creators = new String[] {};
Id mpId = new IdImpl("mp-id");
String testResourceLocation = "/events/create-recurring-event.json";
JSONObject metadataJson = (JSONObject) parser.parse(IOUtils.toString(IndexServiceImplTest.class.getResourceAsStream(testResourceLocation)));
Capture<String> mediapackageIdResult = EasyMock.newCapture();
Capture<String> catalogIdResult = EasyMock.newCapture();
Capture<String> filenameResult = EasyMock.newCapture();
Capture<InputStream> catalogResult = EasyMock.newCapture();
Capture<String> mediapackageTitleResult = EasyMock.newCapture();
SecurityService securityService = setupSecurityService(username, org);
Workspace workspace = EasyMock.createMock(Workspace.class);
EasyMock.expect(workspace.put(EasyMock.capture(mediapackageIdResult), EasyMock.capture(catalogIdResult), EasyMock.capture(filenameResult), EasyMock.capture(catalogResult))).andReturn(new URI("catalog.xml"));
EasyMock.expect(workspace.read(getClass().getResource("/dublincore.xml").toURI())).andAnswer(() -> getClass().getResourceAsStream("/dublincore.xml")).anyTimes();
EasyMock.replay(workspace);
// Create Common Event Catalog UI Adapter
CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = setupCommonCatalogUIAdapter(workspace).getA();
// Setup mediapackage.
MediaPackage mediapackage = EasyMock.createMock(MediaPackage.class);
EasyMock.expect(mediapackage.clone()).andReturn(mediapackage).anyTimes();
EasyMock.expect(mediapackage.getSeries()).andReturn(null).anyTimes();
EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] { CatalogImpl.fromURI(getClass().getResource("/dublincore.xml").toURI()) });
EasyMock.expect(mediapackage.getIdentifier()).andReturn(mpId).anyTimes();
EasyMock.expect(mediapackage.getCreators()).andReturn(creators);
mediapackage.addCreator("");
EasyMock.expectLastCall();
mediapackage.setTitle(EasyMock.capture(mediapackageTitleResult));
EasyMock.expectLastCall().once();
mediapackage.setTitle(EasyMock.anyString());
EasyMock.expectLastCall().times(15);
EasyMock.expect(mediapackage.getElements()).andReturn(new MediaPackageElement[] {}).anyTimes();
EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {}).anyTimes();
mediapackage.setIdentifier(EasyMock.anyObject(Id.class));
EasyMock.expectLastCall().anyTimes();
mediapackage.setSeries(EasyMock.anyString());
mediapackage.setSeriesTitle(EasyMock.anyString());
EasyMock.expectLastCall();
EasyMock.replay(mediapackage);
CaptureAgentStateService captureAgentStateService = setupCaptureAgentStateService();
// Setup scheduler service
Capture<Date> recurrenceStart = EasyMock.newCapture();
Capture<Date> recurrenceEnd = EasyMock.newCapture();
Capture<RRule> rrule = EasyMock.newCapture();
Capture duration = EasyMock.newCapture();
Capture<TimeZone> tz = EasyMock.newCapture();
Capture<Date> schedStart = EasyMock.newCapture();
Capture<Date> schedEnd = EasyMock.newCapture();
Capture<RRule> schedRRule = EasyMock.newCapture();
Capture schedDuration = EasyMock.newCapture();
Capture<TimeZone> schedTz = EasyMock.newCapture();
Capture<MediaPackage> mp = EasyMock.newCapture();
SchedulerService schedulerService = EasyMock.createNiceMock(SchedulerService.class);
// Look up the expected periods
EasyMock.expect(schedulerService.calculatePeriods(EasyMock.capture(rrule), EasyMock.capture(recurrenceStart), EasyMock.capture(recurrenceEnd), EasyMock.captureLong(duration), EasyMock.capture(tz))).andAnswer(new IAnswer<List<Period>>() {
@Override
public List<Period> answer() throws Throwable {
return calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue());
}
}).anyTimes();
// The actual scheduling
EasyMock.expect(schedulerService.addMultipleEvents(EasyMock.capture(schedRRule), EasyMock.capture(schedStart), EasyMock.capture(schedEnd), EasyMock.captureLong(schedDuration), EasyMock.capture(schedTz), EasyMock.anyString(), EasyMock.<Set<String>>anyObject(), EasyMock.capture(mp), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Opt<Boolean>>anyObject(), EasyMock.<Opt<String>>anyObject(), EasyMock.anyString())).andAnswer(new IAnswer<Map<String, Period>>() {
@Override
public Map<String, Period> answer() throws Throwable {
List<Period> periods = calculatePeriods(schedRRule.getValue(), schedStart.getValue(), schedEnd.getValue(), (Long) schedDuration.getValue(), schedTz.getValue());
Map<String, Period> mapping = new LinkedHashMap<>();
int counter = 0;
for (Period p : periods) {
mapping.put(new IdImpl(UUID.randomUUID().toString()).compact(), p);
}
return mapping;
}
}).anyTimes();
EasyMock.replay(schedulerService);
// Run Test
IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
indexServiceImpl.setAuthorizationService(setupAuthorizationService(mediapackage));
indexServiceImpl.setIngestService(setupIngestService(mediapackage, Capture.<InputStream>newInstance()));
indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
indexServiceImpl.addCatalogUIAdapter(commonEventCatalogUIAdapter);
indexServiceImpl.setSecurityService(securityService);
indexServiceImpl.setUserDirectoryService(noUsersUserDirectoryService);
indexServiceImpl.setWorkspace(workspace);
indexServiceImpl.setCaptureAgentStateService(captureAgentStateService);
indexServiceImpl.setSchedulerService(schedulerService);
String scheduledEvents = indexServiceImpl.createEvent(metadataJson, mediapackage);
String[] ids = StringUtils.split(scheduledEvents, ",");
// We should have as many scheduled events as we do periods
Assert.assertTrue(ids.length == calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue()).size());
assertEquals("The catalog should have been added to the correct mediapackage", mpId.toString(), mediapackageIdResult.getValue());
assertTrue("The catalog should have a new id", catalogIdResult.hasCaptured());
assertTrue("The catalog should have a new filename", filenameResult.hasCaptured());
assertTrue("The catalog should have been added to the input stream", catalogResult.hasCaptured());
assertTrue("The mediapackage should have had its title updated", catalogResult.hasCaptured());
assertEquals("The mediapackage title should have been updated.", expectedTitle, mediapackageTitleResult.getValue());
assertTrue("The catalog should have been created", catalogResult.hasCaptured());
// Assert that the start and end recurrence dates captured, along with the duration and recurrence rule
// This is all used by the scheduling calculation, but not the actual scheduling call
assertTrue(recurrenceStart.hasCaptured());
assertTrue(recurrenceEnd.hasCaptured());
assertTrue(duration.hasCaptured());
assertTrue(rrule.hasCaptured());
// Assert that the scheduling call has its necessary data
assertTrue(schedStart.hasCaptured());
assertTrue(schedEnd.hasCaptured());
assertTrue(schedDuration.hasCaptured());
assertTrue(schedRRule.hasCaptured());
assertTrue(schedTz.hasCaptured());
List<Period> pCheck = calculatePeriods(schedRRule.getValue(), schedStart.getValue(), schedEnd.getValue(), (Long) schedDuration.getValue(), schedTz.getValue());
List<Period> pExpected = calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue());
// Assert that the first capture time is the same as the recurrence start
assertEquals(pExpected.get(0).getStart(), pCheck.get(0).getStart());
// Assert that the end of the last capture time is the same as the recurrence end
assertEquals(pExpected.get(pExpected.size() - 1).getEnd(), pCheck.get(pCheck.size() - 1).getEnd());
}
Aggregations