示例#1
0
        public void AlterView(Domain.Entity entity)
        {
            //不从缓存获取,这里要读取未提交的记录
            var attributes    = _attributeFinder.Query(x => x.Where(f => f.EntityId == entity.EntityId));
            var relationShips = _relationShipFinder.Query(x => x.Where(f => f.ReferencingEntityId == entity.EntityId));

            _metadataProvider.AlterView(entity, attributes, relationShips);
        }
示例#2
0
        public IActionResult BusinessProcess([FromBody] BusinessProcessArgsModel args)
        {
            if (args.EntityId.Equals(Guid.Empty) || args.RecordId.Equals(Guid.Empty))
            {
                return(JError(T["parameter_error"]));
            }
            var entityMeta = _entityFinder.FindById(args.EntityId);

            if (entityMeta == null)
            {
                return(NotFound());
            }
            if (!entityMeta.BusinessFlowEnabled)
            {
                return(JError(T["businessflow_disabled"]));
            }
            var data = this._dataFinder.RetrieveById(entityMeta.Name, args.RecordId);

            if (data.IsEmpty())
            {
                return(NotFound());
            }
            WorkFlow flowInfo = null;
            BusinessProcessFlowInstance flowInstance = null;
            List <ProcessStage>         stages       = null;
            Guid entityStageId = data.GetGuidValue("stageid");
            int  entityIndex   = 0;

            if (args.BusinessflowId.HasValue && !args.BusinessflowId.Equals(Guid.Empty))
            {
                flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == args.BusinessflowId.Value);
            }
            else if (args.BusinessflowInstanceId.HasValue && !args.BusinessflowInstanceId.Value.Equals(Guid.Empty))
            {
                flowInstance = _businessProcessFlowInstanceService.FindById(args.BusinessflowInstanceId.Value);
                if (flowInstance != null)
                {
                    flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == flowInstance.WorkFlowId);
                }
            }
            if (flowInfo == null)
            {
                var flowList = _workFlowFinder.QueryAuthorized(args.EntityId, FlowType.Business);
                flowInfo = flowList.NotEmpty() ? flowList.First() : null;
                if (flowInfo == null && !entityStageId.Equals(Guid.Empty))
                {
                    //查找当前实体所在阶段
                    var processstage = _processStageService.Find(n => n.ProcessStageId == entityStageId);
                    if (processstage == null)
                    {
                        return(NotFound());
                    }
                    flowInfo = _workFlowFinder.Find(n => n.WorkFlowId == processstage.WorkFlowId && n.StateCode == RecordState.Enabled);
                }
            }
            if (flowInfo == null)
            {
                return(Content(""));
            }
            stages = _processStageService.Query(n => n
                                                .Where(f => f.WorkFlowId == flowInfo.WorkFlowId)
                                                .Sort(s => s.SortAscending(f => f.StageOrder))
                                                );
            var entityIds = stages.Select(n => n.EntityId).Distinct().ToList();

            entityIndex = entityIds.FindIndex(n => n.Equals(args.EntityId)) + 1;
            //查询业务流程实例
            if (flowInstance == null)
            {
                if (entityIndex == 1)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity1Id == args.RecordId);
                }

                if (entityIndex == 2)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity2Id == args.RecordId);
                }

                if (entityIndex == 3)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity3Id == args.RecordId);
                }

                if (entityIndex == 4)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity4Id == args.RecordId);
                }

                if (entityIndex == 5)
                {
                    flowInstance = _businessProcessFlowInstanceService.Find(n => n.WorkFlowId == flowInfo.WorkFlowId && n.Entity5Id == args.RecordId);
                }
            }

            if (flowInstance != null)
            {
                entityStageId = flowInstance.ProcessStageId.Value;
            }
            BusinessProcessModel model = new BusinessProcessModel
            {
                EntityId             = args.EntityId,
                RecordId             = args.RecordId,
                Data                 = data,
                BusinessFlow         = flowInfo,
                BusinessFlowInstance = flowInstance,
                Stages               = stages
            };

            model.CurrentStageId = entityStageId.Equals(Guid.Empty) ? model.Stages.First().ProcessStageId : entityStageId;
            Dictionary <string, object>    steps      = new Dictionary <string, object>();
            List <Schema.Domain.Attribute> attributes = new List <Schema.Domain.Attribute>();

            foreach (var stage in model.Stages)
            {
                var st = new List <ProcessStep>();
                st = st.DeserializeFromJson(stage.Steps);
                steps.Add(stage.ProcessStageId.ToString(), st);
                var attrs = st.Select(f => f.AttributeName).ToList();
                attributes.AddRange(_attributeFinder.Query(n => n.Where(f => f.Name.In(attrs) && f.EntityId == stage.EntityId)));
            }
            model.Steps      = steps;
            model.Attributes = attributes;
            var related = model.Stages.Where(n => n.RelationshipName.IsNotEmpty()).ToList();

            if (related.NotEmpty())
            {
                var rnames = related.Select(f => f.RelationshipName).ToList();
                model.RelationShips = _relationShipFinder.Query(n => n.Where(f => f.Name.In(rnames)));
                _relationShipFinder.WrapLocalizedLabel(model.RelationShips);
            }
            if (model.BusinessFlowInstance != null)
            {
                var rsRecords = new Dictionary <string, object>();
                int i         = 1;
                foreach (var eid in entityIds)
                {
                    var eidMeta = _entityFinder.FindById(eid);
                    var filter  = new Dictionary <string, object>();
                    if (i == 1 && flowInstance.Entity1Id.HasValue && !flowInstance.Entity1Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity1Id);
                    }

                    if (i == 2 && flowInstance.Entity2Id.HasValue && !flowInstance.Entity2Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity2Id);
                    }

                    if (i == 3 && flowInstance.Entity3Id.HasValue && !flowInstance.Entity3Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity3Id);
                    }

                    if (i == 4 && flowInstance.Entity4Id.HasValue && !flowInstance.Entity4Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity4Id);
                    }

                    if (i == 5 && flowInstance.Entity5Id.HasValue && !flowInstance.Entity5Id.Value.Equals(Guid.Empty))
                    {
                        filter.Add(eidMeta.Name + "id", flowInstance.Entity5Id);
                    }

                    if (filter.Count > 0)
                    {
                        rsRecords.Add(eid.ToString(), _dataFinder.RetrieveByAttribute(eidMeta.Name, filter));
                    }
                    else
                    {
                        rsRecords.Add(eid.ToString(), null);
                    }

                    i++;
                }
                model.RelatedRecords = rsRecords;
            }
            return(View($"~/Views/Flow/{WebContext.ActionName}.cshtml", model));
        }
