use of org.opencastproject.assetmanager.api.query.Predicate in project opencast by opencast.
the class SchedulerServiceImpl method preCollisionEventCheck.
private List<String> preCollisionEventCheck(String trxId, String schedulingSource) throws SchedulerException {
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
TrxProps trxP = new TrxProps(query);
Predicate isSourceWithTrx = trxP.source().eq(schedulingSource).and(trxP.transactionId().eq(trxId));
Predicate hasProperties = p.agent().exists().and(p.start().exists()).and(p.end().exists());
Predicate isNotSource = p.source().eq(schedulingSource).not().and(hasProperties);
Predicate isNotOptedOut = p.optOut().eq(false).or(trxP.optOut().eq(false));
ASelectQuery select = query.select(trxP.optOut().target(), p.optOut().target(), p.agent().target(), p.start().target(), p.end().target(), trxP.agent().target(), trxP.start().target(), trxP.end().target(), trxP.transactionId().target()).where(withOrganization(query).and(isSourceWithTrx.or(isNotSource)).and(query.version().isLatest()).and(isNotOptedOut));
AResult result = select.run();
// Check for conflicts
List<String> conflictingRecords = new ArrayList<>();
Map<String, List<Interval>> invervalMap = new HashMap<>();
for (ARecord record : result.getRecords()) {
String agentId;
Date start;
Date end;
Opt<String> optTrxId = record.getProperties().apply(Properties.getStringOpt(TRANSACTION_ID_CONFIG));
if (optTrxId.isSome() && trxId.equals(optTrxId.get())) {
agentId = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getString(AGENT_CONFIG));
start = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getDate(START_DATE_CONFIG));
end = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getDate(END_DATE_CONFIG));
} else {
agentId = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getString(AGENT_CONFIG));
start = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getDate(START_DATE_CONFIG));
end = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getDate(END_DATE_CONFIG));
}
Interval currentInterval = new Interval(start.getTime(), end.getTime());
List<Interval> intervals = invervalMap.get(agentId);
if (intervals == null)
intervals = new ArrayList<>();
boolean overlaps = false;
for (Interval i : intervals) {
if (!i.overlaps(currentInterval))
continue;
overlaps = true;
break;
}
if (!overlaps) {
intervals.add(currentInterval);
} else {
conflictingRecords.add(record.getMediaPackageId());
}
invervalMap.put(agentId, intervals);
}
return conflictingRecords;
} catch (Exception e) {
logger.error("Failed to search for conflicting events: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.assetmanager.api.query.Predicate in project opencast by opencast.
the class SchedulerServiceImpl method removeRecording.
@Override
public void removeRecording(String id) throws NotFoundException, SchedulerException {
notEmpty(id, "id");
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
AResult result = query.select(query.nothing()).where(withOrganization(query).and(query.mediaPackageId(id).and(query.version().isLatest())).and(query.hasPropertiesOf(p.namespace()))).run();
Opt<ARecord> record = result.getRecords().head();
if (record.isNone())
throw new NotFoundException();
query = assetManager.createQuery();
p = new Props(query);
final Predicate predicate = withOrganization(query).and(query.mediaPackageId(id));
query.delete(SNAPSHOT_OWNER, p.recordingStatus().target()).where(predicate).run();
query.delete(SNAPSHOT_OWNER, p.recordingLastHeard().target()).where(predicate).run();
messageSender.sendObjectMessage(SchedulerItem.SCHEDULER_QUEUE, MessageSender.DestinationType.Queue, SchedulerItem.deleteRecordingState(id));
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
logger.error("Failed to delete recording status of event with mediapackage '{}': {}", id, getStackTrace(e));
throw new SchedulerException(e);
}
}
Aggregations