Click here to Skip to main content
15,896,269 members
Articles / Desktop Programming / Windows Forms

Document approval workflow system

Rate me:
Please Sign up or sign in to vote.
4.09/5 (26 votes)
24 Mar 2007CPOL6 min read 259.6K   9.3K   169  
Generic, full cycle of document approval workflow system, using Windows Workflow Foundation (WF).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DocumentBaseLibrary;
using DocumentCore.Bll;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Runtime.Hosting;
using DocumentWorkflow;
using System.Threading;

namespace DocumentUserApplication
{
    public partial class frmMain : Form
    {
        DocumentService _service = new DocumentService();
        WorkflowRuntime _workflowRuntime;
        public frmMain()
        {
            InitializeComponent();
            this._workflowRuntime = InitWorkflowRuntime();
        }

        private WorkflowRuntime InitWorkflowRuntime()
        {
            // Get a new workflow runtime
            WorkflowRuntime wr = new WorkflowRuntime();
            wr.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
            wr.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(wr_WorkflowTerminated);
            wr.WorkflowIdled += new EventHandler<WorkflowEventArgs>(wr_WorkflowIdled);

            // Add the external data service
            ExternalDataExchangeService dataService = new ExternalDataExchangeService();
            wr.AddService(dataService);

            // Add custom HelpDesk service
            
            dataService.AddService(this._service);
            this._service.SentToWorkflow += new EventHandler<DocumentEventArguments>(_service_SentToWorkflow);

            // Add system SQL state service
            SqlWorkflowPersistenceService stateService = new SqlWorkflowPersistenceService(
                PersistanceServiceSettings.ConnectionString);
            wr.AddService(stateService);

            // Start
            wr.StartRuntime();
            return wr;
        }

