/* 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.activiti.compatibility.spring;

import org.activiti.engine.ProcessEngine;
import org.flowable.compatibility.DefaultProcessEngineFactory;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.spring.SpringProcessEngineConfiguration;

public class DefaultSpringProcessEngineFactory extends DefaultProcessEngineFactory {

    /**
     * Takes in an Activiti 6 process engine config, gives back an Activiti 5 Process engine.
     */
    @Override
    public ProcessEngine buildProcessEngine(ProcessEngineConfigurationImpl activiti6Configuration) {

        org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5Configuration = null;
        if (activiti6Configuration instanceof SpringProcessEngineConfiguration) {
            activiti5Configuration = new org.activiti.spring.SpringProcessEngineConfiguration();
            super.copyConfigItems(activiti6Configuration, activiti5Configuration);
            copySpringConfigItems((SpringProcessEngineConfiguration) activiti6Configuration, (org.activiti.spring.SpringProcessEngineConfiguration) activiti5Configuration);
            return activiti5Configuration.buildProcessEngine();

        } else {
            return super.buildProcessEngine(activiti6Configuration);
        }

    }

    protected void copySpringConfigItems(SpringProcessEngineConfiguration activiti6Configuration, org.activiti.spring.SpringProcessEngineConfiguration activiti5Configuration) {
        activiti5Configuration.setApplicationContext(activiti6Configuration.getApplicationContext());
        activiti5Configuration.setTransactionManager(activiti6Configuration.getTransactionManager());
    }

}

@@ -600,16 +600,39 @@
 pring;%0A%0A
+import java.util.Map;%0A%0A
 import o
@@ -665,16 +665,131 @@
 Engine;%0A
+import org.activiti.engine.impl.cfg.SpringBeanFactoryProxyMap;%0Aimport org.activiti.spring.SpringExpressionManager;%0A
 import o
@@ -1086,25 +1086,17 @@
 s in an 
-Activiti 
+V
 6 proces
@@ -1130,17 +1130,9 @@
  an 
-Activiti 
+V
 5 Pr
@@ -1242,24 +1242,17 @@
 ionImpl 
-activiti
+v
 6Configu
@@ -1330,24 +1330,17 @@
 ionImpl 
-activiti
+v
 5Configu
@@ -1366,24 +1366,17 @@
     if (
-activiti
+v
 6Configu
@@ -1437,32 +1437,25 @@
             
-activiti
+v
 5Configurati
@@ -1553,24 +1553,17 @@
 igItems(
-activiti
+v
 6Configu
@@ -1570,24 +1570,17 @@
 ration, 
-activiti
+v
 5Configu
@@ -1657,24 +1657,17 @@
 ration) 
-activiti
+v
 6Configu
@@ -1729,24 +1729,17 @@
 ration) 
-activiti
+v
 5Configu
@@ -1766,24 +1766,17 @@
  return 
-activiti
+v
 5Configu
@@ -1866,24 +1866,17 @@
 sEngine(
-activiti
+v
 6Configu
@@ -1976,24 +1976,17 @@
 uration 
-activiti
+v
 6Configu
@@ -2046,24 +2046,17 @@
 uration 
-activiti
+v
 5Configu
@@ -2069,32 +2069,25 @@
 ) %7B%0A        
-activiti
+v
 5Configurati
@@ -2111,24 +2111,17 @@
 Context(
-activiti
+v
 6Configu
@@ -2165,17 +2165,48 @@
     
-activiti5
+v5Configuration.setTransactionManager(v6
 Conf
@@ -2207,33 +2207,33 @@
 v6Configuration.
-s
+g
 etTransactionMan
@@ -2241,56 +2241,492 @@
 ger(
-activiti6Configuration.getTransactionManager());
+));%0A%0A        Map%3CObject, Object%3E beans = v6Configuration.getBeans();%0A%0A        if (!(beans instanceof org.flowable.common.engine.impl.cfg.SpringBeanFactoryProxyMap)) %7B%0A            v5Configuration.setBeans(new SpringBeanFactoryProxyMap(v6Configuration.getApplicationContext()));%0A        %7D%0A        if (v5Configuration.getExpressionManager() == null) %7B%0A            v5Configuration.setExpressionManager(new SpringExpressionManager(v6Configuration.getApplicationContext(), null));%0A        %7D
 %0A   

Fix expressionManager creation when using v5 embedded engine (see https://forum.flowable.org/t/delegate-expressions-not-found-for-processes-running-in-v5-compatibility/4996/)