use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.
the class AbstractAclServiceRestEndpoint method getActiveAclForEpisode.
private Either<AccessControlList, Tuple<ManagedAcl, AclScope>> getActiveAclForEpisode(AclService aclService, String episodeId) {
final AQueryBuilder q = getAssetManager().createQuery();
final ASelectQuery sq = q.select(q.snapshot()).where(q.mediaPackageId(episodeId).and(q.version().isLatest()));
for (Snapshot snapshot : enrich(sq.run()).getSnapshots().head()) {
// get active ACL of found media package
final Tuple<AccessControlList, AclScope> activeAcl = getAuthorizationService().getActiveAcl(snapshot.getMediaPackage());
// find corresponding managed ACL
for (ManagedAcl macl : matchAcls(aclService, activeAcl.getA())) {
return right(tuple(macl, activeAcl.getB()));
}
return left(activeAcl.getA());
}
// episode does not exist
logger.warn("Episode {} cannot be found in Archive", episodeId);
return left(EMPTY_ACL);
}
use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.
the class AclServiceImpl method getFromAssetManagerByMpId.
/**
* Return media package with id <code>mpId</code> from asset manager.
*
* @return single element list or empty list
*/
private Option<MediaPackage> getFromAssetManagerByMpId(String mpId) {
final AQueryBuilder q = assetManager.createQuery();
final Opt<MediaPackage> mp = enrich(q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(q.version().isLatest())).run()).getSnapshots().head().map(Snapshots.getMediaPackage);
return Option.fromOpt(mp);
}
use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.
the class TestRestService method newAssetManager.
private static AssetManager newAssetManager() {
Snapshot snapshot = EasyMock.createNiceMock(Snapshot.class);
try {
EasyMock.expect(snapshot.getMediaPackage()).andReturn(new MediaPackageBuilderImpl().createNew()).anyTimes();
} catch (MediaPackageException e) {
throw new RuntimeException(e);
}
ARecord record = EasyMock.createNiceMock(ARecord.class);
EasyMock.expect(record.getSnapshot()).andReturn(Opt.some(snapshot)).anyTimes();
AResult result = EasyMock.createNiceMock(AResult.class);
EasyMock.expect(result.getRecords()).andReturn($(record)).anyTimes();
ASelectQuery select = EasyMock.createNiceMock(ASelectQuery.class);
EasyMock.expect(select.where(EasyMock.anyObject(Predicate.class))).andReturn(select).anyTimes();
EasyMock.expect(select.run()).andReturn(result).anyTimes();
Predicate predicate = EasyMock.createNiceMock(Predicate.class);
EasyMock.expect(predicate.and(EasyMock.anyObject(Predicate.class))).andReturn(predicate).anyTimes();
AQueryBuilder query = EasyMock.createNiceMock(AQueryBuilder.class);
VersionField version = EasyMock.createNiceMock(VersionField.class);
EasyMock.expect(query.version()).andReturn(version).anyTimes();
EasyMock.expect(query.mediaPackageId(EasyMock.anyString())).andReturn(predicate).anyTimes();
EasyMock.expect(query.select(EasyMock.anyObject(Target.class))).andReturn(select).anyTimes();
AssetManager assetManager = EasyMock.createNiceMock(AssetManager.class);
EasyMock.expect(assetManager.createQuery()).andReturn(query).anyTimes();
EasyMock.replay(assetManager, version, query, predicate, select, result, record, snapshot);
return assetManager;
}
use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.
the class ArchiveToAssetManagerMigrationTest method testMigration.
@Test
@Parameters
public void testMigration(String testData, Stream<String> seriesIds) throws Exception {
// setup database and apply the DDL migration
penv.tx(runStatements("/mysql-reset.sql"));
penv.tx(runStatements("/mysql-archive-schema.sql"));
// insert test data since it makes a difference to migrate a database with or without data
penv.tx(runStatements(testData));
// schema migration pre-processing
penv.tx(runStatements("/mysql-migration-1.sql"));
// data migration
penv.tx(runStatements("/mysql-migration-2.sql"));
// schema migration post-processing and cleanup
penv.tx(runStatements("/mysql-migration-3.sql"));
//
// compare actual and expected DDL
final String showCreateTables = $("oc_assets_snapshot", "oc_assets_asset", "oc_assets_properties", "oc_assets_version_claim", "SEQUENCE").map(Strings.wrap("SHOW CREATE TABLE ", "")).bind(findAll).map(take(1)).mkString(";\n\n").concat(";");
logger.info("+ " + showCreateTables);
assertEquals("Migrated database should be equal to DDL in Ansible playbook", IoSupport.loadFileFromClassPathAsString("/mysql-assetmanager-schema.sql").get().trim(), showCreateTables);
// check sequence table migration
assertTrue("Sequence table should be updated", penv.tx(Queries.sql.findSingle("SELECT * FROM SEQUENCE WHERE SEQ_NAME='seq_oc_assets_asset'")).isSome());
assertTrue("Sequence table should be updated", penv.tx(Queries.sql.findSingle("SELECT * FROM SEQUENCE WHERE SEQ_NAME='seq_oc_assets_snapshot'")).isSome());
//
// if the migration completed successfully the asset manager should be able to run some operations
final AQueryBuilder q = am.createQuery();
{
// run series checks
for (String seriesId : seriesIds) {
final RichAResult r = enrich(q.select(q.snapshot()).where(q.seriesId().eq(seriesId)).run());
assertTrue(r.getSize() > 0);
for (Snapshot e : r.getSnapshots()) {
assertEquals(seriesId, e.getMediaPackage().getSeries());
}
}
}
// add some media packages
final String[] mp = createAndAddMediaPackagesSimple(5, 1, 1);
for (String id : mp) {
am.setProperty(p.agent.mk(id, "agent"));
}
{
final RichAResult r = enrich(q.select(q.snapshot(), q.properties()).run());
assertEquals(59, r.getSize());
assertEquals(59, r.countSnapshots());
assertEquals(5, r.countProperties());
}
}
use of org.opencastproject.assetmanager.api.query.AQueryBuilder in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandlerTest method mockDependencies.
private void mockDependencies(int numberOfCopies) throws Exception {
clonedMediaPackages = Capture.newInstance(CaptureType.ALL);
reset(workspace, assetManager, distributionService);
URI uriDc = getClass().getResource("/dublincore.xml").toURI();
for (int i = 0; i < numberOfCopies; i++) {
expect(workspace.read(eq(URI.create("dublincore.xml")))).andReturn(new FileInputStream(new File(uriDc))).times(1);
}
expect(workspace.get(anyObject())).andReturn(new File(getClass().getResource("/av.mov").toURI())).anyTimes();
expect(workspace.put(anyString(), anyString(), eq("dublincore.xml"), anyObject())).andReturn(uriDc).times(numberOfCopies);
replay(workspace);
final AResult qResult = createNiceMock(AResult.class);
expect(qResult.getRecords()).andReturn(Stream.empty()).anyTimes();
replay(qResult);
final ASelectQuery qSelect = createNiceMock(ASelectQuery.class);
expect(qSelect.where(anyObject())).andReturn(qSelect).anyTimes();
expect(qSelect.run()).andReturn(qResult).anyTimes();
replay(qSelect);
final AQueryBuilder qBuilder = createNiceMock(AQueryBuilder.class);
expect(qBuilder.select(anyObject())).andReturn(qSelect).anyTimes();
replay(qBuilder);
expect(assetManager.createQuery()).andReturn(qBuilder).anyTimes();
expect(assetManager.takeSnapshot(eq(AssetManager.DEFAULT_OWNER), capture(clonedMediaPackages))).andReturn(createNiceMock(Snapshot.class)).times(numberOfCopies);
replay(assetManager);
final Job distributionJob = createNiceMock(Job.class);
final Publication internalPub = (Publication) mp.getElementById("pub-int");
final List<MediaPackageElement> internalPubElements = new ArrayList<>();
Collections.addAll(internalPubElements, (internalPub.getAttachments()));
Collections.addAll(internalPubElements, (internalPub.getCatalogs()));
Collections.addAll(internalPubElements, (internalPub.getTracks()));
expect(distributionJob.getStatus()).andReturn(Job.Status.FINISHED).anyTimes();
for (MediaPackageElement e : internalPubElements) {
expect(distributionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(e)).times(numberOfCopies);
}
replay(distributionJob);
expect(distributionService.distribute(eq(InternalPublicationChannel.CHANNEL_ID), anyObject(), anyString())).andReturn(distributionJob).anyTimes();
replay(distributionService);
}
Aggregations