log.debug("Processing batch [ {} ] with workflow model [ {} ]", this.getCurrentBatch(resource).getPath(),
workflowModel);
}
final Session session = resource.getResourceResolver().adaptTo(Session.class);
final WorkflowSession workflowSession = workflowService.getWorkflowSession(session);
final WorkflowModel model = workflowSession.getModel(workflowModel);
final Map<String, String> workflowMap = new LinkedHashMap<String, String>();
final Resource currentBatch = this.getCurrentBatch(resource);
final ModifiableValueMap currentProperties = currentBatch.adaptTo(ModifiableValueMap.class);
Resource batchToProcess;
if (currentProperties.get(KEY_STARTED_AT, Date.class) == null) {
currentProperties.put(KEY_STARTED_AT, Calendar.getInstance());
batchToProcess = currentBatch;
log.debug("Virgin batch [ {} ]; preparing to initiate WF.", currentBatch.getPath());
} else {
batchToProcess = this.advance(resource);
log.debug("Completed batch [ {} ]; preparing to advance and initiate WF on next batch [ {} ].",
currentBatch.getPath(), batchToProcess);
}
if (batchToProcess != null) {
for (final Resource child : batchToProcess.getChildren()) {
final ModifiableValueMap properties = child.adaptTo(ModifiableValueMap.class);
final String state = properties.get(KEY_STATE, "");
final String payloadPath = properties.get(KEY_PATH, String.class);
if (StringUtils.isBlank(state)
&& StringUtils.isNotBlank(payloadPath)) {
// Don't try to restart already processed batch items
final Workflow workflow = workflowSession.startWorkflow(model,
workflowSession.newWorkflowData("JCR_PATH", payloadPath));
properties.put(KEY_WORKFLOW_ID, workflow.getId());
properties.put(KEY_STATE, workflow.getState());
workflowMap.put(child.getPath(), workflow.getId());
}