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

📄 hibernateworkflowstore.cs

📁 基于DotNet的工作流引擎实现
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using DotNetTools.PropertySet;
using DotNetTools.Util;
using DotNetTools.Workflow.Query;
using log4net;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Expression;
using Iesi.Collections;
using Expression = NHibernate.Expression.Expression;
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
namespace DotNetTools.Workflow.Spi.Hibernate
{
	/// <summary> A workflow store backed by Hibernate for persistence.  To use this with the standard
	/// persistence factory, pass to the DefaultConfiguration.persistenceArgs the SessionFactory to
	/// use:
	/// <code>DefaultConfiguration.persistenceArgs.put("sessionFactory", DatabaseHelper.getSessionFactory());</code>
	/// See the HibernateFunctionalWorkflowTestCase for more help.
	/// *
	/// </summary>
	/// <author>jjx (.net)</author>
	public class HibernateWorkflowStore : IWorkflowStore
	{
		//~ Static fields/initializers /////////////////////////////////////////////
		
		private static readonly ILog log;
		
		//~ Instance fields ////////////////////////////////////////////////////////
		
		internal ISession session;
		internal ISessionFactory sessionFactory;
		
		//~ Constructors ///////////////////////////////////////////////////////////
		
		public HibernateWorkflowStore()
		{
		}
		public HibernateWorkflowStore(IDictionary props)
		{
			Init(props);
		}
		
		public HibernateWorkflowStore(ISessionFactory sessionFactory)
		{
			this.sessionFactory = sessionFactory;
			
			try
			{
				this.session = sessionFactory.OpenSession();
			}
			catch (HibernateException he)
			{
				log.Error("constructor", he);
				throw new StoreException("constructor", he);
			}
		}
		
		//~ Methods ////////////////////////////////////////////////////////////////
		
		public virtual void  SetEntryState(long entryId, int state)
		{
			try
			{
				HibernateWorkflowEntry entry = (HibernateWorkflowEntry) session.Find("FROM  " + typeof(HibernateWorkflowEntry).FullName + " as entry WHERE entry.Id = ?", entryId, NHibernateUtil.Int64)[0];
				entry.State = state;
				session.Save(entry);
			}
			catch (HibernateException e)
			{
				log.Error("An exception occured", e);
				
				return ;
			}
		}
		//todo: 使用ado代替
		public virtual IPropertySet GetPropertySet(long entryId)
		{

			IDictionary args = new Hashtable(1);
			args.Add("globalKey", "osff_" + entryId);

			return PropertySetManager.GetInstance("ado", args);
//			IDictionary args = new Hashtable();
//			args.Add("entityName", "OSWorkflowEntry");
//			args.Add("entityId", entryId);
//			
//			DefaultHibernateConfigurationProvider configurationProvider = new DefaultHibernateConfigurationProvider();
//			configurationProvider.setSessionFactory(sessionFactory);
//			
//			args.put("configurationProvider", configurationProvider);
			
//			return PropertySetManager.GetInstance("hibernate", args);
		}
		
		public virtual IStep CreateCurrentStep(long entryId, int stepId, String owner, DateTime startDate, DateTime dueDate, String status, long[] previousIds)
		{
			HibernateCurrentStep step = new HibernateCurrentStep();
			HibernateWorkflowEntry entry;
			
			ITransaction tx;
			
			try
			{
				tx = session.BeginTransaction();
				entry = (HibernateWorkflowEntry) session.Find("FROM  " + typeof(HibernateWorkflowEntry).FullName + " as entry WHERE entry.Id = ?", entryId, NHibernateUtil.Int64)[0];
			}
			catch (HibernateException he)
			{
				log.Error("Looking for workflow entry " + entryId, he);
				throw new StoreException("Looking for workflow entry " + entryId, he);
			}
			
			step.Entry = entry;
			step.StepId = stepId;
			step.Owner = owner;
			step.StartDate = startDate;
			
			step.DueDate = dueDate;
			step.Status = status;
			step.FinishDate=DateTime.MaxValue;
			
			IList stepIdList = new ArrayList(previousIds.Length);
			
			for (int i = 0; i < previousIds.Length; i++)
			{
				long previousId = previousIds[i];
				stepIdList.Add(previousId);
			}
			
			if (stepIdList.Count!=0)
			{
				String stepIds = TextUtils.Join(", ", stepIdList);
				
				try
				{
					step.PreviousSteps=session.Find("FROM  " + typeof(HibernateCurrentStep).FullName + "  as step WHERE step.Id IN (" + stepIds + ")");
				}
				catch (HibernateException he)
				{
					log.Error("Looking for step in " + stepIds, he);
					throw new StoreException("Looking for step in " + stepIds, he);
				}
			}
			else
			{
				step.PreviousSteps=Collections.EMPTY_LIST;
			}
			
			if (entry.CurrentSteps == null)
			{
				ArrayList cSteps = new ArrayList(1);
				cSteps.Add(step);
				entry.CurrentSteps=(cSteps);
			}
			else
			{
				entry.CurrentSteps.Add(step);
			}
			
			try
			{
				session.Save(entry);
				tx.Commit();
				
				//session.save(step);
				return step;
			}
			catch (HibernateException he)
			{
				log.Error("Saving new workflow entry", he);
				throw new StoreException("Saving new workflow entry", he);
			}
		}
		
		public virtual IWorkflowEntry CreateEntry(String workflowName)
		{
			HibernateWorkflowEntry entry = new HibernateWorkflowEntry();
			entry.State = SimpleWorkflowEntry.CREATED;
			entry.WorkflowName = workflowName;
			
			ITransaction tx;
			
			try
			{
				tx = session.BeginTransaction();
				session.Save(entry);
				tx.Commit();
			}
			catch (HibernateException he)
			{
				log.Error("Saving new workflow entry", he);
				throw new StoreException("Saving new workflow entry", he);
			}
			
			return entry;
		}
		
