package com.blueland.bpm.engine.util;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.activiti.bpmn.model.ExpandProperty;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.TransitionImpl;


public class ComparatorTransition  implements Comparator<PvmTransition>{

	@Override
	public int compare(PvmTransition pvm1, PvmTransition pvm2) {
		TransitionImpl transition1=(TransitionImpl) pvm1;
		TransitionImpl transition2=(TransitionImpl) pvm2;
		ExpandProperty expandProperty1=ProcessDefinitionUtils.findTransitionOrder(transition1);
		ExpandProperty expandProperty2=ProcessDefinitionUtils.findTransitionOrder(transition2);
		String val1="99";
		String val2="99";
		if(expandProperty1!=null){
			val1=expandProperty1.getValue();
		}
		if(expandProperty2!=null){
			val2=expandProperty2.getValue();
		}
		return val1.compareTo(val2);
	}
	public static void main(String[] args) {
		List<TransitionImpl> ts=new ArrayList<TransitionImpl>();
		TransitionImpl t1=new TransitionImpl("r1",null);
		TransitionImpl t2=new TransitionImpl("r2",null);
		TransitionImpl t3=new TransitionImpl("r3",null);
		ts.add(t1);
		ts.add(t2);
		ts.add(t3);
			ExpandProperty expandProperty1=new ExpandProperty();
			expandProperty1.setId("TRANSITION_DISPLAY_ORDER");
			expandProperty1.setValue("3");
			List aa=new ArrayList();
			aa.add(expandProperty1);
		//t1.setExpandPropertys(aa);
			ExpandProperty expandProperty2=new ExpandProperty();
			expandProperty2.setId("TRANSITION_DISPLAY_ORDER");
			expandProperty2.setValue("1");
			List aa2=new ArrayList();
			aa2.add(expandProperty2);
		t2.setExpandPropertys(aa2);
		
			ExpandProperty expandProperty3=new ExpandProperty();
			expandProperty3.setId("TRANSITION_DISPLAY_ORDER");
			expandProperty3.setValue("2");
			List aa3=new ArrayList();
			aa3.add(expandProperty3);
		t3.setExpandPropertys(aa3);
		
		System.out.println("before----");
		for (TransitionImpl t : ts) {
			System.out.println(t.getId());
		}
	
		Collections.sort(ts,new ComparatorTransition());
		System.out.println("after----");
		for (TransitionImpl t : ts) {
			System.out.println(t.getId());
		}
	}
	
	

}