        void _service_SentToWorkflow(object sender, DocumentEventArguments e)
        {
            MessageBox.Show(string.Format("Document {0}, was successfuly sent to the workflow", e.Document), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            
        }

        void wr_WorkflowIdled(object sender, WorkflowEventArgs e)
        {
            MessageBox.Show(string.Format("Workflow in Idle state, with workflow instance={0}",e.WorkflowInstance.InstanceId), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            ThreadPool.QueueUserWorkItem(UnloadInstance, e.WorkflowInstance);
            
        }

        void wr_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
        {
            MessageBox.Show(string.Format("Workflow is terminated, with workflow instance={0}", e.Exception.Message), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            
        }

        void wr_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            MessageBox.Show(string.Format("Workflow is completed, with workflow instance={0}", e.WorkflowInstance.InstanceId), "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            PopulateCategory();
        }

        private void PopulateCategory()
        {
        this.cmbCategory.DataSource=Enum.GetValues(typeof(Category));
        }

        private void btnCreateDocument_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                Document _document = new Document();
                _document.Category = (Category)this.cmbCategory.SelectedItem;
                _document.Title = this.txtTitle.Text.Trim();
                _document.Description = this.txtDescription.Text.Trim();
                _document.Depositor = User.CurrentUser.UserName;
                _service.CreateDocument(_document);
                ConfirmSuccess();
            }
            else {
                MessageBox.Show("Please complete, the form's fields", "Validation, Error", MessageBoxButtons.OK
                           , MessageBoxIcon.Error);
            }
        }

        private bool ValidateForm()
        {
            if (string.IsNullOrEmpty(this.txtTitle.Text.Trim())) return false;
            if (string.IsNullOrEmpty(this.txtDescription.Text.Trim())) return false;
            return true;
        }

        private void ConfirmSuccess()
        {
            MessageBox.Show("Document, was successfuly created", "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            ClearForm();
        }

        private void ClearForm()
        {
            this.txtDescription.Text = string.Empty;
            this.txtTitle.Text = string.Empty;
            this.cmbCategory.SelectedIndex = 0;

        }

        private void btnViewAllDocuments_Click(object sender, EventArgs e)
        {
           this.dgrdDocuments.DataSource=this._service.GetCreatedDocuments(User.CurrentUser.UserName);
        
        }

        private void btnViewOpenDocuments_Click(object sender, EventArgs e)
        {
            this.dgrdDocuments.DataSource = this._service.GetCreatedDocuments(User.CurrentUser.UserName, DocumentBaseLibrary.Status.Open);

        }

        private void btnInWorkflow_Click(object sender, EventArgs e)
        {
            this.dgrdDocuments.DataSource = this._service.GetCreatedDocuments(User.CurrentUser.UserName, DocumentBaseLibrary.Status.InWorkflow);

        }

        private void btnApproved_Click(object sender, EventArgs e)
        {
            this.dgrdDocuments.DataSource = this._service.GetCreatedDocuments(User.CurrentUser.UserName, DocumentBaseLibrary.Status.Approved);

        }
        int _documentID = default(int);
        private void submitToWorkflowToolStripMenuItem_Click(object sender, EventArgs e)
        {
              _documentID=  int.Parse(dgrdDocuments.CurrentRow.Cells["ID"].Value.ToString());
           DialogResult result=   MessageBox.Show("Are you sure that you want submit document to workflow", "confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
           Document doc = null;
            if (result == DialogResult.Yes)
           {
               doc = _service.GetDocumentByID(this._documentID);
               if (doc.Status != DocumentBaseLibrary.Status.Open)
               {
                   MessageBox.Show(string.Format("This document {0} is not in Open state", doc), "Error", MessageBoxButtons.OK
                       , MessageBoxIcon.Error);
                   return;
               }

               SubmitToWorkflow(doc);
           }
                        
        }

        private void SubmitToWorkflow(Document document)
        {
            // Fill the Parameters collection for this instance of the workflow
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("Depositor", User.CurrentUser);

            parameters.Add("Document", document);

            // Get the type of the workflow
            Type type = typeof(DocumentWorkflow.DocumentWorkflow);

            // Start the workflow instance
            WorkflowInstance inst = _workflowRuntime.CreateWorkflow(type, parameters);

            inst.Start();
              
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _documentID = int.Parse(dgrdDocuments.CurrentRow.Cells["ID"].Value.ToString());
            DialogResult result = MessageBox.Show("Are you sure that you want to delete  document to workflow", "confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            Document doc = null;
            if (result == DialogResult.Yes)
            {
                doc = _service.GetDocumentByID(this._documentID);
                if (doc.Status != DocumentBaseLibrary.Status.Open)
                {
                    MessageBox.Show(string.Format("This document {0} is not in Open state", doc), "Error", MessageBoxButtons.OK
                        , MessageBoxIcon.Error);
                    return;
                }

                this._service.DeleteDocument(doc);
            }
        }
        // Perform the unload (persist and dispose)
        static void UnloadInstance(object workflowInstance)
        {
            ((WorkflowInstance)workflowInstance).TryUnload();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NSW Curriculum & Learning Innovation Centre
Australia Australia
I am a senior developer self taught,
the main study is electronics and communication engineering

I am working as senior programmer in center for learning and innovation
www.cli.nsw.edu.au

I develop Software since 1995 using Delphi with Microsoft SQL Server

before 2000, I didn’t like Microsoft tools and did many projects using all database tools and other programming tools specially Borland C++ Builder, Delphi, Power Builder
And I loved Oracle database for its stability until I was certified as Master in Database Administration from Oracle University.

I tried to work in web programming but I felt that Java is hard and slow in programming, specially I love productivity.

I began worked with .Net since 2001 , and at the same time Microsoft SQL Server 7 was very stable so I switched all my way to Microsoft Tech.
I really respect .Net Platform especially in web applications

I love database Applications too much
And built library with PowerBuilder it was very useful for me and other developers

I have a wide experience due to my work in different companies
But the best experience I like in wireless applications, and web applications.
The best Application I did in my life is Novartis Marketing System 1999 it takes 8 months developing with PowerBuilder and Borland C++, SQL Server
Performance was the key challenge in this Application.
The other 2 applications that I loved Multilingual Project in Scada company in Italy 2000 and SDP Mobile media content platform server for ChinaUnicom 2004
I hope that you enjoy any Article I post.
God bless you.

Comments and Discussions