		public virtual IList FindCurrentSteps(long entryId)
		{
			HibernateWorkflowEntry entry;
			
			try
			{
				entry = (HibernateWorkflowEntry) session.Find("FROM  " + typeof(HibernateWorkflowEntry).FullName + " as entry WHERE entry.Id = ?", entryId, NHibernateUtil.Int64)[0];
			}
			catch (HibernateException he)
			{
				log.Error("Looking for entryId " + entryId, he);
				throw new StoreException("Looking for entryId " + entryId, he);
			}
			
			try
			{
				return session.Find("FROM  " + typeof(HibernateCurrentStep).FullName + " as step WHERE step.Entry = ?", entry, NHibernateUtil.Entity(entry.GetType()));
			}
			catch (HibernateException he)
			{
				log.Error("Looking for step id" + entry, he);
				throw new StoreException("Looking for step id" + entry, he);
			}
		}
		
		public virtual IWorkflowEntry FindEntry(long entryId)
		{
			try
			{
				IList result = session.Find("FROM  " + typeof(HibernateWorkflowEntry).FullName + "  as entry WHERE entry.Id = ?", entryId, NHibernateUtil.Int64);
				
				return (IWorkflowEntry) result[0];
			}
			catch (HibernateException he)
			{
				log.Error("Looking for entry " + entryId, he);
				throw new StoreException("Loooking for entry " + entryId, he);
			}
		}
		
		public virtual IList FindHistorySteps(long entryId)
		{
			HibernateWorkflowEntry entry;
			
			try
			{
				entry = (HibernateWorkflowEntry) session.Find("FROM  " + typeof(HibernateWorkflowEntry).FullName + " as entry WHERE entry.Id = ?", entryId, NHibernateUtil.Int64)[0];
			}
			catch (HibernateException he)
			{
				log.Error("Finding entry " + entryId, he);
				throw new StoreException("Finding entry " + entryId, he);
			}
			
			try
			{
				return session.Find("FROM  " + typeof(HibernateHistoryStep).FullName + " as step WHERE step.Entry = ? order by step.StepId desc", entry, NHibernateUtil.Entity(entry.GetType()));
			}
			catch (HibernateException he)
			{
				log.Error("Looking for step with entry " + entry, he);
				throw new StoreException("Looking for step with entry " + entry, he);
			}
		}
		protected ISessionFactory buildSessionFactory(IDictionary hibernateProperties)
		{
			Configuration configuration=new Configuration();
			
			configuration.AddProperties(hibernateProperties);

			//configuration.AddAssembly("DotNetTools.Workflow");
			configuration.AddAssembly(GetType().Assembly);

			
			return configuration.BuildSessionFactory();
		}
	
		public virtual void  Init(IDictionary props)
		{
			try
			{
				//if(1==2){
				//sessionFactory = (ISessionFactory) props["sessionFactory"];
				sessionFactory=buildSessionFactory(props);
				session = sessionFactory.OpenSession();
				
				//}
			}
			catch (HibernateException he)
			{
				log.Error("Setting sessionFactory", he);
				throw new StoreException("Setting sessionFactory", he);
			}
		}
		
		public virtual IStep MarkFinished(IStep step, int actionId, DateTime finishDate, String status, String caller)
		{
			HibernateCurrentStep currentStep = (HibernateCurrentStep) step;
			
			currentStep.ActionId = actionId;
			currentStep.FinishDate = finishDate;
			currentStep.Status = status;
			currentStep.Caller = caller;
			
			try
			{
				ITransaction tx = session.BeginTransaction();
				session.Save(currentStep);
				tx.Commit();
				
				return currentStep;
			}
			catch (HibernateException he)
			{
				log.Error("Saving current step with action " + actionId, he);
				throw new StoreException("Saving current step with action " + actionId, he);
			}
		}
		
		public virtual void  MoveToHistory(IStep step)
		{
			HibernateWorkflowEntry entry;
			
			ITransaction tx;
			
			try
			{
				tx = session.BeginTransaction();
				entry = (HibernateWorkflowEntry) session.Find("FROM " + typeof(HibernateWorkflowEntry).FullName + " as entry WHERE entry.Id = ?", step.EntryId, NHibernateUtil.Int64)[0];
			}
			catch (HibernateException he)
			{
				log.Error("Looking for workflow entry " + step.EntryId, he);
				throw new StoreException("Looking for workflow entry " + step.EntryId, he);
			}
			
			HibernateHistoryStep hStep = new HibernateHistoryStep((HibernateStep) step);
			
			entry.CurrentSteps.Remove(step);
			
			if (entry.HistorySteps == null)
			{
				ArrayList hSteps = new ArrayList(1);
				hSteps.Add(hStep);
				entry.HistorySteps=(hSteps);
			}
			else
			{
				entry.HistorySteps.Add(hStep);
			}
			
			try
			{
				session.Save(hStep);
				session.Save(entry);
				tx.Commit();
				
				//session.delete(step);
				//session.save(hStep, new Long(hStep.getId()));
			}
			catch (HibernateException he)
			{
				log.Error("Saving workflow entry " + entry.Id, he);
				throw new StoreException("Saving workflow entry " + entry.Id, he);
			}
		}
		
		public virtual IList Query(WorkflowExpressionQuery query)
		{
			Query.Expression expression = query.Expression;
			
			ICriterion expr;
			
			Type entityClass = getQueryClass(expression, null);
			

⌨️ 快捷键说明

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