//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.tree;

import java.util.Iterator;
import java.util.List;

import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowApplication;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.activity.Implementation;
import org.huihoo.workflow.xpdl.activity.SubFlowImplementation;
import org.huihoo.workflow.xpdl.activity.ToolImplementation;
import org.huihoo.workflow.xpdl.activity.ToolSet;
import org.huihoo.workflow.xpdl.parameter.ActualParameter;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ExplorerUtilities
{
	//---------------------------------------------------------------------
	// Node Type Check
	//---------------------------------------------------------------------

	public static boolean isNullNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return true;
		}
		
		return false;
	}

	public static boolean isPackageNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_PACKAGE;
	}

	public static boolean isProcessesNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_PROCESSES;
	}

	public static boolean isProcessNode(ExplorerNode treeNode)
	{
		return treeNode.getNodeType()  == ExplorerConstants.NODE_PROCESS;
	}

	public static boolean isActivitiesNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_ACTIVITIES;
	}

	public static boolean isActivityNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		int activityMask = ExplorerConstants.NODE_ACTIVITY >> 8;
		return (treeNode.getNodeType()  >> 8) == activityMask;
	}

	public static boolean isTransitionsNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_TRANSITIONS;
	}

	public static boolean isTransitionNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_TRANSITION;
	}

	public static boolean isApplicationsNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_APPLICATIONS;
	}

	public static boolean isApplicationNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_APPLICATION;
	}

	public static boolean isParametersNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_PARAMETERS;
	}

	public static boolean isParameterNode(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_PARAMETER;
	}

	public static boolean isBeginActivity(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_ACTIVITY_BEGIN;
	}

	public static boolean isEndActivity(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_ACTIVITY_END;
	}

	public static boolean isSubFlowActivity(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_ACTIVITY_SUBFLOW;
	}

	public static boolean isRouteActivity(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType()  == ExplorerConstants.NODE_ACTIVITY_ROUTE;
	}

	public static boolean isBusinessActivity(ExplorerNode treeNode)
	{
		if(treeNode == null)
		{
		  return false;
		}
		
		return treeNode.getNodeType() == ExplorerConstants.NODE_ACTIVITY_BUSINESS;
	}
	
	//---------------------------------------------------------------------
	// Node Remove Check
	//---------------------------------------------------------------------

	public static boolean canRemoveWorkflowProcess(
		WorkflowPackage workflowPackage,
		WorkflowProcess toRemove)
	{

		boolean remove = true;
		Iterator it = workflowPackage.getWorkflowProcesses().iterator();
		while (it.hasNext())
		{
			WorkflowProcess wp = (WorkflowProcess) it.next();
			// do not check itself and external workflows
			if (wp != toRemove)
			{
				List acts = wp.getWorkflowActivities();
				remove = canRemoveWorkflowProcess(acts, workflowPackage, toRemove);
				if (!remove)
				{
					break;
				}
			}
		}
		return remove;
	}

	public static boolean canRemoveWorkflowApplication(
		WorkflowPackage workflowPackage,
		WorkflowApplication toRemove)
	{
		boolean remove = true;
		Iterator it = workflowPackage.getWorkflowProcesses().iterator();
		while (it.hasNext())
		{
			WorkflowProcess wp = (WorkflowProcess) it.next();

			List acts = wp.getWorkflowActivities();
			remove = canRemoveWorkflowApplication(acts, workflowPackage, toRemove);
			if (!remove)
			{
				break;
			}
		}
		return remove;
	}

	public static boolean canRemoveWorkflowParameter(
		WorkflowPackage workflowPackage,
		WorkflowParameter toRemove)
	{
		boolean remove = true;
		Iterator it = workflowPackage.getWorkflowProcesses().iterator();
		while (it.hasNext())
		{
			WorkflowProcess wp = (WorkflowProcess) it.next();

			List acts = wp.getWorkflowActivities();
			remove = canRemoveWorkflowParameter(acts, workflowPackage, toRemove);
			if (!remove)
			{
				break;
			}
		}
		return remove;
	}

	protected static boolean canRemoveWorkflowProcess(
		List activitiesList,
		WorkflowPackage workflowPackage,
		WorkflowProcess toRemove)
	{
    Iterator it = activitiesList.iterator();
    while (it.hasNext())
    {
      WorkflowActivity act=(WorkflowActivity)it.next();
      Implementation impl= act.getImplementation();
      
      if(impl!=null && (impl instanceof SubFlowImplementation))
      {
        SubFlowImplementation subflow=(SubFlowImplementation)impl;
        if(toRemove.getUUID().equals(subflow.getProcessID()))
        {
          return false;
        }
      }
    }
		return true;
	}

	protected static boolean canRemoveWorkflowApplication(
		List activitiesList,
		WorkflowPackage workflowPackage,
		WorkflowApplication toRemove)
	{
    Iterator it = activitiesList.iterator();
    while (it.hasNext())
    {
      WorkflowActivity act=(WorkflowActivity)it.next();
      Implementation impl= act.getImplementation();
      
      if(impl!=null)
      {
        if(impl instanceof ToolImplementation)
        {
          ToolImplementation toolImpl=(ToolImplementation)impl;
          if(toRemove.getUUID().equals(toolImpl.getApplicationID()))
          {
            return false;
          }
        }
        else if (impl instanceof ToolSet)
        {
          ToolSet toolSet=(ToolSet)impl;
          List impls=toolSet.getToolKits();
          int size=impls.size();
          for(int i=0;i<size;++i)
          {
            ToolImplementation toolImpl=(ToolImplementation)impls.get(i);
            if(toRemove.getUUID().equals(toolImpl.getApplicationID()))
            {
              return false;
            }
          }
        }
        
      }
    }
		return true;
	}

	protected static boolean canRemoveWorkflowParameter(
		List activitiesList,
		WorkflowPackage workflowPackage,
		WorkflowParameter toRemove)
	{
    Iterator it = activitiesList.iterator();
    while (it.hasNext())
    {
      WorkflowActivity act=(WorkflowActivity)it.next();
      Implementation impl= act.getImplementation();
      
      if(impl!=null)
      {
        if(impl instanceof ToolImplementation)
        {
          ToolImplementation toolImpl=(ToolImplementation)impl;
          Iterator itAPs=toolImpl.getActualParameters().iterator();
          while(itAPs.hasNext())
          {
            ActualParameter ap = (ActualParameter) itAPs.next();
            if(toRemove.getUUID().equals(ap.getUUID()))
            {
              return false;
            }           
          }

        }
        else if (impl instanceof ToolSet)
        {
          ToolSet toolSet=(ToolSet)impl;
          List impls=toolSet.getToolKits();
          int size=impls.size();
          for(int i=0;i<size;++i)
          {
            ToolImplementation toolImpl=(ToolImplementation)impls.get(i);
            Iterator itAPs=toolImpl.getActualParameters().iterator();
            while(itAPs.hasNext())
            {
              ActualParameter ap = (ActualParameter) itAPs.next();
              if(toRemove.getUUID().equals(ap.getUUID()))
              {
                return false;
              }           
            }
          }
        }
        
      }
    }
		return true;
	}
}
