Search in sources :

Example 31 with AQueryBuilder

use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.

the class SchedulerServiceImplTest method testRemoveEventSimple.

@Test
public void testRemoveEventSimple() throws Exception {
    final Date start = new Date(System.currentTimeMillis() - 160000);
    final Date end = new Date(System.currentTimeMillis() - 60000);
    final String captureDeviceID = "demo";
    final MediaPackage mp = generateEvent(Opt.<String>none());
    final String mpId = mp.getIdentifier().toString();
    final DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
    addDublinCore(Opt.<String>none(), mp, event);
    final Map<String, String> caProperties = generateCaptureAgentMetadata(captureDeviceID);
    final AQueryBuilder q = assetManager.createQuery();
    // make sure that the asset manager is empty
    assertEquals("The asset manager should not contain any episodes", 0, q.select(q.snapshot()).run().getSize());
    // store event
    schedSvc.addEvent(start, end, captureDeviceID, Collections.<String>emptySet(), mp, wfProperties, caProperties, Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
    {
        final RichAResult r = enrich(q.select(q.snapshot(), q.properties()).run());
        assertEquals("The asset manager should contain one episode", 1, r.getSize());
        assertEquals("Episode ID", mpId, r.getRecords().head2().getMediaPackageId());
        assertFalse("The episode should have some properties", r.getProperties().isEmpty());
    }
    // remove event
    schedSvc.removeEvent(mpId);
    try {
        schedSvc.getMediaPackage(mpId);
        Assert.fail("No media package should be found since it has been deleted before");
    } catch (NotFoundException ignore) {
    }
    {
        final RichAResult r = enrich(q.select(q.snapshot(), q.properties()).run());
        assertTrue("The asset manager should not contain any properties anymore", r.getProperties().isEmpty());
        assertTrue("The asset manager should not contain any episodes anymore", r.getSnapshots().isEmpty());
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) NotFoundException(org.opencastproject.util.NotFoundException) RichAResult(org.opencastproject.assetmanager.api.query.RichAResult) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Date(java.util.Date) Test(org.junit.Test)

Example 32 with AQueryBuilder

use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.

the class DuplicateEventWorkflowOperationHandler method copyProperties.

private void copyProperties(String namespace, MediaPackage source, MediaPackage destination) {
    final AQueryBuilder q = assetManager.createQuery();
    final AResult properties = q.select(q.propertiesOf(namespace)).where(q.mediaPackageId(source.getIdentifier().toString())).run();
    if (properties.getRecords().head().isNone()) {
        logger.info("No properties to copy for media package {}.", source.getIdentifier(), namespace);
        return;
    }
    for (final Property p : properties.getRecords().head().get().getProperties()) {
        final PropertyId newPropId = PropertyId.mk(destination.getIdentifier().toString(), namespace, p.getId().getName());
        assetManager.setProperty(Property.mk(newPropId, p.getValue()));
    }
}
Also used : AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) AResult(org.opencastproject.assetmanager.api.query.AResult) Property(org.opencastproject.assetmanager.api.Property) PropertyId(org.opencastproject.assetmanager.api.PropertyId)

Example 33 with AQueryBuilder

use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.

the class LiveScheduleServiceImpl method getSnapshot.

Snapshot getSnapshot(String mpId) throws LiveScheduleException {
    AQueryBuilder query = assetManager.createQuery();
    AResult result = query.select(query.snapshot()).where(query.mediaPackageId(mpId).and(query.version().isLatest())).run();
    if (result.getSize() == 0) {
        // Media package not archived?.
        throw new LiveScheduleException(String.format("Unexpected error: media package %s has not been archived.", mpId));
    }
    Opt<ARecord> record = result.getRecords().head();
    if (record.isNone()) {
        // No snapshot?
        throw new LiveScheduleException(String.format("Unexpected error: media package %s has not been archived.", mpId));
    }
    return record.get().getSnapshot().get();
}
Also used : ARecord(org.opencastproject.assetmanager.api.query.ARecord) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) AResult(org.opencastproject.assetmanager.api.query.AResult) LiveScheduleException(org.opencastproject.liveschedule.api.LiveScheduleException)

Example 34 with AQueryBuilder

use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.

