Example #1
0
        /// <summary>
        /// 保存表单数据
        /// </summary>
        /// <param name="workItem"></param>
        /// <param name="user"></param>
        /// <param name="boolMatchValue"></param>
        /// <param name="paramValues"></param>
        private void SaveBizObject(OThinker.H3.WorkItem.WorkItem workItem, OThinker.Organization.User user, OThinker.Data.BoolMatchValue boolMatchValue, List <DataItemParam> paramValues)
        {
            InstanceContext InstanceContext = this.Engine.InstanceManager.GetInstanceContext(workItem.InstanceId);
            string          bizObjectId     = InstanceContext == null ? string.Empty : InstanceContext.BizObjectId;

            OThinker.H3.DataModel.BizObjectSchema schema = this.Engine.BizObjectManager.GetPublishedSchema(InstanceContext.BizObjectSchemaCode);
            OThinker.H3.DataModel.BizObject       bo     = new OThinker.H3.DataModel.BizObject(this.Engine, schema, workItem.Participant);
            bo.ObjectID = bizObjectId;
            bo.Load();

            // 设置数据项的值
            foreach (DataItemParam param in paramValues)
            {
                OThinker.H3.DataModel.PropertySchema property = schema.GetProperty(param.ItemName);
                if (property.LogicType == DataLogicType.Comment)
                {// 审核意见
                    CommentParam comment = JSSerializer.Deserialize <CommentParam>(param.ItemValue + string.Empty);
                    this.Engine.BizObjectManager.AddComment(new Comment()
                    {
                        BizObjectId         = bo.ObjectID,
                        BizObjectSchemaCode = schema.SchemaCode,
                        InstanceId          = workItem.InstanceId,
                        TokenId             = workItem.TokenId,
                        Approval            = boolMatchValue,
                        Activity            = workItem.ActivityCode,
                        DataField           = property.Name,
                        UserID      = user.ObjectID,
                        SignatureId = comment.SignatureId,
                        Text        = comment.Text
                    });
                }
                else
                {
                    new BPM().SetItemValue(bo, property, param.ItemValue);
                }
            }

            bo.Update();
        }
        //private object LoadCommentData(string datafield)
        //{
        //    //审批意见
        //    Comment[] comments;
        //    Unit[] units;
        //    //常用意见
        //    List<string> frequentlyUsedComments;
        //    //一次从Engine获取方法需要的数据
        //    AppUtility.Engine.Interactor.LoadCommentData(
        //        this.ActionContext.SchemaCode,
        //        this.ActionContext.BizObjectID,
        //        this.ActionContext.InstanceId,
        //        this.ActionContext.WorkItem == null ? null : this.ActionContext.WorkItem.WorkItemID,
        //        datafield,
        //        datafield == SheetConsultComment ? this.ActionContext.User.UserID : null,
        //        null,
        //        this.ActionContext.User.UserID,
        //        out comments,
        //        out units,
        //        out frequentlyUsedComments);
        //    AppUtility.Engine.BizObjectManager.GetCommentsByBizObject("", "", "");

        //    List<object> historyComments = null;
        //    if (comments != null)
        //    {
        //        historyComments = new List<object>();
        //        foreach (Comment comment in comments)
        //        {
        //            OThinker.Organization.User user = null;
        //            OThinker.Organization.User delegant = null;
        //            foreach (OThinker.Organization.Unit unit in units)
        //            {
        //                if (unit.ObjectID == comment.UserID)
        //                {
        //                    user = unit as OThinker.Organization.User;
        //                }
        //                if (unit.ObjectID == comment.Delegant)
        //                {
        //                    delegant = unit as OThinker.Organization.User;
        //                }
        //            }

        //            // 获取用户图片
        //            string imagePath = UserValidator.GetUserImagePath(AppUtility.Engine, delegant == null ? user : delegant, TempImagesPath);
        //            string avatar = "";
        //            if (user.Gender == Organization.UserGender.Male)
        //            {
        //                avatar = AppUtility.PortalRoot + "/img/TempImages/usermale.jpg";
        //            }
        //            else if (user.Gender == Organization.UserGender.Female)
        //            {
        //                avatar = AppUtility.PortalRoot + "/img/TempImages/userfemale.jpg";
        //            }
        //            else
        //            {
        //                avatar = AppUtility.PortalRoot + "/img/user.jpg";
        //            }

        //            if (!string.IsNullOrEmpty(imagePath))
        //            {
        //                avatar = this.GetImageUrlPath(imagePath) + "?" + DateTime.Now.ToString("yyyyMMddHHmmss");
        //            }

        //            WorkflowTemplate.Activity Activity = this.ActionContext.Workflow.GetActivityByCode(comment.Activity);
        //            bool isMyComment = false;
        //            if (this.ActionContext.WorkItem != null && this.ActionContext.WorkItem.TokenId == comment.TokenId)
        //            {// A委托给B,那么A填的意见B能改,B填的意见A也能改
        //                isMyComment = comment.UserID == this.ActionContext.WorkItem.Participant;
        //            }
        //            string itemAction = string.Empty;
        //            string itemAction2 = string.Empty;
        //            string OriGinatorName = string.Empty;
        //            InstanceDetailController instanceDetail = new InstanceDetailController();
        //            OThinker.H3.WorkItem.WorkItem workitem = AppUtility.Engine.WorkItemManager.GetWorkItem(comment.WorkItemId);
        //            if (workitem != null)
        //            {
        //                // 类型
        //                WorkItem.WorkItemType itemType = (WorkItem.WorkItemType)OThinker.Data.Convertor.Convert<int>(workitem.ItemType);
        //                itemAction = instanceDetail.GetItemAction(itemType);
        //                WorkItem.WorkItemType consulted = (WorkItem.WorkItemType)OThinker.Data.Convertor.Convert<int>(workitem.Consulted);
        //                itemAction2 = instanceDetail.GetItemAction(consulted);
        //                if (!string.IsNullOrWhiteSpace(comment.WorkItemId) && string.Equals(itemAction, Configs.Global.ResourceManager.GetString("SheetActionPane_Assist")))
        //                {
        //                    DataTable dt = AppUtility.Engine.Query.GetWorkItemCreator(comment.WorkItemId);
        //                    if (dt != null && dt.Rows.Count > 0)
        //                    {
        //                        Unit unit = AppUtility.Engine.Organization.GetUnit(dt.Rows[0]["Creator"].ToString().Trim());
        //                        if (unit != null) OriGinatorName = unit.Name + "的" + itemAction;
        //                    }
        //                }
        //                if (string.Equals(itemAction2, Configs.Global.ResourceManager.GetString("WorkItemType_Consult")))
        //                {
        //                    Unit _unit = AppUtility.Engine.Organization.GetUnit(comment.ConsultInitiator);
        //                    if (_unit != null) OriGinatorName = _unit.Name + "的" + Configs.Global.ResourceManager.GetString("WorkItemType_Consult");
        //                }
        //            }
        //            historyComments.Add(new
        //            {
        //                CommentID = comment.CommentID,
        //                Avatar = Page.ResolveUrl(avatar),                                                        // 用户图片
        //                OUName = comment.OUName,                                                                 // 意见审批人所属OU
        //                UserName = comment.UserName,                                                             // 意见审批人
        //                DelegantName = comment.Delegant == comment.UserID ? string.Empty : comment.DelegantName, // 代理人
        //                Activity = Activity == null ? comment.Activity : Activity.DisplayName,
        //                DateStr = comment.ModifiedTime.ToShortDateString() + " " + comment.ModifiedTime.ToShortTimeString(),
        //                Text = comment.Text,
        //                IsMyComment = isMyComment,
        //                SignatureId = comment.SignatureId,
        //                Approval = comment.Approval,
        //                ParentPropertyName = comment.ParentPropertyText,
        //                itemAction = itemAction,
        //                OriGinatorName = OriGinatorName.Trim()
        //            });

        //            CheckSignatureImage(comment.SignatureId);
        //        }
        //    }

        //    return new { Comments = historyComments, FrequentlyUsedComments = frequentlyUsedComments };
        //}

        //private List<MvcListItem> LoadAttachmentData(FieldSchema field)
        //{
        //    List<MvcListItem> listItems = new List<MvcListItem>();
        //    AttachmentHeader[] headers = this.ActionContext.Engine.BizObjectManager.QueryAttachment(
        //         this.ActionContext.SchemaCode,
        //         this.ActionContext.BizObjectID,
        //         field.Name,
        //         OThinker.Data.BoolMatchValue.True,
        //         null);
        //    if (headers != null)
        //    {
        //        foreach (AttachmentHeader header in headers)
        //        {
        //            string url = string.IsNullOrEmpty(header.DownloadUrl) ? AppConfig.GetReadAttachmentUrl(
        //                this.ActionContext.IsMobile,
        //                this.ActionContext.Schema.SchemaCode,
        //                this.ActionContext.BizObjectID,
        //                header.ObjectID,
        //                AttachmentOpenMethod.Download) : header.DownloadUrl;

        //            listItems.Add(new MvcListItem(header.ObjectID, header.FileName, url, header.ContentLength, header.ContentType));
        //        }
        //    }
        //    return listItems;
        //}
        private static OThinker.H3.DataModel.BizObject[] GetBOsValue(BizObject BizObject, FieldSchema field)
        {
            OThinker.H3.DataModel.BizObject[] objs = null;
            object v = BizObject[field.Name];

            if (v == null)
            {
                objs = null;
            }
            else if (v is OThinker.H3.DataModel.BizObject)
            {
                objs = new OThinker.H3.DataModel.BizObject[] { (OThinker.H3.DataModel.BizObject)v };
            }
            else if (v is OThinker.H3.DataModel.BizObject[])
            {
                objs = (OThinker.H3.DataModel.BizObject[])v;
            }
            if (!field.IsProperty)
            {// 关联对象,存储到 Session 中,因为 ObjectID 不是固定的
                // 可能需要设计将业务字段赋值给 ObjectID
                HttpContext.Current.Session[BizObject.ObjectID + field.Name] = objs;
            }
            return(objs);
        }
        /// <summary>
        /// 启动H3流程实例(包含子表)
        /// </summary>
        /// <typeparam name="T">实体类-泛型</typeparam>
        /// <param name="workflowCode">流程模板编码</param>
        /// <param name="userCode">启动流程的用户编码</param>
        /// <param name="finishStart">是否结束第一个活动</param>
        /// <param name="EntityParamValues">流程实例启动初始化数据项实体类集合</param>
        /// <returns></returns>
        private BPMServiceResult startWorkflowByEntityTrinsJson(
            string workflowCode,
            string userCode,
            bool finishStart,
            object EntityParamValues)
        {
            string workItemID, keyItem, errorMessage;

            workItemID = keyItem = errorMessage = string.Empty;
            BPMServiceResult result;

            try
            {
                // 获取模板
                OThinker.H3.WorkflowTemplate.PublishedWorkflowTemplateHeader workflowTemplate = GetWorkflowTemplate(workflowCode);
                if (workflowTemplate == null)
                {
                    result = new BPMServiceResult(false, "流程启动失败,流程模板不存在,模板编码:" + workflowCode + "。");
                    return(result);
                }
                // 查找流程发起人
                //OThinker.Organization.User user = OThinker.H3.Controllers.AppUtility.Engine.Organization.GetUserByCode(userCode) as OThinker.Organization.User;
                string user = "******";// GetUserIDByCode(userCode);  //"18f923a7-5a5e-426d-94ae-a55ad1a4b239"; //
                if (user == null)
                {
                    result = new BPMServiceResult(false, "流程启动失败,用户{" + userCode + "}不存在。");
                    return(result);
                }

                OThinker.H3.DataModel.BizObjectSchema schema = OThinker.H3.Controllers.AppUtility.Engine.BizObjectManager.GetPublishedSchema(workflowTemplate.BizObjectSchemaCode);
                OThinker.H3.DataModel.BizObject       bo     = new OThinker.H3.DataModel.BizObject(
                    OThinker.H3.Controllers.AppUtility.Engine.Organization,
                    OThinker.H3.Controllers.AppUtility.Engine.MetadataRepository,
                    OThinker.H3.Controllers.AppUtility.Engine.BizObjectManager,
                    OThinker.H3.Controllers.AppUtility.Engine.BizBus,
                    schema,
                    OThinker.Organization.User.AdministratorID,
                    OThinker.Organization.OrganizationUnit.DefaultRootID);

                if (EntityParamValues != null)
                {
                    Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(EntityParamValues.ToString());
                    foreach (KeyValuePair <string, Newtonsoft.Json.Linq.JToken> jo in jObject)
                    {
                        //如果字段是GTAttachment则是附件
                        if (bo.Schema.ContainsField(jo.Key))
                        {
                            if (bo.Schema.GetLogicType(jo.Key) == OThinker.H3.Data.DataLogicType.Comment)
                            {
                                continue;
                            }
                            //判断是否子表
                            else if (bo.Schema.GetLogicType(jo.Key) != OThinker.H3.Data.DataLogicType.BizObjectArray)
                            {
                                //给对象赋值
                                bo[jo.Key] = jo.Value;
                            }
                            else
                            {
                                List <object> list = JSSerializer.Deserialize <List <object> >(Newtonsoft.Json.JsonConvert.SerializeObject(jo.Value) as string);
                                //获取子表的属性
                                BizObjectSchema childSchema = schema.GetProperty(jo.Key).ChildSchema;
                                BizObject[]     bizObjects  = new BizObject[list.Count];
                                int             i           = 0;
                                foreach (object objT in list)
                                {
                                    bizObjects[i] = new BizObject(OThinker.H3.Controllers.AppUtility.Engine, childSchema, userCode);
                                    foreach (KeyValuePair <string, object> t in (dynamic)objT)
                                    {
                                        if (childSchema.ContainsField(t.Key))
                                        {
                                            //给对象赋值
                                            bizObjects[i][t.Key] = t.Value;
                                            continue;
                                        }
                                    }
                                    i++;
                                }
                                //子表回写给流程子表
                                bo[jo.Key] = bizObjects;
                            }
                        }
                    }
                }
                bo.Create();

                // 创建流程实例
                string InstanceId = OThinker.H3.Controllers.AppUtility.Engine.InstanceManager.CreateInstance(
                    bo.ObjectID,
                    workflowTemplate.WorkflowCode,
                    workflowTemplate.WorkflowVersion,
                    null,
                    null,
                    user,
                    null,
                    false,
                    OThinker.H3.Instance.InstanceContext.UnspecifiedID,
                    null,
                    OThinker.H3.Instance.Token.UnspecifiedID);

                // 设置紧急程度为普通
                OThinker.H3.Messages.MessageEmergencyType emergency = OThinker.H3.Messages.MessageEmergencyType.Normal;
                // 这里也可以在启动流程的时候赋值
                Dictionary <string, object> paramTables = new Dictionary <string, object>();

                // 启动流程的消息
                OThinker.H3.Messages.StartInstanceMessage startInstanceMessage
                    = new OThinker.H3.Messages.StartInstanceMessage(
                          emergency,
                          InstanceId,
                          workItemID,
                          paramTables,
                          OThinker.H3.Instance.PriorityType.Normal,
                          true,
                          null,
                          false,
                          OThinker.H3.Instance.Token.UnspecifiedID,
                          null);
                OThinker.H3.Controllers.AppUtility.Engine.InstanceManager.SendMessage(startInstanceMessage);
                result = new BPMServiceResult(true, InstanceId, workItemID, "流程实例启动成功!", "");
            }
            catch (Exception ex)
            {
                result = new BPMServiceResult(false, "流程实例启动失败!错误:" + ex + string.Empty);
            }
            return(result);
        }
        /// <summary>
        /// 启动H3流程实例
        /// </summary>
        /// <param name="workflowCode">流程模板编码</param>
        /// <param name="userCode">启动流程的用户编码</param>
        /// <param name="finishStart">是否结束第一个活动</param>
        /// <param name="paramValues">流程实例启动初始化数据项集合</param>
        /// <returns></returns>
        public BPMServiceResult StartWorkflow(string workflowCode, string userCode, bool finishStart, List <DataItemParam> paramValues)
        {
            //ValidateSoapHeader();
            BPMServiceResult result = new BPMServiceResult();

            try
            {
                // 获取模板
                OThinker.H3.WorkflowTemplate.PublishedWorkflowTemplateHeader workflowTemplate = GetWorkflowTemplate(workflowCode);
                if (workflowTemplate == null)
                {
                    return(new BPMServiceResult(false, "流程启动失败,流程模板不存在,模板编码:" + workflowCode + "。"));
                }
                // 查找流程发起人
                //OThinker.Organization.User user = Engine.Organization.GetUnitByCode(userCode) as Organization.User;
                string user = "******";// GetUserIDByCode(userCode);
                if (user == null)
                {
                    return(new BPMServiceResult(false, "流程启动失败,用户{" + userCode + "}不存在。"));
                }
                OThinker.H3.DataModel.BizObjectSchema schema = Engine.BizObjectManager.GetPublishedSchema(workflowTemplate.BizObjectSchemaCode);
                OThinker.H3.DataModel.BizObject       bo     = new OThinker.H3.DataModel.BizObject(Engine.Organization, Engine.MetadataRepository, Engine.BizObjectManager, null, schema, user);
                if (paramValues != null)
                {
                    // 这里可以在创建流程的时候赋值
                    foreach (DataItemParam param in paramValues)
                    {
                        if (bo.Schema.GetProperty(param.ItemName).LogicType == OThinker.H3.Data.DataLogicType.BizObjectArray)
                        {
                            var t = new List <OThinker.H3.DataModel.BizObject>();
                            foreach (List <DataItemParam> list in (IEnumerable)param.ItemValue)
                            {
                                var m = new OThinker.H3.DataModel.BizObject(Engine.Organization, Engine.MetadataRepository, Engine.BizObjectManager, null, schema.Fields.FirstOrDefault(x => x.ChildSchemaCode == param.ItemName).Schema, user);
                                foreach (DataItemParam item in list)
                                {
                                    if (m.Schema.ContainsField(item.ItemName))
                                    {
                                        m[item.ItemName] = item.ItemValue;
                                    }
                                }
                                t.Add(m);
                            }
                            bo[param.ItemName] = t.ToArray();
                        }
                        else if (bo.Schema.ContainsField(param.ItemName))
                        {
                            bo[param.ItemName] = param.ItemValue;
                        }
                    }
                }
                bo.Create();
                // 创建流程实例
                //string InstanceId = this.Engine.InstanceManager.CreateInstance(
                //    bo.ObjectID,
                //    workflowTemplate.WorkflowCode,
                //    workflowTemplate.WorkflowVersion,
                //    null,
                //    null,
                //    user,
                //    null, // 以组的身份发起
                //    null, // 以岗位的身份发起
                //    false, //
                //    OThinker.H3.Instance.InstanceContext.UnspecifiedID,
                //    null,
                //    OThinker.H3.Instance.Token.UnspecifiedID);
                string InstanceId = this.Engine.InstanceManager.CreateInstanceByDefault(
                    bo.ObjectID, workflowTemplate.WorkflowCode, null, user);
                // 设置紧急程度为普通
                OThinker.H3.Messages.MessageEmergencyType emergency = OThinker.H3.Messages.MessageEmergencyType.Normal;
                // 这里也可以在启动流程的时候赋值
                Dictionary <string, object> paramTables = new Dictionary <string, object>();
                // 启动流程的消息
                OThinker.H3.Messages.StartInstanceMessage startInstanceMessage =
                    new OThinker.H3.Messages.StartInstanceMessage(emergency, InstanceId, null, paramTables,
                                                                  OThinker.H3.Instance.PriorityType.Normal, true, null, false, OThinker.H3.Instance.Token.UnspecifiedID, null);
                Engine.InstanceManager.SendMessage(startInstanceMessage);
                result = new BPMServiceResult(true, InstanceId, null, "流程实例启动成功!", "");
            }
            catch (Exception ex)
            {
                result = new BPMServiceResult(false, "流程实例启动失败!错误:" + ex.Message);
            }
            return(result);
        }