public WfClientProcessStartupParams ServerToClient(WfProcessStartupParams server, ref WfClientProcessStartupParams client)
        {
            server.NullCheck("server");

            if (client == null)
                client = new WfClientProcessStartupParams();

            if (server.ProcessDescriptor != null)
                client.ProcessDescriptorKey = server.ProcessDescriptor.Key;

            WfClientDictionaryConverter.Instance.ServerToClient(server.ApplicationRuntimeParameters, client.ApplicationRuntimeParameters);

            client.AutoCommit = server.AutoCommit;
            client.AutoStartInitialActivity = server.AutoStartInitialActivity;
            client.CheckStartProcessUserPermission = server.CheckStartProcessUserPermission;
            client.DefaultTaskTitle = server.DefaultTaskTitle;
            client.DefaultUrl = server.DefaultUrl;
            client.RelativeID = server.RelativeID;
            client.RelativeURL = server.RelativeURL;
            client.ResourceID = server.ResourceID;
            client.RuntimeProcessName = server.RuntimeProcessName;

            client.Creator = (WfClientUser)server.Creator.ToClientOguObject();
            client.Department = (WfClientOrganization)server.Department.ToClientOguObject();

            WfClientAssigneeCollectionConverter.Instance.ServerToClient(server.Assignees, client.Assignees);

            return client;
        }
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            WfClientProcessStartupParams startupParams = new WfClientProcessStartupParams();

            startupParams.AutoStartInitialActivity = dictionary.GetValue("autoStartInitialActivity", true);
            startupParams.AutoCommit = dictionary.GetValue("autoCommit", false);
            startupParams.CheckStartProcessUserPermission = dictionary.GetValue("checkStartProcessUserPermission", true);
            startupParams.DefaultTaskTitle = dictionary.GetValue("defaultTaskTitle", string.Empty);
            startupParams.DefaultUrl = dictionary.GetValue("defaultUrl", string.Empty);
            startupParams.ProcessDescriptorKey = dictionary.GetValue("processDescriptorKey", string.Empty);
            startupParams.RelativeID = dictionary.GetValue("relativeID", string.Empty);
            startupParams.RelativeURL = dictionary.GetValue("relativeURL", string.Empty);
            startupParams.ResourceID = dictionary.GetValue("resourceID", string.Empty);
            startupParams.RuntimeProcessName = dictionary.GetValue("runtimeProcessName", string.Empty);
            startupParams.AutoPersist = dictionary.GetValue("autoPersist", true);

            startupParams.Creator = JSONSerializerExecute.Deserialize<WfClientUser>(dictionary.GetValue("creator", (object)null));
            startupParams.Department = JSONSerializerExecute.Deserialize<WfClientOrganization>(dictionary.GetValue("department", (object)null));

            startupParams.Opinion = JSONSerializerExecute.Deserialize<WfClientOpinion>(dictionary.GetValue("opinion", (object)null));

            JSONSerializerExecute.FillDeserializedCollection(dictionary.GetValue("assignees", (object)null), startupParams.Assignees);
            JSONSerializerExecute.FillDeserializedDictionary(dictionary, "applicationRuntimeParameters", startupParams.ApplicationRuntimeParameters);
            JSONSerializerExecute.FillDeserializedDictionary(dictionary, "processContext", startupParams.ProcessContext);

            return startupParams;
        }
        public WfProcessStartupParams ClientToServer(WfClientProcessStartupParams client, ref WfProcessStartupParams server)
        {
            client.NullCheck("client");

            if (server == null)
                server = new WfProcessStartupParams();

            if (this._TestMode == false)
                server.ProcessDescriptor = WfProcessDescriptorManager.GetDescriptor(client.ProcessDescriptorKey);

            WfClientDictionaryConverter.Instance.ClientToServer(client.ApplicationRuntimeParameters, server.ApplicationRuntimeParameters);

            server.AutoCommit = client.AutoCommit;
            server.AutoStartInitialActivity = client.AutoStartInitialActivity;
            server.CheckStartProcessUserPermission = client.CheckStartProcessUserPermission;
            server.DefaultTaskTitle = client.DefaultTaskTitle;
            server.DefaultUrl = client.DefaultUrl;
            server.RelativeID = client.RelativeID;
            server.RelativeURL = client.RelativeURL;
            server.ResourceID = client.ResourceID;
            server.RuntimeProcessName = client.RuntimeProcessName;

            server.Creator = (IUser)client.Creator.ToOguObject();
            server.Department = (IOrganization)client.Department.ToOguObject();

            WfClientAssigneeCollectionConverter.Instance.ClientToServer(client.Assignees, server.Assignees);

            return server;
        }
        public static WfClientProcessStartupParams PrepareClientProcessStartupParams(string processKey)
        {
            WfClientProcessStartupParams startupParams = new WfClientProcessStartupParams();

            startupParams.ProcessDescriptorKey = processKey;
            startupParams.AutoCommit = true;
            startupParams.AutoStartInitialActivity = true;
            startupParams.CheckStartProcessUserPermission = true;
            startupParams.DefaultTaskTitle = "Please approve";
            startupParams.DefaultUrl = "http://www.sina.com"; ;
            startupParams.Creator = Consts.Users["Requestor"];
            startupParams.Department = Consts.Departments["RequestorOrg"];
            startupParams.RelativeID = UuidHelper.NewUuidString();
            startupParams.RelativeURL = "http://www.baidu.com";
            startupParams.ResourceID = UuidHelper.NewUuidString();
            startupParams.RuntimeProcessName = "Test Process";

            startupParams.ApplicationRuntimeParameters["Amount"] = 1000;
            startupParams.ApplicationRuntimeParameters["CostCenter"] = "1001";

            startupParams.ProcessContext["Context"] = "This is a context";

            startupParams.Assignees.Add(Consts.Users["Requestor"]);

            return startupParams;
        }
        public WfClientProcessInfo StartWorkflow(WfClientProcessStartupParams clientStartupParams)
        {
            if (clientStartupParams == null)
                throw new ApplicationException(Translator.Translate(Define.DefaultCulture, "流程的启动参数不能为空"));

            //设置标准参数,优先使用外部参数
            if (!clientStartupParams.ApplicationRuntimeParameters.ContainsKey("ProcessRequestor")) 
                clientStartupParams.ApplicationRuntimeParameters["ProcessRequestor"] = clientStartupParams.Creator; 

            OperationContext.Current.FillContextToOguServiceContext();

            WfProcessStartupParams startupParams = null;

            WfClientProcessStartupParamsConverter.Instance.ClientToServer(clientStartupParams, ref startupParams);

            IWfProcess process = null;

            DoPrincipalAction(startupParams.Creator, () =>
            {
                WfStartWorkflowExecutor executor = new WfStartWorkflowExecutor(startupParams);

                executor.AfterModifyWorkflow += (dataContext =>
                {
                    dataContext.CurrentProcess.GenerateCandidatesFromResources();
                    clientStartupParams.ProcessContext.ForEach(kp => dataContext.CurrentProcess.Context[kp.Key] = kp.Value);

                    WfClientProcessInfoBaseConverter.Instance.FillOpinionInfoByProcessByActivity(
                        clientStartupParams.Opinion,
                        dataContext.CurrentProcess.CurrentActivity,
                        clientStartupParams.Creator,
                        clientStartupParams.Creator);
                });

                executor.SaveApplicationData += (dataContext) => SaveOpinion(clientStartupParams.Opinion);

                if (clientStartupParams.AutoPersist)
                    process = executor.Execute();
                else
                    process = executor.ExecuteNotPersist();
            });

            return process.ToClientProcessInfo(clientStartupParams.Creator).FillCurrentOpinion(process.CurrentActivity, clientStartupParams.Creator);
        }
        public static void AreSame(this WfClientProcessStartupParams expected, WfClientProcessStartupParams actual)
        {
            Assert.AreEqual(expected.ProcessDescriptorKey, actual.ProcessDescriptorKey);
            Assert.AreEqual(expected.AutoCommit, actual.AutoCommit);
            Assert.AreEqual(expected.AutoStartInitialActivity, actual.AutoStartInitialActivity);
            Assert.AreEqual(expected.CheckStartProcessUserPermission, actual.CheckStartProcessUserPermission);
            Assert.AreEqual(expected.DefaultTaskTitle, actual.DefaultTaskTitle);
            Assert.AreEqual(expected.RelativeID, actual.RelativeID);
            Assert.AreEqual(expected.RelativeURL, actual.RelativeURL);
            Assert.AreEqual(expected.ResourceID, actual.ResourceID);
            Assert.AreEqual(expected.RuntimeProcessName, actual.RuntimeProcessName);

            expected.Assignees.AreSame(actual.Assignees);
            expected.Creator.AreSame(actual.Creator);
            expected.Department.AreSame(actual.Department);

            expected.ApplicationRuntimeParameters.AreSame(actual.ApplicationRuntimeParameters);
            expected.ProcessContext.AreSame(actual.ProcessContext);
        }
        /// <summary>
        /// 检查
        /// </summary>
        /// <param name="processInfo"></param>
        /// <param name="clientStartupParams"></param>
        private void AssertStartedProcess(WfClientProcessInfo processInfo, WfClientProcessStartupParams clientStartupParams)
        {
            Assert.IsTrue(processInfo.ID.IsNotEmpty());
            Assert.AreEqual(clientStartupParams.ProcessDescriptorKey, processInfo.ProcessDescriptorKey);
            Assert.AreEqual(WfClientProcessStatus.Running, processInfo.Status);
            Assert.IsNotNull(processInfo.CurrentActivity);

            processInfo.Creator.AreSame(clientStartupParams.Assignees[0].User);

            Assert.IsNotNull(processInfo.CurrentActivity);
            processInfo.CurrentActivity.Assignees.AreSame(clientStartupParams.Assignees, false);

            Assert.IsNotNull(processInfo.CurrentActivity.Descriptor);

            Assert.IsTrue(processInfo.NextActivities.Count > 0);
            Assert.IsNotNull(processInfo.NextActivities[0].Transition);
            Assert.IsNotNull(processInfo.NextActivities[0].Activity);

            processInfo.ApplicationRuntimeParameters.AssertContains(clientStartupParams.ApplicationRuntimeParameters);
            processInfo.ProcessContext.AssertContains(clientStartupParams.ProcessContext);
        }