the class AssetManagerDeleteWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    final MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    final String mpId = mediaPackage.getIdentifier().toString();
    try {
        final AQueryBuilder q = assetManager.createQuery();
        final long deleted = q.delete(DEFAULT_OWNER, q.snapshot()).where(q.mediaPackageId(mpId)).run();
        if (deleted == 0) {
            logger.info(format("The asset manager does not contain episode %s", mpId));
        } else {
            logger.info(format("Successfully deleted %d version/s episode %s from the asset manager", deleted, mpId));
        }
    } catch (Exception e) {
        logger.warn(format("Error deleting episode %s from the asset manager: %s", mpId, e));
        throw new WorkflowOperationException("Unable to delete episode from the asset manager", e);
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 35 with AQueryBuilder

use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.

the class AssetManagerTest method testAssetManagerApiCompilation.

/**
 * Does not run. Only to test compilation.
 */
@Test(expected = java.lang.NullPointerException.class)
public void testAssetManagerApiCompilation() {
    AssetManager am = null;
    AQueryBuilder q = am.createQuery();
    // 
    // Selection
    // 
    // Select all versions of media package "mp-id".
    // Fetch media packages and associated metadata like version, creation date etc.
    // Do not fetch any properties since the query does not contain a property target.
    q.select(q.snapshot()).where(q.mediaPackageId("mp-id")).run();
    // Select the latest version of "mp-id".
    // Fetch the media package and associated metadata like version, creation date etc.
    // Do not fetch any properties since the query does not contain a property target.
    q.select(q.snapshot()).where(q.mediaPackageId("mp-id").and(q.version().isLatest())).run();
    // Select the latest version of "mp-id".
    // Fetch the media package and associated metadata like version, creation date etc.
    // Also fetch _all_ properties of the media package.
    q.select(q.snapshot(), q.properties()).where(q.mediaPackageId("some-mp-id").and(q.version().isLatest())).run();
    // Select the latest version of "mp-id" only if it has properties of namespace "org.opencastproject.service" attached.
    // Fetch only the media package and associated metadata like version, creation date etc.
    q.select(q.snapshot()).where(q.mediaPackageId("mp-id").and(q.version().isLatest()).and(q.hasPropertiesOf("org.opencastproject.service"))).run();
    // Select all versions of media package "mp-id" that are archived before now. Do not fetch associated properties.
    // <- assume some real date here
    final Date date = null;
    q.select(q.snapshot()).where(q.mediaPackageId("mp-id").and(q.archived().lt(date))).run();
    // does not compile because of a type error
    // AssetResult r = q.select(q.mediaPackage("some-mp-id").and(q.archived().lt(q.val(Value.mk("bla"))))).fetchProperties(false).run();
    // Select all versions of all media packages of series "series".
    // Also fetch all of their properties.
    q.select(q.snapshot()).where(q.seriesId().eq("series")).run();
    // Select the latest version of each media package of series "series".
    // Also fetch all of their properties.
    q.select(q.snapshot()).where(q.seriesId().eq("series").and(q.version().isLatest())).run();
    // Select all media packages and all of their versions that have boolean property "org.opencastproject.myservice:approved" set to true.
    // Fetch media packages and associated metadata like version, creation date etc.
    // Fetch all properties of namespace "org.opencastproject.myservice"
    q.select(q.snapshot(), q.propertiesOf("org.opencastproject.myservice")).where(q.property(Value.BOOLEAN, "org.opencastproject.myservice", "approved").eq(true)).run();
    // The above works but it's better to define and use a property schema!
    final Props p = new Props(q);
    // Select all versions of media packages that have boolean property "org.opencastproject.myservice:approved" set to true.
    q.select(q.snapshot()).where(p.approved().eq(true)).run();
    // Select all versions of media packages and properties of namespace "org.opencastproject.myservice" having a "count" property of less than 10.
    q.select(q.snapshot(), q.propertiesOf(p.namespace())).where(p.count().lt(10L)).run();
    // <- assume some version here
    final Version v = null;
    // Select all versions of all media packages having a younger version than "v".
    // Order the result set after their archival date.
    q.select(q.snapshot()).where(q.version().lt(v)).orderBy(q.archived().asc()).run();
    // Select properties of namespace "org.opencastproject.myservice" of media package "mp-id".
    q.select(p.allProperties()).where(q.mediaPackageId("mp-id")).run();
    // Select property "org.opencastproject.myservice:count" of media package "mp-id".
    q.select(p.count().target()).where(q.mediaPackageId("mp-id")).run();
    // Select all media packages and their start property which start in the future.
    q.select(q.snapshot(), p.start().target()).where(p.start().gt(new Date())).run();
    // 
    // Deletion
    // 
    // Delete all properties of all media packages.
    q.delete("owner", q.properties()).run();
    // Delete all properties of namespace "org.opencastproject.myservice" of all media packages.
    q.delete("owner", q.propertiesOf("org.opencastproject.myservice")).run();
    // Delete all properties of namespace "org.opencastproject.myservice" from media package "mp-id".
    q.delete("owner", q.propertiesOf("org.opencastproject.myservice")).where(q.mediaPackageId("mp-id")).run();
    // Delete property "org.opencastproject.myservice:prop" from media package "mp-id".
    q.delete("owner", q.properties(PropertyName.mk("org.opencastproject.myservice", "prop"))).where(q.mediaPackageId("mp-id")).run();
    // Delete properties "org.opencastproject.myservice:prop" and "org.opencastproject.myservice:prop2" from media package "mp-id".
    q.delete("owner", q.properties(PropertyName.mk("org.opencastproject.myservice", "prop"), PropertyName.mk("org.opencastproject.myservice", "prop2"))).where(q.mediaPackageId("mp-id")).run();
    // Delete all "org.opencastproject.myservice:approved" properties from all media package having a "org.opencastproject.myservice:count" greater 10.
    q.delete("owner", p.approved().target()).where(p.count().gt(10L));
    // 
    // Properties
    // 
    // Set property "org.opencastproject.myservice:count" of media package "mp-id" to 10.
    am.setProperty(Property.mk(PropertyId.mk("mp-id", "org.opencastproject.myservice", "count"), Value.mk(10L)));
    // Set property "org.opencastproject.myservice:count" of media package "mp-id" to 10 using the schema definition.
    am.setProperty(p.count().mk("mp-id", 10L));
}
Also used : AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)48 AResult (org.opencastproject.assetmanager.api.query.AResult)37 ARecord (org.opencastproject.assetmanager.api.query.ARecord)31 NotFoundException (org.opencastproject.util.NotFoundException)31 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)24 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)23 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)23 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)23 IOException (java.io.IOException)22 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)22 ValidationException (net.fortuna.ical4j.model.ValidationException)21 SeriesException (org.opencastproject.series.api.SeriesException)21 ServiceException (org.osgi.framework.ServiceException)21 ConfigurationException (org.osgi.service.cm.ConfigurationException)21 MediaPackage (org.opencastproject.mediapackage.MediaPackage)18 Date (java.util.Date)17 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)14 Log.getHumanReadableTimeString (org.opencastproject.util.Log.getHumanReadableTimeString)14 Test (org.junit.Test)12 Predicate (org.opencastproject.assetmanager.api.query.Predicate)10