Search in sources :

Example 6 with DCMIPeriod

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;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) Temporal(org.opencastproject.metadata.dublincore.Temporal) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) IOException(java.io.IOException) Date(java.util.Date)

Example 7 with DCMIPeriod

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);
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) NotFoundException(org.opencastproject.util.NotFoundException) Log.getHumanReadableTimeString(org.opencastproject.util.Log.getHumanReadableTimeString) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) LinkedHashMap(java.util.LinkedHashMap) Predicate(org.opencastproject.assetmanager.api.query.Predicate) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) AResult(org.opencastproject.assetmanager.api.query.AResult) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) Calendar(java.util.Calendar) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) Period(net.fortuna.ical4j.model.Period) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Date(java.util.Date) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) IOException(java.io.IOException) ServiceException(org.osgi.framework.ServiceException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ValidationException(net.fortuna.ical4j.model.ValidationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) PropertyId(org.opencastproject.assetmanager.api.PropertyId) Id(org.opencastproject.mediapackage.identifier.Id) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 8 with DCMIPeriod

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;
}
Also used : DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Date(java.util.Date)

Example 9 with DCMIPeriod

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());
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) ParserException(net.fortuna.ical4j.data.ParserException) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) Calendar(net.fortuna.ical4j.model.Calendar) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) NotFoundException(org.opencastproject.util.NotFoundException) ComponentList(net.fortuna.ical4j.model.ComponentList) IOException(java.io.IOException) Date(java.util.Date) PropertyList(net.fortuna.ical4j.model.PropertyList) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Property(net.fortuna.ical4j.model.Property) Test(org.junit.Test)

Example 10 with DCMIPeriod

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);
        }
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ParseException(java.text.ParseException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

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