AnsweredAssumed Answered

Delegating to spring beans from JavaDelegate

Question asked by msandoz on Dec 19, 2011
I'd like to be able to create delegate classes that can adapt existing business logic that relies heavily on configured spring beans.

In order to do this, I am planning on putting this logic into my delegate. It is the only way I can see to do what I am trying to do. Is this the best way to do this? I am using activiti 5.8.

Also, since the editor creates activiti:string instead of activiti:expression, I am re-evaluating the expressions using the expression manager.

package org.nexusbpm.activiti.delegate;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.el.Expression;
import org.nexusbpm.service.excel.ExcelServiceImpl;
import org.nexusbpm.service.excel.ExcelServiceRequest;
import org.nexusbpm.service.excel.ExcelServiceResponse;

public class ExcelNexusJavaDelegate extends NexusJavaDelegate implements
        JavaDelegate {

  private Expression skipHeader;
  private Expression rowLimit;
  private Expression columnLimit;
  private Expression excelAnchor;
  private Expression sheetName;
  private Expression dataFile;
  private Expression templateFile;
  private Expression outputFile;

  @Override
  public void execute(DelegateExecution execution) throws Exception {
    ExcelServiceImpl impl = (ExcelServiceImpl) Context.getProcessEngineConfiguration().getBeans().get("excelService");

    ExcelServiceRequest request = new ExcelServiceRequest();
    request.setSkipHeader(evaluateToBoolean(skipHeader, execution));
    request.setRowLimit(evaluateToInt(rowLimit, execution));
    request.setColumnLimit(evaluateToInt(columnLimit, execution));
    request.setExcelAnchor(evaluateToString(excelAnchor, execution));
    request.setSheetName(evaluateToString(sheetName, execution));
    request.setDataFile(evaluateToUri(dataFile, execution));
    request.setOutputFile(evaluateToUri(outputFile, execution));
    request.setTemplateFile(evaluateToUri(templateFile, execution));
    ExcelServiceResponse response = (ExcelServiceResponse) impl.execute(request);
  }
}

here's the base class:

package org.nexusbpm.activiti.delegate;

import java.net.URI;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.el.Expression;

public class NexusJavaDelegate {

  protected String evaluateToString(Expression expression, DelegateExecution execution) {
    return evaluate(expression, execution).toString();
  }

  protected Boolean evaluateToBoolean(Expression expression, DelegateExecution execution) {
    return Boolean.valueOf(evaluate(expression, execution).toString());
  }

  protected Integer evaluateToInt(Expression expression, DelegateExecution execution) {
    return Integer.valueOf(evaluate(expression, execution).toString());
  }

  protected URI evaluateToUri(Expression expression, DelegateExecution execution) {
    return URI.create(evaluate(expression, execution).toString());
  }

  protected Object evaluate(Expression expression, DelegateExecution execution) {
    return Context.getProcessEngineConfiguration().getExpressionManager().createExpression(expression.getExpressionText()).getValue(execution);
  }
}

Outcomes