use of org.opencastproject.assetmanager.api.query.ARecord in project opencast by opencast.
the class SchedulerServiceImpl method getCalendar.
@Override
public String getCalendar(Opt<String> captureAgentId, Opt<String> seriesId, Opt<Date> cutoff) throws SchedulerException {
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
Predicate predicate = withOrganization(query).and(withOwner(query)).and(query.hasPropertiesOf(p.namespace())).and(p.optOut().eq(false)).and(withVersion(query)).and(p.end().ge(DateTime.now().minusHours(1).toDate()));
for (String agentId : captureAgentId) {
predicate = predicate.and(p.agent().eq(agentId));
}
for (String series : seriesId) {
predicate = predicate.and(query.seriesId().eq(series));
}
for (Date d : cutoff) {
predicate = predicate.and(p.start().le(d));
}
ASelectQuery select = query.select(query.snapshot(), p.agent().target(), p.start().target(), p.end().target(), query.propertiesOf(CA_NAMESPACE)).where(predicate);
Stream<ARecord> records = select.run().getRecords();
CalendarGenerator cal = new CalendarGenerator(seriesService);
for (ARecord record : records) {
boolean blacklisted;
// isBlacklisted() methods are not implemented in the persistence layer and return always false
// try {
// //blacklisted = isBlacklisted(record.getMediaPackageId());
// } catch (NotFoundException e) {
// continue;
// }
blacklisted = false;
// Skip blacklisted events
if (blacklisted)
continue;
Opt<MediaPackage> optMp = record.getSnapshot().map(episodeToMp);
// If the event media package is empty, skip the event
if (optMp.isNone()) {
logger.warn("Mediapackage for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
continue;
}
Opt<DublinCoreCatalog> catalogOpt = loadEpisodeDublinCoreFromAsset(record.getSnapshot().get());
if (catalogOpt.isNone()) {
logger.warn("No episode catalog available, skipping!");
continue;
}
Map<String, String> caMetadata = record.getProperties().filter(filterByNamespace._2(CA_NAMESPACE)).group(toKey, toValue);
// If the even properties are empty, skip the event
if (caMetadata.isEmpty()) {
logger.warn("Properties for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
continue;
}
String agentId = record.getProperties().apply(Properties.getString(AGENT_CONFIG));
Date start = record.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
Date end = record.getProperties().apply(Properties.getDate(END_DATE_CONFIG));
Date lastModified = record.getSnapshot().get().getArchivalDate();
// Add the entry to the calendar, skip it with a warning if adding fails
try {
cal.addEvent(optMp.get(), catalogOpt.get(), agentId, start, end, lastModified, toPropertyString(caMetadata));
} catch (Exception e) {
logger.warn("Error adding event '{}' to calendar, event is not recorded: {}", record.getMediaPackageId(), getStackTrace(e));
continue;
}
}
// Only validate calendars with events. Without any events, the iCalendar won't validate
if (cal.getCalendar().getComponents().size() > 0) {
try {
cal.getCalendar().validate();
} catch (ValidationException e) {
logger.warn("Recording calendar could not be validated (returning it anyways): {}", getStackTrace(e));
}
}
return cal.getCalendar().toString();
} catch (Exception e) {
if (e instanceof SchedulerException)
throw e;
logger.error("Failed getting calendar: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.assetmanager.api.query.ARecord in project opencast by opencast.
the class SchedulerServiceImpl method updateRecordingState.
@Override
public boolean updateRecordingState(String id, String state) throws NotFoundException, SchedulerException {
notEmpty(id, "id");
notEmpty(state, "state");
if (!RecordingState.KNOWN_STATES.contains(state)) {
logger.warn("Invalid recording state: {}.", state);
return false;
}
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
AResult result = query.select(p.recordingStatus().target(), p.recordingLastHeard().target()).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();
Opt<String> recordingState = record.get().getProperties().apply(Properties.getStringOpt(RECORDING_STATE_CONFIG));
Opt<Long> lastHeard = record.get().getProperties().apply(Properties.getLongOpt(RECORDING_LAST_HEARD_CONFIG));
if (recordingState.isSome() && lastHeard.isSome()) {
Recording r = new RecordingImpl(id, recordingState.get(), lastHeard.get());
if (state.equals(r.getState())) {
logger.debug("Recording state not changed");
// Reset the state anyway so that the last-heard-from time is correct...
r.setState(state);
} else {
logger.debug("Setting Recording {} to state {}.", id, state);
r.setState(state);
sendRecordingUpdate(r);
}
assetManager.setProperty(p.recordingStatus().mk(id, r.getState()));
assetManager.setProperty(p.recordingLastHeard().mk(id, r.getLastCheckinTime()));
return true;
} else {
Recording r = new RecordingImpl(id, state);
assetManager.setProperty(p.recordingStatus().mk(id, r.getState()));
assetManager.setProperty(p.recordingLastHeard().mk(id, r.getLastCheckinTime()));
sendRecordingUpdate(r);
return true;
}
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
logger.error("Failed to update recording status of event with mediapackage '{}': {}", id, getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.assetmanager.api.query.ARecord in project opencast by opencast.
the class SchedulerServiceImpl method checkScheduleConflicts.
/*
* Returns a list of conflicting mediapackages. This method checks for containment, starts-during, and ends-during.
*/
private List<MediaPackage> checkScheduleConflicts(Date checkStart, Date checkEnd, ARecord[] records) {
final List<ARecord> result = new ArrayList<>();
for (final ARecord r : records) {
final Date start = r.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
final Date end = r.getProperties().apply(Properties.getDate(END_DATE_CONFIG));
/*
If the potential event starts during event r OR
If the potential event ends during event r OR
If the potential event begins before, and ends after event r (ie, containment) OR
If the potential event begins or ends within the minimum separation distance of event r
*/
if (checkStart.after(start) && checkStart.before(end) || checkEnd.after(start) && checkEnd.before(end) || checkStart.before(start) && checkEnd.after(end) || eventWithinMinimumSeparation(checkStart, checkEnd, start, end)) {
result.add(r);
}
}
result.sort(new Comparator<ARecord>() {
@Override
public int compare(ARecord o1, ARecord o2) {
Date start1 = o1.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
Date start2 = o2.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
return start1.compareTo(start2);
}
});
return Stream.mk(result).bind(recordToMp).toList();
}
use of org.opencastproject.assetmanager.api.query.ARecord in project opencast by opencast.
the class SchedulerServiceImpl method getDublinCore.
@Override
public DublinCoreCatalog getDublinCore(String mediaPackageId) throws NotFoundException, SchedulerException {
notEmpty(mediaPackageId, "mediaPackageId");
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
AResult result = query.select(query.snapshot()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId)).and(withVersion(query)).and(query.hasPropertiesOf(p.namespace()))).run();
Opt<ARecord> record = result.getRecords().head();
if (record.isNone())
throw new NotFoundException();
Opt<DublinCoreCatalog> dublinCore = loadEpisodeDublinCoreFromAsset(record.get().getSnapshot().get());
if (dublinCore.isNone())
throw new NotFoundException("No dublincore catalog found " + mediaPackageId);
return dublinCore.get();
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
logger.error("Failed to get dublin core catalog of event '{}': {}", mediaPackageId, getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.assetmanager.api.query.ARecord in project opencast by opencast.
the class SchedulerServiceImpl method removeEvent.
@Override
public synchronized void removeEvent(String mediaPackageId) throws NotFoundException, UnauthorizedException, SchedulerException {
notEmpty(mediaPackageId, "mediaPackageId");
try {
// Check if there are properties only from scheduler
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
AResult result = query.select(p.agent().target(), p.source().target()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId)).and(query.version().isLatest()).and(query.hasPropertiesOf(p.namespace()))).run();
Opt<ARecord> recordOpt = result.getRecords().head();
long deletedProperties = 0;
if (recordOpt.isSome()) {
// Check for locked transactions
Opt<String> source = recordOpt.get().getProperties().apply(Properties.getStringOpt(SOURCE_CONFIG));
if (source.isSome() && persistence.hasTransaction(source.get())) {
logger.warn("Unable to remove event '{}', source '{}' is currently locked due to an active transaction!", mediaPackageId, source.get());
throw new SchedulerTransactionLockException("Unable to remove event, locked source " + source.get());
}
String agentId = recordOpt.get().getProperties().apply(Properties.getString(AGENT_CONFIG));
// Delete all properties
deletedProperties = query.delete(SNAPSHOT_OWNER, query.propertiesOf(p.namespace(), WORKFLOW_NAMESPACE, CA_NAMESPACE)).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId))).name("delete all properties").run();
if (StringUtils.isNotEmpty(agentId))
touchLastEntry(agentId);
}
// Delete scheduler snapshot
long deletedSnapshots = query.delete(SNAPSHOT_OWNER, query.snapshot()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId))).name("delete episode").run();
if (deletedProperties + deletedSnapshots == 0)
throw new NotFoundException();
messageSender.sendObjectMessage(SchedulerItem.SCHEDULER_QUEUE, MessageSender.DestinationType.Queue, SchedulerItem.delete(mediaPackageId));
} catch (NotFoundException | SchedulerException e) {
throw e;
} catch (Exception e) {
logger.error("Could not remove event '{}' from persistent storage: {}", mediaPackageId, e);
throw new SchedulerException(e);
}
}
Aggregations