⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializableworkflowstore.cs

📁 基于DotNet的工作流引擎实现
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using DotNetTools.PropertySet;
using DotNetTools.Workflow.Query;
using log4net;
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
namespace DotNetTools.Workflow.Spi.Memory
{
	/// <summary> Simple flat file implementation.
	/// *
	/// Following properties are <b>required</b>:
	/// <ul>
	/// <li><b>storeFile</b> - the absolute path to the store file
	/// (<i>ex:c:\workflow.store</i>)</li>
	/// </ul>
	/// *
	/// </summary>
	/// <author>  <a href="mailto:gbort@msn.com">Guillaume Bort</a>
	/// 
	/// </author>
	public class SerializableWorkflowStore:MemoryWorkflowStore
	{
		public static String StoreFile
		{
			get
			{
				return storeFile;
			}
			
			set
			{
				SerializableWorkflowStore.storeFile = value;
			}
			
		}
		//~ Static fields/initializers /////////////////////////////////////////////
		
		protected internal static readonly ILog log;
		internal static String storeFile;
		
		//~ Methods ////////////////////////////////////////////////////////////////
		
		public override IPropertySet GetPropertySet(long entryId)
		{
			IPropertySet ps = (IPropertySet) SerializableCache.Instance.propertySetCache[entryId];
			
			if (ps == null)
			{
				ps = PropertySetManager.GetInstance("serializable", null);
				SerializableCache.Instance.propertySetCache[entryId]= ps;
			}
			
			return ps;
		}
		
		
		
		public override IStep CreateCurrentStep(long entryId, int stepId, String owner, DateTime startDate, DateTime dueDate, String status, long[] previousIds)
		{
			long id = SerializableCache.Instance.globalStepId++;
			SimpleStep step = new SimpleStep(id, entryId, stepId, 0, owner, startDate, dueDate, DateTime.MaxValue, status, previousIds, null);
			
			IList currentSteps = (IList) SerializableCache.Instance.currentStepsCache[entryId];
			
			if (currentSteps == null)
			{
				currentSteps = new ArrayList();
				SerializableCache.Instance.currentStepsCache[entryId]=currentSteps;
			}
			
			currentSteps.Add(step);
			SerializableCache.store();
			
			return step;
		}
		
		public override IWorkflowEntry CreateEntry(String workflowName)
		{
			long id = SerializableCache.Instance.globalEntryId++;
			SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id, workflowName, SimpleWorkflowEntry.CREATED);
			SerializableCache.Instance.entryCache[id]= entry;
			SerializableCache.store();
			
			return entry;
		}
		
		public override IList FindCurrentSteps(long entryId)
		{
			IList currentSteps = (IList) SerializableCache.Instance.currentStepsCache[entryId];
			
			if (currentSteps == null)
			{
				currentSteps = new ArrayList();
				SerializableCache.Instance.currentStepsCache[entryId]= currentSteps;
			}
			
			return currentSteps;
		}
		
		public override IWorkflowEntry FindEntry(long entryId)
		{
			return (IWorkflowEntry) SerializableCache.Instance.entryCache[entryId];
		}
		
		public override IList FindHistorySteps(long entryId)
		{
			IList historySteps = (IList) SerializableCache.Instance.historyStepsCache[entryId];
			
			if (historySteps == null)
			{
				historySteps = new ArrayList();
				SerializableCache.Instance.historyStepsCache[entryId]= historySteps;
			}
			
			return historySteps;
		}
		
		public override void  Init(IDictionary props)
		{
			storeFile = (String) props["storeFile"];
			
			// check whether the file denoted by the storeFile property is a normal file.
			if (!File.Exists(new FileInfo(storeFile).FullName))
			{
				log.Fatal("storePath property should indicate a normal file");
			}
			
			// check wheter the directory containing the storeFile exist
			if (!new FileInfo(storeFile).Directory.Exists)
			{
				log.Fatal("directory " + new FileInfo(storeFile).DirectoryName + " not found");
			}
		}
		
		public override IStep MarkFinished(IStep step, int actionId, DateTime finishDate, String status, String caller)
		{
			IList currentSteps = (IList) SerializableCache.Instance.currentStepsCache[step.EntryId];
			
			foreach(SimpleStep theStep in currentSteps){
				
				if (theStep.Id == step.Id)
				{
					theStep.Status = status;
					theStep.ActionId = actionId;
					theStep.FinishDate = finishDate;
					theStep.Caller = caller;
					
					return theStep;
				}
			}
			
			SerializableCache.store();
			
			return null;
		}
		
		public override void  MoveToHistory(IStep step)
		{
			IList currentSteps = (IList) SerializableCache.Instance.currentStepsCache[step.EntryId];
			
			IList historySteps = (IList) SerializableCache.Instance.historyStepsCache[step.EntryId];
			
			if (historySteps == null)
			{
				historySteps = new ArrayList();
				SerializableCache.Instance.historyStepsCache[step.EntryId]=historySteps;
			}
			
			SimpleStep simpleStep = (SimpleStep) step;
			for(int i=0;i<currentSteps.Count;i++){
				IStep currentStep=(IStep)currentSteps[i];
				if (simpleStep.Id == currentStep.Id)
				{
					currentSteps.Remove(currentStep);
					historySteps.Add(simpleStep);
					
					break;
				}
			}
			
			SerializableCache.store();
		}
		static SerializableWorkflowStore()
		{
			log = LogManager.GetLogger(typeof(SerializableWorkflowStore));
		}
	}
	
	
	[Serializable]
	class SerializableCache 
	{
		internal static SerializableCache Instance
		{
			get
			{
				if (instance == null)
				{
					instance = load();
				}
				
				return instance;
			}
			
		}
		//~ Static fields/initializers /////////////////////////////////////////////
		
		[NonSerialized()]
		private static SerializableCache instance;
		
		//~ Instance fields ////////////////////////////////////////////////////////
		
		internal IDictionary currentStepsCache;
		internal IDictionary entryCache;
		internal IDictionary historyStepsCache;
		internal IDictionary propertySetCache;
		internal long globalEntryId = 1;
		internal long globalStepId = 1;
		
		//~ Constructors ///////////////////////////////////////////////////////////
		
		private SerializableCache()
		{
			entryCache = new Hashtable();
			currentStepsCache = new Hashtable();
			historyStepsCache = new Hashtable();
			propertySetCache = new Hashtable();
		}
		
		//~ Methods ////////////////////////////////////////////////////////////////
		
		public virtual IList query(WorkflowQuery query)
		{
			// not implemented
			return  new ArrayList();
		}
		
		
		internal static SerializableCache load()
		{
			try
			{
				FileStream fis = new FileStream(new FileInfo(SerializableWorkflowStore.storeFile).FullName, FileMode.Open, FileAccess.Read);
				BinaryReader ois = new BinaryReader(fis);
				
				SerializableCache o = (SerializableCache) SupportClass.Deserialize(ois);
				fis.Close();
				
				return o;
			}
			catch (Exception )
			{
				SerializableWorkflowStore.log.Fatal("cannot store in file " + SerializableWorkflowStore.storeFile + ". Create a new blank store.");
			}
			
			return new SerializableCache();
		}
		
		internal static void  store()
		{
			try
			{
				FileStream fos = new FileStream(new FileInfo(SerializableWorkflowStore.storeFile).FullName, FileMode.Create);
				BinaryWriter oos = new BinaryWriter(fos);
				SupportClass.Serialize(oos, Instance);
				fos.Close();
			}
			catch (Exception )
			{
				SerializableWorkflowStore.log.Fatal("cannot store in file " + SerializableWorkflowStore.storeFile + ".");
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -