use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class SeriesServiceSolrIndex method createDocument.
/**
* Creates solr document for inserting into solr index.
*
* @param dc
* {@link DublinCoreCatalog} to be stored in index
* @return {@link SolrInputDocument} created out of Dublin core
*/
protected SolrInputDocument createDocument(DublinCoreCatalog dc) {
final SolrInputDocument doc = new SolrInputDocument();
String dublinCoreId = dc.getFirst(DublinCore.PROPERTY_IDENTIFIER);
String orgId = securityService.getOrganization().getId();
doc.addField(SolrFields.COMPOSITE_ID_KEY, getCompositeKey(dublinCoreId, orgId));
doc.addField(SolrFields.ORGANIZATION, orgId);
doc.addField(SolrFields.IDENTIFIER_KEY, dublinCoreId);
try {
doc.addField(SolrFields.XML_KEY, serializeDublinCore(dc));
} catch (IOException e1) {
throw new IllegalArgumentException(e1);
}
doc.addField(SolrFields.OPT_OUT, false);
// single valued fields
if (dc.hasValue(DublinCore.PROPERTY_TITLE)) {
doc.addField(SolrFields.TITLE_KEY, dc.getFirst(DublinCore.PROPERTY_TITLE));
doc.addField(SolrFields.TITLE_KEY + "_sort", dc.getFirst(DublinCore.PROPERTY_TITLE));
}
if (dc.hasValue(DublinCore.PROPERTY_CREATED)) {
final Temporal temporal = EncodingSchemeUtils.decodeTemporal(dc.get(DublinCore.PROPERTY_CREATED).get(0));
temporal.fold(new Temporal.Match<Void>() {
@Override
public Void period(DCMIPeriod period) {
doc.addField(SolrFields.CREATED_KEY, period.getStart());
return null;
}
@Override
public Void instant(Date instant) {
doc.addField(SolrFields.CREATED_KEY, instant);
return null;
}
@Override
public Void duration(long duration) {
throw new IllegalArgumentException("Dublin core dc:created is neither a date nor a period");
}
});
}
if (dc.hasValue(DublinCore.PROPERTY_AVAILABLE)) {
Temporal temporal = EncodingSchemeUtils.decodeTemporal(dc.get(DublinCore.PROPERTY_AVAILABLE).get(0));
temporal.fold(new Temporal.Match<Void>() {
@Override
public Void period(DCMIPeriod period) {
if (period.hasStart()) {
doc.addField(SolrFields.AVAILABLE_FROM_KEY, period.getStart());
}
if (period.hasEnd()) {
doc.addField(SolrFields.AVAILABLE_TO_KEY, period.getEnd());
}
return null;
}
@Override
public Void instant(Date instant) {
doc.addField(SolrFields.AVAILABLE_FROM_KEY, instant);
return null;
}
@Override
public Void duration(long duration) {
throw new IllegalArgumentException("Dublin core field dc:available is neither a date nor a period");
}
});
}
// multivalued fields
addMultiValuedFieldToSolrDocument(doc, SolrFields.SUBJECT_KEY, dc.get(DublinCore.PROPERTY_SUBJECT));
addMultiValuedFieldToSolrDocument(doc, SolrFields.CREATOR_KEY, dc.get(DublinCore.PROPERTY_CREATOR));
addMultiValuedFieldToSolrDocument(doc, SolrFields.PUBLISHER_KEY, dc.get(DublinCore.PROPERTY_PUBLISHER));
addMultiValuedFieldToSolrDocument(doc, SolrFields.CONTRIBUTOR_KEY, dc.get(DublinCore.PROPERTY_CONTRIBUTOR));
addMultiValuedFieldToSolrDocument(doc, SolrFields.ABSTRACT_KEY, dc.get(DublinCore.PROPERTY_ABSTRACT));
addMultiValuedFieldToSolrDocument(doc, SolrFields.DESCRIPTION_KEY, dc.get(DublinCore.PROPERTY_DESCRIPTION));
addMultiValuedFieldToSolrDocument(doc, SolrFields.LANGUAGE_KEY, dc.get(DublinCore.PROPERTY_LANGUAGE));
addMultiValuedFieldToSolrDocument(doc, SolrFields.RIGHTS_HOLDER_KEY, dc.get(DublinCore.PROPERTY_RIGHTS_HOLDER));
addMultiValuedFieldToSolrDocument(doc, SolrFields.SPATIAL_KEY, dc.get(DublinCore.PROPERTY_SPATIAL));
addMultiValuedFieldToSolrDocument(doc, SolrFields.TEMPORAL_KEY, dc.get(DublinCore.PROPERTY_TEMPORAL));
addMultiValuedFieldToSolrDocument(doc, SolrFields.IS_PART_OF_KEY, dc.get(DublinCore.PROPERTY_IS_PART_OF));
addMultiValuedFieldToSolrDocument(doc, SolrFields.REPLACES_KEY, dc.get(DublinCore.PROPERTY_REPLACES));
addMultiValuedFieldToSolrDocument(doc, SolrFields.TYPE_KEY, dc.get(DublinCore.PROPERTY_TYPE));
addMultiValuedFieldToSolrDocument(doc, SolrFields.ACCESS_RIGHTS_KEY, dc.get(DublinCore.PROPERTY_ACCESS_RIGHTS));
addMultiValuedFieldToSolrDocument(doc, SolrFields.LICENSE_KEY, dc.get(DublinCore.PROPERTY_LICENSE));
return doc;
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class SchedulerServiceImpl method addMultipleEventInternal.
private Map<String, Period> addMultipleEventInternal(List<Period> periods, String captureAgentId, Set<String> userIds, MediaPackage templateMp, Map<String, String> wfProperties, Map<String, String> caMetadata, String modificationOrigin, Opt<Boolean> optOutStatus, Opt<String> schedulingSource, Opt<String> trxId) throws SchedulerException {
notNull(periods, "periods");
requireTrue(periods.size() > 0, "periods");
notEmpty(captureAgentId, "captureAgentId");
notNull(userIds, "userIds");
notNull(templateMp, "mediaPackages");
notNull(wfProperties, "wfProperties");
notNull(caMetadata, "caMetadata");
notEmpty(modificationOrigin, "modificationOrigin");
notNull(optOutStatus, "optOutStatus");
notNull(schedulingSource, "schedulingSource");
notNull(trxId, "trxId");
Map<String, Period> scheduledEvents = new LinkedHashMap<>();
try {
LinkedList<Id> ids = new LinkedList<>();
AQueryBuilder qb = assetManager.createQuery();
Predicate p = null;
// While we don't have a list of IDs equal to the number of periods
while (ids.size() <= periods.size()) {
// Create a list of IDs equal to the number of periods, along with a set of AM predicates
while (ids.size() <= periods.size()) {
Id id = new IdImpl(UUID.randomUUID().toString());
ids.add(id);
Predicate np = qb.mediaPackageId(id.compact());
// Haha, p = np jokes with the AM query language. Ha. Haha. Ha. (Sob...)
if (null == p) {
p = np;
} else {
p = p.or(np);
}
}
// Select the list of ids which alread exist. Hint: this needs to be zero
AResult result = qb.select(qb.nothing()).where(withOrganization(qb).and(p).and(qb.version().isLatest())).run();
// If there is conflict, clear the list and start over
if (result.getTotalSize() > 0) {
ids.clear();
}
}
Opt<String> seriesId = Opt.nul(StringUtils.trimToNull(templateMp.getSeries()));
// Get opt out status
boolean optOut = getOptOutStatus(seriesId, optOutStatus);
if (trxId.isNone()) {
// Check for locked transactions
if (schedulingSource.isSome() && persistence.hasTransaction(schedulingSource.get())) {
logger.warn("Unable to add events, source '{}' is currently locked due to an active transaction!", schedulingSource.get());
throw new SchedulerTransactionLockException("Unable to add event, locked source " + schedulingSource.get());
}
// Check for conflicting events if not opted out
if (!optOut) {
List<MediaPackage> conflictingEvents = findConflictingEvents(periods, captureAgentId, TimeZone.getDefault());
if (conflictingEvents.size() > 0) {
logger.info("Unable to add events, conflicting events found: {}", conflictingEvents);
throw new SchedulerConflictException("Unable to add event, conflicting events found");
}
}
}
// counter for index into the list of mediapackages
int counter = 0;
for (Period event : periods) {
MediaPackage mediaPackage = (MediaPackage) templateMp.clone();
Date startDate = new Date(event.getStart().getTime());
Date endDate = new Date(event.getEnd().getTime());
Id id = ids.get(counter);
// Get, or make, the DC catalog
DublinCoreCatalog dc;
Opt<DublinCoreCatalog> dcOpt = DublinCoreUtil.loadEpisodeDublinCore(workspace, templateMp);
if (dcOpt.isSome()) {
dc = dcOpt.get();
dc = (DublinCoreCatalog) dc.clone();
// make sure to bind the OC_PROPERTY namespace
dc.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(DublinCores.OC_PROPERTY_NS_PREFIX, DublinCores.OC_PROPERTY_NS_URI)));
} else {
dc = DublinCores.mkOpencastEpisode().getCatalog();
}
// Set the new media package identifier
mediaPackage.setIdentifier(id);
// Update dublincore title and temporal
String newTitle = dc.getFirst(DublinCore.PROPERTY_TITLE) + String.format(" %0" + Integer.toString(periods.size()).length() + "d", ++counter);
dc.set(DublinCore.PROPERTY_TITLE, newTitle);
DublinCoreValue eventTime = EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDate, endDate), Precision.Second);
dc.set(DublinCore.PROPERTY_TEMPORAL, eventTime);
mediaPackage = updateDublincCoreCatalog(mediaPackage, dc);
mediaPackage.setTitle(newTitle);
String mediaPackageId = mediaPackage.getIdentifier().compact();
// Converting from iCal4j DateTime objects to plain Date objects to prevent AMQ issues below
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(event.getStart());
Date startDateTime = cal.getTime();
cal.setTime(event.getEnd());
Date endDateTime = cal.getTime();
// Load dublincore and acl for update
Opt<DublinCoreCatalog> dublinCore = DublinCoreUtil.loadEpisodeDublinCore(workspace, mediaPackage);
Option<AccessControlList> acl = authorizationService.getAcl(mediaPackage, AclScope.Episode);
// Get updated agent properties
Map<String, String> finalCaProperties = getFinalAgentProperties(caMetadata, wfProperties, captureAgentId, seriesId, dublinCore);
// Persist asset
String checksum = calculateChecksum(workspace, getEventCatalogUIAdapterFlavors(), startDateTime, endDateTime, captureAgentId, userIds, mediaPackage, dublinCore, wfProperties, finalCaProperties, optOut, acl.toOpt().getOr(new AccessControlList()));
persistEvent(mediaPackageId, modificationOrigin, checksum, Opt.some(startDateTime), Opt.some(endDateTime), Opt.some(captureAgentId), Opt.some(userIds), Opt.some(mediaPackage), Opt.some(wfProperties), Opt.some(finalCaProperties), Opt.some(optOut), schedulingSource, trxId);
if (trxId.isNone()) {
// Send updates
sendUpdateAddEvent(mediaPackageId, acl.toOpt(), dublinCore, Opt.some(startDateTime), Opt.some(endDateTime), Opt.some(userIds), Opt.some(captureAgentId), Opt.some(finalCaProperties), Opt.some(optOut));
// Update last modified
touchLastEntry(captureAgentId);
}
scheduledEvents.put(mediaPackageId, event);
for (MediaPackageElement mediaPackageElement : mediaPackage.getElements()) {
try {
workspace.delete(mediaPackage.getIdentifier().toString(), mediaPackageElement.getIdentifier());
} catch (NotFoundException | IOException e) {
logger.warn("Failed to delete media package element", e);
}
}
}
return scheduledEvents;
} catch (SchedulerException e) {
throw e;
} catch (Exception e) {
logger.error("Failed to create events: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class SchedulerServiceImplTest method generateEvent.
protected static DublinCoreCatalog generateEvent(String captureDeviceID, Opt<String> eventId, Opt<String> title, Date startTime, Date endTime) {
DublinCoreCatalog dc = DublinCores.mkOpencastEpisode().getCatalog();
dc.set(PROPERTY_IDENTIFIER, eventId.getOr("1"));
dc.set(PROPERTY_TITLE, title.getOr("Demo event"));
dc.set(PROPERTY_CREATOR, "demo");
dc.set(PROPERTY_SUBJECT, "demo");
dc.set(PROPERTY_TEMPORAL, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startTime, endTime), Precision.Second));
dc.set(PROPERTY_SPATIAL, captureDeviceID);
dc.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(new Date(), Precision.Minute));
dc.set(PROPERTY_LANGUAGE, "demo");
dc.set(PROPERTY_CONTRIBUTOR, "demo");
dc.set(PROPERTY_DESCRIPTION, "demo");
return dc;
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class SchedulerServiceImplTest method testEventManagement.
@Test
public void testEventManagement() throws Exception {
MediaPackage mediaPackage = generateEvent(Opt.<String>none());
DublinCoreCatalog event = generateEvent("testdevice", new Date(System.currentTimeMillis() - 2000), new Date(System.currentTimeMillis() + 60000));
event.set(PROPERTY_TITLE, "Demotitle");
event.add(PROPERTY_CREATOR, "creator2");
String catalogId = addDublinCore(Opt.<String>none(), mediaPackage, event);
Map<String, String> caProperties = generateCaptureAgentMetadata("testdevice");
schedSvc.addEvent(new Date(System.currentTimeMillis() - 2000), new Date(System.currentTimeMillis() + 60000), "testdevice", Collections.<String>emptySet(), mediaPackage, wfProperties, caProperties, Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
// test iCalender export
CalendarBuilder calBuilder = new CalendarBuilder();
Calendar cal;
try {
String icalString = schedSvc.getCalendar(Opt.<String>none(), Opt.<String>none(), Opt.<Date>none());
cal = calBuilder.build(IOUtils.toInputStream(icalString, "UTF-8"));
ComponentList vevents = cal.getComponents(VEVENT);
for (int i = 0; i < vevents.size(); i++) {
PropertyList attachments = ((VEvent) vevents.get(i)).getProperties(Property.ATTACH);
for (int j = 0; j < attachments.size(); j++) {
String attached = ((Property) attachments.get(j)).getValue();
String filename = ((Property) attachments.get(j)).getParameter("X-APPLE-FILENAME").getValue();
attached = new String(Base64.decodeBase64(attached));
if ("org.opencastproject.capture.agent.properties".equals(filename)) {
Assert.assertTrue(attached.contains("capture.device.id=testdevice"));
}
if ("episode.xml".equals(filename)) {
Assert.assertTrue(attached.contains("Demotitle"));
}
}
}
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParserException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
// test for upcoming events (it should not be in there).
List<MediaPackage> upcoming = schedSvc.search(Opt.<String>none(), Opt.some(new Date(System.currentTimeMillis())), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none());
Assert.assertTrue(upcoming.isEmpty());
List<MediaPackage> all = schedSvc.search(Opt.<String>none(), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none());
assertEquals(1, all.size());
all = schedSvc.search(Opt.some("somedevice"), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none());
Assert.assertTrue(all.isEmpty());
// update event
event.set(PROPERTY_TEMPORAL, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(new Date(System.currentTimeMillis() + 180000), new Date(System.currentTimeMillis() + 600000)), Precision.Second));
addDublinCore(Opt.some(catalogId), mediaPackage, event);
schedSvc.updateEvent(mediaPackage.getIdentifier().compact(), Opt.some(new Date(System.currentTimeMillis() + 180000)), Opt.some(new Date(System.currentTimeMillis() + 600000)), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.some(wfPropertiesUpdated), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
// test for upcoming events (now it should be there)
upcoming = schedSvc.search(Opt.<String>none(), Opt.some(new Date(System.currentTimeMillis())), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none());
assertEquals(1, upcoming.size());
// delete event
schedSvc.removeEvent(mediaPackage.getIdentifier().compact());
try {
schedSvc.getMediaPackage(mediaPackage.getIdentifier().compact());
Assert.fail();
} catch (NotFoundException e) {
Assert.assertNotNull(e);
}
upcoming = schedSvc.search(Opt.<String>none(), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none(), Opt.<Date>none());
assertEquals(0, upcoming.size());
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class SchedulerRestService method startCapture.
@POST
@Path("capture/{agent}")
@RestQuery(name = "startcapture", description = "Create an immediate event", returnDescription = "If events were successfully generated, status CREATED is returned", pathParameters = { @RestParameter(name = "agent", isRequired = true, type = Type.STRING, description = "The agent identifier") }, restParameters = { @RestParameter(name = "workflowDefinitionId", isRequired = false, type = Type.STRING, description = "The workflow definition id to use") }, reponses = { @RestResponse(responseCode = HttpServletResponse.SC_CREATED, description = "Recording started"), @RestResponse(responseCode = HttpServletResponse.SC_NOT_FOUND, description = "There is no such agent"), @RestResponse(responseCode = HttpServletResponse.SC_CONFLICT, description = "The agent is already recording"), @RestResponse(responseCode = HttpServletResponse.SC_UNAUTHORIZED, description = "You do not have permission to start this immediate capture. Maybe you need to authenticate."), @RestResponse(responseCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE, description = "The agent is not ready to communicate") })
public Response startCapture(@PathParam("agent") String agentId, @FormParam("workflowDefinitionId") String wfId) throws NotFoundException, UnauthorizedException {
if (service == null || agentService == null || prolongingService == null)
return Response.serverError().status(Response.Status.SERVICE_UNAVAILABLE).entity("Scheduler service is unavailable, please wait...").build();
// Lookup the agent. If it doesn't exist, add a temporary registration
boolean adHocRegistration = false;
try {
agentService.getAgent(agentId);
} catch (NotFoundException e) {
Properties adHocProperties = new Properties();
adHocProperties.put(AGENT_REGISTRATION_TYPE, AGENT_REGISTRATION_TYPE_ADHOC);
agentService.setAgentConfiguration(agentId, adHocProperties);
agentService.setAgentState(agentId, AgentState.CAPTURING);
adHocRegistration = true;
logger.info("Temporarily registered agent '{}' for ad-hoc recording", agentId);
}
try {
Date now = new Date();
Date temporaryEndDate = DateTime.now().plus(prolongingService.getInitialTime()).toDate();
try {
List<MediaPackage> events = service.findConflictingEvents(agentId, now, temporaryEndDate);
if (!events.isEmpty()) {
logger.info("An already existing event is in a conflict with the the one to be created on the agent {}!", agentId);
return Response.status(Status.CONFLICT).build();
}
} catch (SchedulerException e) {
logger.error("Unable to create immediate event on agent {}: {}", agentId, e);
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
String workflowId = defaultWorkflowDefinitionId;
if (StringUtils.isNotBlank(wfId))
workflowId = wfId;
Map<String, String> caProperties = new HashMap<>();
caProperties.put("org.opencastproject.workflow.definition", workflowId);
caProperties.put("event.location", agentId);
caProperties.put("event.title", "Capture now event");
// caProperties.put("org.opencastproject.workflow.config.captionHold", "false");
// caProperties.put("org.opencastproject.workflow.config.archiveOp", "true");
// caProperties.put("org.opencastproject.workflow.config.trimHold", "false");
// TODO default metadata? configurable?
// A temporal with start and end period is needed! As well PROPERTY_SPATIAL is needed
DublinCoreCatalog eventCatalog = DublinCores.mkOpencastEpisode().getCatalog();
eventCatalog.set(PROPERTY_TITLE, "Capture now event");
eventCatalog.set(PROPERTY_TEMPORAL, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(now, temporaryEndDate), Precision.Second));
eventCatalog.set(PROPERTY_SPATIAL, agentId);
eventCatalog.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(new Date(), Precision.Minute));
// eventCatalog.set(PROPERTY_CREATOR, "demo");
// eventCatalog.set(PROPERTY_SUBJECT, "demo");
// eventCatalog.set(PROPERTY_LANGUAGE, "demo");
// eventCatalog.set(PROPERTY_CONTRIBUTOR, "demo");
// eventCatalog.set(PROPERTY_DESCRIPTION, "demo");
// TODO workflow properties
Map<String, String> wfProperties = new HashMap<>();
MediaPackage mediaPackage = null;
try {
mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
mediaPackage = addCatalog(workspace, IOUtils.toInputStream(eventCatalog.toXmlString(), "UTF-8"), "dublincore.xml", MediaPackageElements.EPISODE, mediaPackage);
prolongingService.schedule(agentId);
service.addEvent(now, temporaryEndDate, agentId, Collections.<String>emptySet(), mediaPackage, wfProperties, caProperties, Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
return Response.status(Status.CREATED).header("Location", serverUrl + serviceUrl + '/' + mediaPackage.getIdentifier().compact() + ".xml").build();
} catch (Exception e) {
prolongingService.stop(agentId);
if (e instanceof UnauthorizedException)
throw (UnauthorizedException) e;
logger.error("Unable to create immediate event on agent {}: {}", agentId, e);
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if (mediaPackage != null) {
for (MediaPackageElement elem : $(mediaPackage.getElements()).bind(MediaPackageSupport.Filters.byFlavor(MediaPackageElements.EPISODE).toFn())) {
try {
workspace.delete(elem.getURI());
} catch (NotFoundException e) {
logger.warn("Unable to find (and hence, delete), this mediapackage '{}' element '{}'", mediaPackage.getIdentifier(), elem.getIdentifier());
} catch (IOException e) {
chuck(e);
}
}
}
}
} catch (Throwable t) {
throw t;
} finally {
if (adHocRegistration) {
agentService.removeAgent(agentId);
logger.info("Removed temporary registration for agent '{}'", agentId);
}
}
}
Aggregations