/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的Dao或者多个Dao来实现数据库操作
 * 实际的数据库操作在对应的Dao或其他Dao中实现
 */

package com.blueland.ebpm.engine.core.script;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.javax.el.ELContext;
import org.activiti.engine.impl.javax.el.ExpressionFactory;
import org.activiti.engine.impl.juel.ExpressionFactoryImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

import com.blueland.bpm.common.util.ApplicationContextHolder;
import com.blueland.ebpm.engine.BaseManagerTestCase;
import com.blueland.bpm.engine.core.model.BusinessDataObject;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.service.GroovyScriptEngine;
import com.blueland.bpm.engine.util.ActivityTranstionDelegateExecution;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.bpm.identity.service.UserServiceImpl;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author lw
 * @version 1.0
 * @since
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class GroovyScriptEngineTest extends BaseManagerTestCase {
	@Autowired
	GroovyScriptEngine groovyScriptEngine;
	UserServiceImpl userServiceImpl;

	 @Test
	public void testexecute() {
		Map params = new HashMap();
		params.put("prince","2000");
		groovyScriptEngine.execute(
				"return Integer.parseInt(prince)<1000", params);
	}
	 @Test
		public void testexecutes() {
			Map params = new HashMap();
			params.put("prince","2000");
			System.out.println(groovyScriptEngine.executeString(
					"return true;", params));
		}
	 
	 
	@Test
	public void testexecute1() {
		Map params = new HashMap();
		params.put("wf_sender", "gaof0709");
		System.out.println(groovyScriptEngine.executeBoolean(
				"return wf_sender=='gaof0709'", params));
	}

	@Test
	public void testexecute4() {
		Map params = new HashMap();
		params.put("a","4");
		System.out.println(groovyScriptEngine.executeBoolean(
				"return Integer.parseInt(a)<7", params));
	}
	@Test
	public void testexecute2() {
		List<ProcessInputModel> processInputModels = new ArrayList<ProcessInputModel>();
		ProcessInputModel communicate = new ProcessInputModel();
		communicate.setWf_appId("STDD_SYS");
		communicate
				.setWf_procTitle("自动化模拟测试流程:test_wait_task20150714-11:08:23");
		communicate.setWf_procInstId("986162a3-29d5-11e5-b8f3-68f72803f161");
		communicate
				.setWf_curActDefId("sid-393FB09C-32D8-4FAA-97B0-8BF779BFCFDB");
		communicate
				.setWf_actionType(WorkFlowContants.ACTION_TYPE_SIGNAL_RECEIVE_ACTIVITY);
		BusinessDataObject businessDataObject = new BusinessDataObject();
		businessDataObject.setBizData("2");
		businessDataObject.setObjectType("aa");
		communicate.setWf_businessDataObject(businessDataObject);

		Map fieldsMap = new HashMap();
		fieldsMap.put("leaveDay", "2");
		communicate.setFields(fieldsMap);
		ProcessInputModel communicate2 = new ProcessInputModel();
		// communicate.setFields(fieldsMap);
		communicate2.setWf_procDefId("my-leave-manual:2:19004");
		communicate2.setWf_procTitle("1");
		communicate2.setWf_procInstId("19101");
		communicate2.setWf_curActInstId("19905");
		communicate2.setWf_curActDefId("deptLeader_sp");
		BusinessDataObject businessDataObject2 = new BusinessDataObject();
		businessDataObject.setBizData("1");
		businessDataObject.setObjectType("aa");
		communicate2.setWf_businessDataObject(businessDataObject2);

		Map fieldsMap2 = new HashMap();
		fieldsMap2.put("leaveDay", "1");
		communicate2.setFields(fieldsMap2);

		ProcessInputModel communicate3 = new ProcessInputModel();
		// communicate.setFields(fieldsMap);
		communicate3.setWf_procDefId("my-leave-manual:2:19004");
		communicate3.setWf_procTitle("1");
		communicate3.setWf_procInstId("19101");
		communicate3.setWf_curActInstId("19905");
		communicate3.setWf_curActDefId("deptLeader_sp");
		BusinessDataObject businessDataObject3 = new BusinessDataObject();
		businessDataObject.setBizData("1");
		businessDataObject.setObjectType("aa");
		communicate3.setWf_businessDataObject(businessDataObject3);
		Map fieldsMap3 = new HashMap();
		fieldsMap3.put("leaveDay", "1");
		communicate3.setFields(fieldsMap2);
		processInputModels.add(communicate);
		processInputModels.add(communicate2);
		processInputModels.add(communicate3);
		Map<String, Object> filterVariables = new HashMap<String, Object>();
		filterVariables.put("processInputModels", processInputModels);
		String script = "int count=0;for(int i=0;i<processInputModels.size();i++)"
				+ "{"
				+ "com.blueland.bpm.engine.core.model.ProcessInputModel processInputModel=processInputModels.get(i);"
				+ "Map<String,Object> bizDataMap=processInputModel.getFields();"
				+ "String  leaveDay=(String)bizDataMap.get('leaveDay');"
				+ "int day=Integer.parseInt(leaveDay);"
				+ "count+=day;"
				+ "System.out.println(day);"
				+ "};"
				+ "System.out.println(count);if(count>3){return true;}else{return false;}";
		boolean result = groovyScriptEngine.executeBoolean(script,
				filterVariables);
		System.out.println(script);
		System.out.println(result);
	}

	@Test
	public void testJuel() {
		Map<Object, Object> beans = new HashMap<Object, Object>();
		String[] beanNames = ApplicationContextHolder.getApplicationContext()
				.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			beans.put(beanName, ApplicationContextHolder
					.getApplicationContext().getBean(beanName));
		}
		System.out.println(beans);
		String conditionText = "return startUser=='gaof0709'";
		Map conditionMap = new HashMap();
		conditionMap.put("startUser", "gaof0709");
		ExpressionManager expressionManager = new ExpressionManager(beans);

		ActivityTranstionDelegateExecution atde = new ActivityTranstionDelegateExecution(
				conditionMap, null);
		ELContext elContext = expressionManager.getElContext(atde);
		ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
		org.activiti.engine.impl.javax.el.ValueExpression valueExpression = expressionFactory
				.createValueExpression(elContext, conditionText.trim(),
						Object.class);
		Object obj = valueExpression.getValue(elContext);
		System.out.println(obj);
	}
}
