在企業中,大部分重要的業務需要進行審批,由于具有決定權的上司者事務繁忙或者受環境或低于限制,沒法通過網際網路進行工作處理。
移動審批,通過手機友善快捷地登陸企業内部網及網際網路,即使處于無線的狀态下,也能進行遠端辦公,以此來提高對于突發事件的反應能力,提升企業競争力。
這篇文章我将使用iPhone作為移動終端進行設計與實作,iPhone引入了基于大型多觸點顯示屏和領先性新軟體的全新使用者界面,讓使用者用手指即可控制iPhone。iPhone還開創了移動裝置軟體尖端功能的新紀元,重新定義了行動電話的功能。iPhone開發中,除了比較複雜的遊戲開發,還有一種就是跟資料庫打交道的應用程式,這可以在本文中得到展現。
WF4.0是微軟提供的一套工作流開發架構,裡面包含:流程設計器,各種類型封裝好的工作流活動,持久化服務,WCF服務等功能。這篇文章我将結合iPhone+WF4.0實作移動審批工作流。通過一個簡單的例子你可以清楚的看到與資料庫相關的iPhone應用程式的開發,以及使用iPhone+WCF+WF實作移動審批的各種關鍵技術的使用。
設計架構圖: 由于是使用了WCF的restful服務,是以其它手機平台也會很友善使用此工作流服務。
iPhone用戶端的表單可以通過從服務端設計的表單結構的XML動态産生。
下面通過一個簡單的請假流程說明如何實作:
用例流程圖:
上面的流程大緻為:申請人在iPhone上打開請假流程,填寫姓名和請假天數,點選送出,這個流程将走到他的上級,上級進行審批,并将審批結果通過短信知道申請者。
參考上圖,通過WF4.0設計器設計一個流程,如下圖:
下面我使用WCF将此流程封裝到restful服務中。
wcf restful服務設計如下:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getleavelist")]
List<Leave> GetLeaveList();
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "createleave/{name}/{day}")]
string CreateLeave(string name, string day);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "auditingleave/{id}/{comment}")]
string AuditingLeave(string id, string comment);
}
資料庫設計:
1、WF持久化資料表:使用WF自帶的資料表。
2、業務資料表:請假單,設計如下:
WF流程啟動和運作代碼:
public class WorkFlowProcess
{
static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
static Hashtable InstanceHashtable = new Hashtable();
public static string RunInstance(Guid ID,string Comment)
{
SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore("Data Source=PC-ZHUQL//ZHUQL;Initial Catalog=TestDB;Integrated Security=True;Pooling=False");
WorkflowApplication application1 = new WorkflowApplication(new LeaveProcess());
application1.InstanceStore = instanceStore;
application1.Completed = (workflowApplicationCompletedEventArgs) =>
{
Console.WriteLine("/nWorkflowApplication has Completed in the {0} state.", workflowApplicationCompletedEventArgs.CompletionState);
};
application1.Unloaded = (workflowApplicationEventArgs) =>
{
Console.WriteLine("WorkflowApplication has Unloaded/n");
instanceUnloaded.Set();
};
application1.Load(ID);
application1.ResumeBookmark("Check", Comment);
instanceUnloaded.WaitOne();
return "success";
}
// creates a workflow application, binds parameters, links extensions and run it
public static string CreateAndRun(string LeaveName, int LeaveDay)
{
SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore("Data Source=PC-ZHUQL//ZHUQL;Initial Catalog=TestDB;Integrated Security=True;Pooling=False");
InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
instanceStore.DefaultInstanceOwner = view.InstanceOwner;
IDictionary<string, object> input = new Dictionary<string, object>
{
{ "LeaveName" , LeaveName },
{ "LeaveDay" , LeaveDay }
};
WorkflowApplication application = new WorkflowApplication(new LeaveProcess(), input);
application.InstanceStore = instanceStore;
application.PersistableIdle = (e) =>
{
instanceUnloaded.Set();
return PersistableIdleAction.Unload;
};
application.Unloaded = (e) =>
{
instanceUnloaded.Set();
};
application.OnUnhandledException = (ex) =>
{
Console.Write("Exception");
return UnhandledExceptionAction.Terminate;
};
Guid id = application.Id;
application.Run();
instanceUnloaded.WaitOne();
return "success";
}
// executed when instance is persisted
public static void OnWorkflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
}
// executed when instance goes idle
public static void OnIdle(WorkflowApplicationIdleEventArgs e)
{
}
public static PersistableIdleAction OnIdleAndPersistable(WorkflowApplicationIdleEventArgs e)
{
return PersistableIdleAction.Persist;
}
}
iPhone用戶端調用:
在前一篇文章:iPhone中調用WCF RESTFUL Service ,講解了iPhone中如何調用WCF restful服務,下面用啟動流程為例說明:
-(IBAction) btnSender:(id)sender
{
NSString * urlString =@"http://10.5.23.117:8008/RestServiceImpl.svc/createleave/%@/%@";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:urlString,txtName.text,txtDay.text]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"杩斿洖缁撴灉"
message:@"鎻愪氦鎴愬姛"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
iPhone界面設計:
1、啟動流程界面:
2、填寫表單資訊:
3、啟動流程
4、待稽核清單:
5、稽核界面:
6、稽核結果:
這裡是個簡單的demo,界面非常的簡陋。
總結: 本文通過一個簡單的用例來說明了移動審批流程的各個層面的各種技術的實作。使用這些技術實作一個工作流服務以及工作流用戶端界面将非常簡單。
代碼:服務端 ,iPhone用戶端