/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.workflow.kaleo.runtime.scripting.internal.condition;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.resource.StringResourceRetriever;
import com.liferay.portal.rules.engine.Fact;
import com.liferay.portal.rules.engine.Query;
import com.liferay.portal.rules.engine.RulesEngine;
import com.liferay.portal.rules.engine.RulesResourceRetriever;
import com.liferay.portal.workflow.kaleo.model.KaleoCondition;
import com.liferay.portal.workflow.kaleo.runtime.ExecutionContext;
import com.liferay.portal.workflow.kaleo.runtime.condition.ConditionEvaluator;
import com.liferay.portal.workflow.kaleo.runtime.util.RulesContextBuilder;
import com.liferay.portal.workflow.kaleo.runtime.util.WorkflowContextUtil;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Michael C. Han
*/
@Component(
immediate = true, property = {"scripting.language=drl"},
service = ConditionEvaluator.class
)
public class DRLConditionEvaluator implements ConditionEvaluator {
@Override
public String evaluate(
KaleoCondition kaleoCondition, ExecutionContext executionContext)
throws PortalException {
List<Fact<?>> facts = _rulesContextBuilder.buildRulesContext(
executionContext);
RulesResourceRetriever rulesResourceRetriever =
new RulesResourceRetriever(
new StringResourceRetriever(kaleoCondition.getScript()));
Query query = Query.createStandardQuery();
Map<String, ?> results = _rulesEngine.execute(
rulesResourceRetriever, facts, query);
String returnValue = (String)results.get(_RETURN_VALUE);
Map<String, Serializable> resultsWorkflowContext =
(Map<String, Serializable>)results.get(
WorkflowContextUtil.WORKFLOW_CONTEXT_NAME);
WorkflowContextUtil.mergeWorkflowContexts(
executionContext, resultsWorkflowContext);
if (returnValue != null) {
return returnValue;
}
throw new IllegalStateException(
"Conditional did not return value for script " +
kaleoCondition.getScript());
}
private static final String _RETURN_VALUE = "returnValue";
@Reference
private RulesContextBuilder _rulesContextBuilder;
@Reference
private RulesEngine _rulesEngine;
}