Search in sources :

Example 1 with DCMIPeriod

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);
            }
        }
    }
}
Also used : EName(org.opencastproject.mediapackage.EName) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) HashMap(java.util.HashMap) ProgressInputStream(org.opencastproject.util.ProgressInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) IOException(java.io.IOException) Date(java.util.Date) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) IngestException(org.opencastproject.ingest.api.IngestException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 2 with DCMIPeriod

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);
}
Also used : DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with DCMIPeriod

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");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) RRule(net.fortuna.ical4j.model.property.RRule) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Properties(java.util.Properties) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) LinkedHashMap(java.util.LinkedHashMap) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) HashSet(java.util.HashSet) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) Period(net.fortuna.ical4j.model.Period) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) IOException(java.io.IOException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Date(java.util.Date) DateTimeZone(org.joda.time.DateTimeZone) TimeZone(java.util.TimeZone) JSONObject(org.json.simple.JSONObject) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 4 with DCMIPeriod

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));
}
Also used : DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) DateTime(org.joda.time.DateTime)

Example 5 with DCMIPeriod

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));
    }
}
Also used : DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) DateTime(org.joda.time.DateTime)

Aggregations

DCMIPeriod (org.opencastproject.metadata.dublincore.DCMIPeriod)17 Date (java.util.Date)12 IOException (java.io.IOException)8 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)8 MediaPackage (org.opencastproject.mediapackage.MediaPackage)6 NotFoundException (org.opencastproject.util.NotFoundException)6 ParseException (java.text.ParseException)4 DateTime (org.joda.time.DateTime)4 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)4 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)4 DublinCoreValue (org.opencastproject.metadata.dublincore.DublinCoreValue)4 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)4 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)4 HashMap (java.util.HashMap)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)3 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)3 LinkedHashMap (java.util.LinkedHashMap)2 Properties (java.util.Properties)2 Path (javax.ws.rs.Path)2