示例#3
0
        public bool Assign(Schema.Domain.Entity entityMetadata, Entity entity, OwnerObject owner, bool ignorePermissions = false)
        {
            if (!ignorePermissions)
            {
                VerifyEntityPermission(entity, AccessRightValue.Assign, entityMetadata);
            }
            var    recordId     = entity.GetIdValue();
            Entity ownerEntity  = null;
            Entity updateEntity = new Entity(entityMetadata.Name);

            updateEntity.SetIdValue(recordId);
            updateEntity.SetAttributeValue("ownerid", owner);
            if (owner.OwnerType == OwnerTypes.SystemUser)
            {
                if (owner.OwnerId.Equals(_user.SystemUserId))
                {
                    updateEntity.SetAttributeValue("owningbusinessunit", _user.BusinessUnitId);
                }
                else
                {
                    //business unit
                    var queryBusinessUnit = new QueryExpression("systemuser", _languageId);
                    queryBusinessUnit.ColumnSet.AddColumn("businessunitid");
                    queryBusinessUnit.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, owner.OwnerId);
                    ownerEntity = _organizationDataRetriever.Retrieve(queryBusinessUnit, true);

                    updateEntity.SetAttributeValue("owningbusinessunit", ownerEntity.GetGuidValue("businessunitid"));
                }
                updateEntity.SetAttributeValue("modifiedon", DateTime.Now);
                updateEntity.SetAttributeValue("modifiedby", _user.SystemUserId);
            }
            else
            {
                updateEntity.SetAttributeValue("owningbusinessunit", null);
            }
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityMetadata.EntityId);
            var result             = true;

            try
            {
                _organizationDataProvider.BeginTransaction();
                InternalOnAssign(entity, updateEntity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                result = _organizationDataProvider.Update(updateEntity);
                if (result)
                {
                    //assign cascade relationship, 1: N
                    var relationships = _relationShipFinder.Query(n => n
                                                                  .Where(f => f.ReferencedEntityId == entityMetadata.EntityId && f.CascadeAssign != (int)CascadeUpdateType.None)
                                                                  );
                    if (relationships.NotEmpty())
                    {
                        foreach (var rs in relationships)
                        {
                            var relatedEntityMeta = _entityFinder.FindById(rs.ReferencingEntityId);
                            if (relatedEntityMeta.EntityMask == EntityMaskEnum.Organization)
                            {
                                continue;
                            }

                            var queryRelated = new QueryExpression(rs.ReferencingEntityName, _languageId);
                            queryRelated.ColumnSet.AddColumns(rs.ReferencingEntityName + "id");
                            queryRelated.Criteria.AddCondition(rs.ReferencingAttributeName, ConditionOperator.Equal, recordId);
                            //update related records
                            Entity updEntity = new Entity(rs.ReferencingEntityName);
                            updEntity.SetAttributeValue("ownerid", owner);
                            if (owner.OwnerType == OwnerTypes.SystemUser)
                            {
                                updEntity.SetAttributeValue("owningbusinessunit", ownerEntity.GetGuidValue("businessunitid"));
                            }
                            else
                            {
                                updEntity.SetAttributeValue("owningbusinessunit", null);
                            }
                            updEntity.SetAttributeValue("modifiedon", DateTime.Now);
                            updEntity.SetAttributeValue("modifiedby", _user.SystemUserId);
                            _organizationDataProvider.Update(updEntity, _queryResolverFactory.Get(queryRelated), true);
                        }
                    }
                    _organizationDataProvider.CommitTransaction();
                    InternalOnAssign(entity, updateEntity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
                else
                {
                    _organizationDataProvider.RollBackTransaction();
                }
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                return(OnException(e));
            }
            return(true);
        }
示例#4
0
        private bool DeleteCore(Schema.Domain.Entity entityMetadata, List <Schema.Domain.Attribute> attributeMetadatas, Entity record, bool ignorePermissions = false)
        {
            if (!ignorePermissions)
            {
                VerifyEntityPermission(record, AccessRightValue.Delete, entityMetadata);
            }
            //cascade relationship, 1: N
            var relationships = _relationShipFinder.Query(n => n
                                                          .Where(f => f.ReferencedEntityId == entityMetadata.EntityId)
                                                          );
            //check referenced
            var cascadeDeleteRestrict = relationships.Where(n => n.ReferencedEntityId == entityMetadata.EntityId && n.CascadeDelete == (int)CascadeDeleteType.Restrict && n.RelationshipType == RelationShipType.ManyToOne);

            var primaryAttr = attributeMetadatas.Find(n => n.TypeIsPrimaryKey());
            var primarykey  = primaryAttr.Name;

            record.IdName = primarykey;
            var recordId = record.GetIdValue();

            foreach (var cdr in cascadeDeleteRestrict)
            {
                var referencingRecord = _aggregateService.Count(cdr.ReferencingEntityName, new FilterExpression(LogicalOperator.And).AddCondition(cdr.ReferencingAttributeName, ConditionOperator.Equal, recordId));
                if (referencingRecord > 0)
                {
                    _relationShipFinder.WrapLocalizedLabel(cdr);
                    return(OnException(_loc["referenced"] + ": " + cdr.ReferencingEntityLocalizedName));
                }
            }
            var result = false;

            try
            {
                _organizationDataProvider.BeginTransaction();
                InternalOnDelete(record, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                //delete related records
                var cascadeDelete = relationships.Where(n => n.ReferencedEntityId == entityMetadata.EntityId && n.CascadeDelete == (int)CascadeDeleteType.All && n.RelationshipType == RelationShipType.ManyToOne).ToList();
                if (cascadeDelete.NotEmpty())
                {
                    DeleteRelatedRecords(entityMetadata, attributeMetadatas, cascadeDelete, recordId);
                }
                //delete main record
                result = _organizationDataProvider.Delete(record.Name, recordId, primarykey);
                if (result)
                {
                    //update maps
                    _mapUpdater.Update(entityMetadata, record, true);
                    _formulaUpdater.Update(entityMetadata, record);
                    _organizationDataProvider.CommitTransaction();
                    InternalOnDelete(record, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
                else
                {
                    _organizationDataProvider.RollBackTransaction();
                }
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                OnException(e);
            }

            return(result);
        }