use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class IngestServiceImpl method schedule.
@Override
public void schedule(MediaPackage mediaPackage, String workflowDefinitionID, Map<String, String> properties) throws IllegalStateException, IngestException, NotFoundException, UnauthorizedException, SchedulerException {
MediaPackageElement[] mediaPackageElements = mediaPackage.getElementsByFlavor(MediaPackageElements.EPISODE);
if (mediaPackageElements.length != 1) {
logger.debug("There can be only one (and exactly one) episode dublin core catalog: https://youtu.be/_J3VeogFUOs");
throw new IngestException("There can be only one (and exactly one) episode dublin core catalog");
}
InputStream inputStream;
DublinCoreCatalog dublinCoreCatalog;
try {
inputStream = workingFileRepository.get(mediaPackage.getIdentifier().toString(), mediaPackageElements[0].getIdentifier());
dublinCoreCatalog = dublinCoreService.load(inputStream);
} catch (IOException e) {
throw new IngestException(e);
}
EName temporal = new EName(DublinCore.TERMS_NS_URI, "temporal");
List<DublinCoreValue> periods = dublinCoreCatalog.get(temporal);
if (periods.size() != 1) {
logger.debug("There can be only one (and exactly one) period");
throw new IngestException("There can be only one (and exactly one) period");
}
DCMIPeriod period = EncodingSchemeUtils.decodeMandatoryPeriod(periods.get(0));
if (!period.hasStart() || !period.hasEnd()) {
logger.debug("A scheduled recording needs to have a start and end.");
throw new IngestException("A scheduled recording needs to have a start and end.");
}
EName createdEName = new EName(DublinCore.TERMS_NS_URI, "created");
List<DublinCoreValue> created = dublinCoreCatalog.get(createdEName);
if (created.size() == 0) {
logger.debug("Created not set");
} else if (created.size() == 1) {
Date date = EncodingSchemeUtils.decodeMandatoryDate(created.get(0));
if (date.getTime() != period.getStart().getTime()) {
logger.debug("start and created date differ ({} vs {})", date.getTime(), period.getStart().getTime());
throw new IngestException("Temporal start and created date differ");
}
} else {
logger.debug("There can be only one created date");
throw new IngestException("There can be only one created date");
}
// spatial
EName spatial = new EName(DublinCore.TERMS_NS_URI, "spatial");
List<DublinCoreValue> captureAgents = dublinCoreCatalog.get(spatial);
if (captureAgents.size() != 1) {
logger.debug("Exactly one capture agent needs to be set");
throw new IngestException("Exactly one capture agent needs to be set");
}
String captureAgent = captureAgents.get(0).getValue();
// Go through properties
Map<String, String> agentProperties = new HashMap<>();
Map<String, String> workflowProperties = new HashMap<>();
for (String key : properties.keySet()) {
if (key.startsWith("org.opencastproject.workflow.config.")) {
workflowProperties.put(key, properties.get(key));
} else {
agentProperties.put(key, properties.get(key));
}
}
try {
schedulerService.addEvent(period.getStart(), period.getEnd(), captureAgent, new HashSet<>(), mediaPackage, workflowProperties, agentProperties, Opt.none(), Opt.none(), "ingest-service");
} finally {
for (MediaPackageElement mediaPackageElement : mediaPackage.getElements()) {
try {
workingFileRepository.delete(mediaPackage.getIdentifier().toString(), mediaPackageElement.getIdentifier());
} catch (IOException e) {
logger.warn("Failed to delete media package element", e);
}
}
}
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class DublinCoreCatalogUIAdapterTest method testGetDuration.
@Test
public void testGetDuration() {
int startHour = 19;
int startMinute = 35;
int startSecond = 19;
int endHour = 20;
int endMinute = 48;
int endSecond = 23;
// Test a none() period
Opt<DCMIPeriod> emptyPeriod = Opt.<DCMIPeriod>none();
Long result = DublinCoreMetadataUtil.getDuration(emptyPeriod);
assertEquals(new Long(0L), result);
DateTime periodStart = new DateTime(2014, 11, 04, startHour, startMinute, startSecond, DateTimeZone.UTC);
DateTime periodEnd = new DateTime(2014, 11, 04, endHour, endMinute, endSecond, DateTimeZone.UTC);
// Test a period that is missing the start date
DCMIPeriod withoutStartPeriod = new DCMIPeriod(null, periodEnd.toDate());
result = DublinCoreMetadataUtil.getDuration(Opt.some(withoutStartPeriod));
assertEquals(new Long(0L), result);
// Test a period with a start but no end.
DCMIPeriod withoutEndPeriod = new DCMIPeriod(periodStart.toDate(), null);
result = DublinCoreMetadataUtil.getDuration(Opt.some(withoutEndPeriod));
assertEquals(new Long(0L), result);
// Test a period with both a start and an end.
DCMIPeriod standardPeriod = new DCMIPeriod(periodStart.toDate(), periodEnd.toDate());
result = DublinCoreMetadataUtil.getDuration(Opt.some(standardPeriod));
assertEquals(new Long(4384000L), result);
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class IndexServiceImpl method createEvent.
@Override
public String createEvent(EventHttpServletRequest eventHttpServletRequest) throws ParseException, IOException, MediaPackageException, IngestException, NotFoundException, SchedulerException, UnauthorizedException {
// Preconditions
if (eventHttpServletRequest.getAcl().isNone()) {
throw new IllegalArgumentException("No access control list available to create new event.");
}
if (eventHttpServletRequest.getMediaPackage().isNone()) {
throw new IllegalArgumentException("No mediapackage available to create new event.");
}
if (eventHttpServletRequest.getMetadataList().isNone()) {
throw new IllegalArgumentException("No metadata list available to create new event.");
}
if (eventHttpServletRequest.getProcessing().isNone()) {
throw new IllegalArgumentException("No processing metadata available to create new event.");
}
if (eventHttpServletRequest.getSource().isNone()) {
throw new IllegalArgumentException("No source field metadata available to create new event.");
}
// Get Workflow
String workflowTemplate = (String) eventHttpServletRequest.getProcessing().get().get("workflow");
if (workflowTemplate == null)
throw new IllegalArgumentException("No workflow template in metadata");
// Get Type of Source
SourceType type = getSourceType(eventHttpServletRequest.getSource().get());
MetadataCollection eventMetadata = eventHttpServletRequest.getMetadataList().get().getMetadataByAdapter(eventCatalogUIAdapter).get();
JSONObject sourceMetadata = (JSONObject) eventHttpServletRequest.getSource().get().get("metadata");
if (sourceMetadata != null && (type.equals(SourceType.SCHEDULE_SINGLE) || type.equals(SourceType.SCHEDULE_MULTIPLE))) {
try {
MetadataField<?> current = eventMetadata.getOutputFields().get("location");
eventMetadata.updateStringField(current, (String) sourceMetadata.get("device"));
} catch (Exception e) {
logger.warn("Unable to parse device {}", sourceMetadata.get("device"));
throw new IllegalArgumentException("Unable to parse device");
}
}
Date currentStartDate = null;
MetadataField<?> starttime = eventMetadata.getOutputFields().get(DublinCore.PROPERTY_TEMPORAL.getLocalName());
if (starttime != null && starttime.isUpdated() && starttime.getValue().isSome()) {
DCMIPeriod period = EncodingSchemeUtils.decodeMandatoryPeriod((DublinCoreValue) starttime.getValue().get());
currentStartDate = period.getStart();
}
MetadataField<?> created = eventMetadata.getOutputFields().get(DublinCore.PROPERTY_CREATED.getLocalName());
if (created == null || !created.isUpdated() || created.getValue().isNone()) {
eventMetadata.removeField(created);
MetadataField<String> newCreated = MetadataUtils.copyMetadataField(created);
if (currentStartDate != null) {
newCreated.setValue(EncodingSchemeUtils.encodeDate(currentStartDate, Precision.Second).getValue());
} else {
newCreated.setValue(EncodingSchemeUtils.encodeDate(new Date(), Precision.Second).getValue());
}
eventMetadata.addField(newCreated);
}
// Get presenter usernames for use as technical presenters
Set<String> presenterUsernames = new HashSet<>();
Opt<Set<String>> technicalPresenters = updatePresenters(eventMetadata);
if (technicalPresenters.isSome()) {
presenterUsernames = technicalPresenters.get();
}
eventHttpServletRequest.getMetadataList().get().add(eventCatalogUIAdapter, eventMetadata);
updateMediaPackageMetadata(eventHttpServletRequest.getMediaPackage().get(), eventHttpServletRequest.getMetadataList().get());
DublinCoreCatalog dc = getDublinCoreCatalog(eventHttpServletRequest);
String captureAgentId = null;
TimeZone tz = null;
org.joda.time.DateTime start = null;
org.joda.time.DateTime end = null;
long duration = 0L;
Properties caProperties = new Properties();
RRule rRule = null;
if (sourceMetadata != null && (type.equals(SourceType.SCHEDULE_SINGLE) || type.equals(SourceType.SCHEDULE_MULTIPLE))) {
Properties configuration;
try {
captureAgentId = (String) sourceMetadata.get("device");
configuration = captureAgentStateService.getAgentConfiguration((String) sourceMetadata.get("device"));
} catch (Exception e) {
logger.warn("Unable to parse device {}: because: {}", sourceMetadata.get("device"), getStackTrace(e));
throw new IllegalArgumentException("Unable to parse device");
}
String durationString = (String) sourceMetadata.get("duration");
if (StringUtils.isBlank(durationString))
throw new IllegalArgumentException("No duration in source metadata");
// Create timezone based on CA's reported TZ.
String agentTimeZone = configuration.getProperty("capture.device.timezone");
if (StringUtils.isNotBlank(agentTimeZone)) {
tz = TimeZone.getTimeZone(agentTimeZone);
dc.set(DublinCores.OC_PROPERTY_AGENT_TIMEZONE, tz.getID());
} else {
// No timezone was present, assume the serve's local timezone.
tz = TimeZone.getDefault();
logger.debug("The field 'capture.device.timezone' has not been set in the agent configuration. The default server timezone will be used.");
}
org.joda.time.DateTime now = new org.joda.time.DateTime(DateTimeZone.UTC);
start = now.withMillis(DateTimeSupport.fromUTC((String) sourceMetadata.get("start")));
end = now.withMillis(DateTimeSupport.fromUTC((String) sourceMetadata.get("end")));
duration = Long.parseLong(durationString);
DublinCoreValue period = EncodingSchemeUtils.encodePeriod(new DCMIPeriod(start.toDate(), start.plus(duration).toDate()), Precision.Second);
String inputs = (String) sourceMetadata.get("inputs");
caProperties.putAll(configuration);
dc.set(DublinCore.PROPERTY_TEMPORAL, period);
caProperties.put(CaptureParameters.CAPTURE_DEVICE_NAMES, inputs);
}
if (type.equals(SourceType.SCHEDULE_MULTIPLE)) {
rRule = new RRule((String) sourceMetadata.get("rrule"));
}
Map<String, String> configuration = new HashMap<>();
if (eventHttpServletRequest.getProcessing().get().get("configuration") != null) {
configuration = new HashMap<>((JSONObject) eventHttpServletRequest.getProcessing().get().get("configuration"));
}
for (Entry<String, String> entry : configuration.entrySet()) {
caProperties.put(WORKFLOW_CONFIG_PREFIX.concat(entry.getKey()), entry.getValue());
}
caProperties.put(CaptureParameters.INGEST_WORKFLOW_DEFINITION, workflowTemplate);
eventHttpServletRequest.setMediaPackage(authorizationService.setAcl(eventHttpServletRequest.getMediaPackage().get(), AclScope.Episode, eventHttpServletRequest.getAcl().get()).getA());
MediaPackage mediaPackage;
switch(type) {
case UPLOAD:
case UPLOAD_LATER:
eventHttpServletRequest.setMediaPackage(updateDublincCoreCatalog(eventHttpServletRequest.getMediaPackage().get(), dc));
configuration.put("workflowDefinitionId", workflowTemplate);
WorkflowInstance ingest = ingestService.ingest(eventHttpServletRequest.getMediaPackage().get(), workflowTemplate, configuration);
return eventHttpServletRequest.getMediaPackage().get().getIdentifier().compact();
case SCHEDULE_SINGLE:
mediaPackage = updateDublincCoreCatalog(eventHttpServletRequest.getMediaPackage().get(), dc);
eventHttpServletRequest.setMediaPackage(mediaPackage);
try {
schedulerService.addEvent(start.toDate(), start.plus(duration).toDate(), captureAgentId, presenterUsernames, mediaPackage, configuration, (Map) caProperties, Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
} finally {
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 mediaPackage.getIdentifier().compact();
case SCHEDULE_MULTIPLE:
List<Period> periods = schedulerService.calculatePeriods(rRule, start.toDate(), end.toDate(), duration, tz);
Map<String, Period> scheduled = new LinkedHashMap<>();
scheduled = schedulerService.addMultipleEvents(rRule, start.toDate(), end.toDate(), duration, tz, captureAgentId, presenterUsernames, eventHttpServletRequest.getMediaPackage().get(), configuration, (Map) caProperties, Opt.none(), Opt.none(), SchedulerService.ORIGIN);
return StringUtils.join(scheduled.keySet(), ",");
default:
logger.warn("Unknown source type {}", type);
throw new IllegalArgumentException("Unknown source type");
}
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class DublinCoreMetadataUtil method setDuration.
/**
* Sets the duration in a dublin core catalog to the right value and keeps the start date and start time the same.
*
* @param dc
* The dublin core catalog to adjust
* @param field
* The metadata field that contains the duration.
* @param ename
* The EName in the catalog to identify the property that has the dublin core period.
*/
static void setDuration(DublinCoreCatalog dc, MetadataField<?> field, EName ename) {
if (field.getValue().isNone()) {
logger.error("No value was set for metadata field with dublin core id '{}' and json id '{}'", field.getInputID(), field.getOutputID());
return;
}
// Get the current period
Opt<DCMIPeriod> period = getPeriodFromCatalog(dc, ename);
// Get the current duration
Long duration = 0L;
try {
duration = Long.parseLong(field.getValue().get().toString());
} catch (NumberFormatException e) {
logger.debug("Unable to parse the duration's value '{}' as a long value. Trying it as a period next.", field.getValue().get());
}
if (duration < 1L) {
duration = getDuration(period);
}
// Get the current start date
DateTime startDateTime = getCurrentStartDateTime(period);
// Get the current end date based on new date and duration.
DateTime endDate = new DateTime(startDateTime.toDate().getTime() + duration);
dc.set(ename, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDateTime.toDate(), endDate.toDate()), Precision.Second));
}
use of org.opencastproject.metadata.dublincore.DCMIPeriod in project opencast by opencast.
the class DublinCoreMetadataUtil method setStartDate.
/**
* Sets the start date in a dublin core catalog to the right value and keeps the start time and duration the same.
*
* @param dc
* The dublin core catalog to adjust
* @param field
* The metadata field that contains the start date.
* @param ename
* The EName in the catalog to identify the property that has the dublin core period.
*/
static void setStartDate(DublinCoreCatalog dc, MetadataField<?> field, EName ename) {
if (field.getValue().isNone() || (field.getValue().get() instanceof String && StringUtils.isBlank(field.getValue().get().toString()))) {
logger.debug("No value was set for metadata field with dublin core id '{}' and json id '{}'", field.getInputID(), field.getOutputID());
return;
}
try {
// Get the current date
SimpleDateFormat dateFormat = MetadataField.getSimpleDateFormatter(field.getPattern().get());
Date startDate = dateFormat.parse((String) field.getValue().get());
// Get the current period
Opt<DCMIPeriod> period = getPeriodFromCatalog(dc, ename);
// Get the current duration
Long duration = getDuration(period);
// Get the current end date based on new date and duration.
DateTime endDate = new DateTime(startDate.getTime() + duration);
dc.set(ename, EncodingSchemeUtils.encodePeriod(new DCMIPeriod(startDate, endDate.toDate()), Precision.Second));
// ensure that DC created is start date, see MH-12250
setDate(dc, field, DublinCore.PROPERTY_CREATED);
} catch (ParseException e) {
logger.error("Not able to parse date {} to update the dublin core because: {}", field.getValue(), ExceptionUtils.getStackTrace(e));
}
}
Aggregations