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));
}
});
}
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();
}
}
Aggregations