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();
}
}
}