/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.bpmn.analytics.publisher.handlers;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.bpmn.parser.handler.AbstractBpmnParseHandler;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.bpmn.analytics.publisher.listeners.ProcessTerminationKPIListener;
import java.util.List;
public class ProcessKPIParseHandler extends AbstractBpmnParseHandler {
private static final Log log = LogFactory.getLog(ProcessKPIParseHandler.class);
@Override
protected Class<? extends BaseElement> getHandledType() {
return org.activiti.bpmn.model.Process.class;
}
@Override
protected void executeParse(BpmnParse bpmnParse, BaseElement baseElement) {
ProcessDefinitionEntity processDefinitionEntity = bpmnParse.getCurrentProcessDefinition();
// We have to check if the data publishing KPI listener has already been associated at server startup.
ExecutionListener processTerminationKPIListener = null;
List<ExecutionListener> endListeners = processDefinitionEntity.getExecutionListeners(PvmEvent.EVENTNAME_END);
if (endListeners != null) {
for (ExecutionListener listener : endListeners) {
if (listener instanceof ProcessTerminationKPIListener) {
processTerminationKPIListener = listener;
}
}
}
if (processTerminationKPIListener == null) {
if (log.isDebugEnabled()) {
log.debug("Enabling data publishing for process: " + processDefinitionEntity.getName());
}
processDefinitionEntity.addExecutionListener(PvmEvent.EVENTNAME_END, new ProcessTerminationKPIListener());
}
}
}