Search in sources :

Example 1 with X

use of org.opencastproject.util.data.Function0.X in project opencast by opencast.

the class SeriesServiceImpl method repopulate.

@Override
public void repopulate(final String indexName) {
    final String destinationId = SeriesItem.SERIES_QUEUE_PREFIX + indexName.substring(0, 1).toUpperCase() + indexName.substring(1);
    try {
        final int total = persistence.countSeries();
        logger.info("Re-populating '{}' index with series. There are {} series to add to the index.", indexName, total);
        final int responseInterval = (total < 100) ? 1 : (total / 100);
        List<SeriesEntity> databaseSeries = persistence.getAllSeries();
        int current = 1;
        for (SeriesEntity series : databaseSeries) {
            Organization organization = orgDirectory.getOrganization(series.getOrganization());
            SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(systemUserName, organization), new Function0.X<Void>() {

                @Override
                public Void xapply() throws Exception {
                    String id = series.getSeriesId();
                    logger.trace("Adding series '{}' for org '{}'", id, series.getOrganization());
                    DublinCoreCatalog catalog = DublinCoreXmlFormat.read(series.getDublinCoreXML());
                    messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateCatalog(catalog));
                    AccessControlList acl = AccessControlParser.parseAcl(series.getAccessControl());
                    if (acl != null) {
                        messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateAcl(id, acl));
                    }
                    messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateOptOut(id, series.isOptOut()));
                    for (Entry<String, String> property : persistence.getSeriesProperties(id).entrySet()) {
                        messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, SeriesItem.updateProperty(id, property.getKey(), property.getValue()));
                    }
                    return null;
                }
            });
            if ((current % responseInterval == 0) || (current == total)) {
                logger.info("Initializing {} series index rebuild {}/{}: {} percent", indexName, current, total, current * 100 / total);
            }
            current++;
        }
        logger.info("Finished initializing '{}' index rebuild", indexName);
    } catch (Exception e) {
        logger.warn("Unable to index series instances:", e);
        throw new ServiceException(e.getMessage());
    }
    Organization organization = new DefaultOrganization();
    SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(systemUserName, organization), new Effect0() {

        @Override
        protected void run() {
            messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Series));
        }
    });
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) SeriesEntity(org.opencastproject.series.impl.persistence.SeriesEntity) Function0(org.opencastproject.util.data.Function0) ServiceException(org.osgi.framework.ServiceException) SeriesException(org.opencastproject.series.api.SeriesException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) FunctionException(org.opencastproject.util.data.FunctionException) Entry(java.util.Map.Entry) ServiceException(org.osgi.framework.ServiceException) Effect0(org.opencastproject.util.data.Effect0) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Example 2 with X

use of org.opencastproject.util.data.Function0.X in project opencast by opencast.

the class IngestRestService method ingest.

private Response ingest(MultivaluedMap<String, String> formData, String wdID) {
    /**
     * Note: We use a MultivaluedMap here to ensure that we can get any arbitrary form parameters. This is required to
     * enable things like holding for trim or distributing to YouTube.
     */
    final Map<String, String> wfConfig = getWorkflowConfig(formData);
    if (StringUtils.isNotBlank(wdID))
        wfConfig.put(WORKFLOW_DEFINITION_ID_PARAM, wdID);
    final MediaPackage mp;
    try {
        mp = factory.newMediaPackageBuilder().loadFromXml(formData.getFirst("mediaPackage"));
        if (MediaPackageSupport.sanityCheck(mp).isSome()) {
            logger.warn("Rejected ingest with invalid mediapackage {}", mp);
            return Response.status(Status.BAD_REQUEST).build();
        }
    } catch (Exception e) {
        logger.warn("Rejected ingest without mediapackage");
        return Response.status(Status.BAD_REQUEST).build();
    }
    final String workflowInstance = wfConfig.get(WORKFLOW_INSTANCE_ID_PARAM);
    final String workflowDefinition = wfConfig.get(WORKFLOW_DEFINITION_ID_PARAM);
    // Adding ingest start time to workflow configuration
    wfConfig.put(IngestService.START_DATE_KEY, formatter.format(startCache.asMap().get(mp.getIdentifier().toString())));
    final X<WorkflowInstance> ingest = new X<WorkflowInstance>() {

        @Override
        public WorkflowInstance xapply() throws Exception {
            /* Legacy support: Try to convert the workflowInstance to integer */
            Long workflowInstanceId = null;
            if (StringUtils.isNotBlank(workflowInstance)) {
                try {
                    workflowInstanceId = Long.parseLong(workflowInstance);
                } catch (NumberFormatException e) {
                    // The workflowId is not a long value and might be the media package identifier
                    wfConfig.put(IngestServiceImpl.LEGACY_MEDIAPACKAGE_ID_KEY, workflowInstance);
                }
            }
            if (workflowInstanceId != null) {
                return ingestService.ingest(mp, trimToNull(workflowDefinition), wfConfig, workflowInstanceId);
            } else {
                return ingestService.ingest(mp, trimToNull(workflowDefinition), wfConfig);
            }
        }
    };
    try {
        WorkflowInstance workflow = ingest.apply();
        startCache.asMap().remove(mp.getIdentifier().toString());
        return Response.ok(WorkflowParser.toXml(workflow)).build();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) X(org.opencastproject.util.data.Function0.X) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IngestException(org.opencastproject.ingest.api.IngestException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Aggregations

UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)2 NotFoundException (org.opencastproject.util.NotFoundException)2 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 IngestException (org.opencastproject.ingest.api.IngestException)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)1 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)1 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)1 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)1 AccessControlList (org.opencastproject.security.api.AccessControlList)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 Organization (org.opencastproject.security.api.Organization)1 SeriesException (org.opencastproject.series.api.SeriesException)1 SeriesEntity (org.opencastproject.series.impl.persistence.SeriesEntity)1 Effect0 (org.opencastproject.util.data.Effect0)1 Function0 (org.opencastproject.util.data.Function0)1 X (org.opencastproject.util.data.Function0.X)1 FunctionException (org.opencastproject.util.data.FunctionException)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1