示例#1
0
文件: DataFinder.cs 项目: xixiky/xms
 /// <summary>
 /// 查询所有记录
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public List <Entity> RetrieveAll(QueryBase request, bool ignorePermissions = false)
 {
     VerifyQuery(request);
     if (!ignorePermissions)
     {
         //get entity permissions
         BindUserEntityPermissions(request, AccessRightValue.Read);
     }
     return(_organizationDataRetriever.RetrieveAll(request, ignorePermissions)?.ToList());
 }
示例#2
0
        private bool DeleteRelatedRecords(Schema.Domain.Entity entityMetaData, List <Schema.Domain.Attribute> attributeMetadatas, List <Schema.Domain.RelationShip> relationships, Guid parentId)
        {
            var result          = true;
            var primaryKeyField = attributeMetadatas.Find(x => x.TypeIsPrimaryKey());

            foreach (var rs in relationships)
            {
                //delete the 'N' records
                var query = new QueryExpression(rs.ReferencingEntityName, _languageId);
                query.ColumnSet.AddColumn(primaryKeyField.Name);
                query.Criteria.AddCondition(rs.ReferencingAttributeName, ConditionOperator.Equal, parentId);
                var datas = _organizationDataRetriever.RetrieveAll(query, true)?.ToList();
                if (datas.NotEmpty())
                {
                    var entityMetadata2 = GetEntityMetaData(datas.First().Name);
                    var relationships2  = _relationShipFinder.Query(n => n
                                                                    .Where(f => f.ReferencedEntityId == entityMetadata2.EntityId && f.RelationshipType == RelationShipType.ManyToOne && f.CascadeDelete == (int)CascadeDeleteType.All)
                                                                    );
                    foreach (var item in datas)
                    {
                        if (relationships2.NotEmpty())
                        {
                            DeleteRelatedRecords(entityMetadata2, attributeMetadatas, relationships2, item.GetIdValue());
                        }
                        InternalOnDelete(item, OperationStage.PreOperation, entityMetaData, attributeMetadatas);
                        result = _organizationDataProvider.Delete(item.Name, item.GetIdValue());
                        if (result)
                        {
                            InternalOnDelete(item, OperationStage.PostOperation, entityMetaData, attributeMetadatas);
                            _mapUpdater.Update(entityMetadata2, item, true);
                            _formulaUpdater.Update(entityMetadata2, item);
                        }
                    }
                }
            }
            return(result);
        }
示例#3
0
文件: DataUpdater.cs 项目: xixiky/xms
        public bool Update(IList <Entity> entities, bool ignorePermissions = false)
        {
            var entityName         = entities.First().Name;
            var entityMetadata     = GetEntityMetaData(entityName);
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityMetadata.EntityId);

            foreach (var entity in entities)
            {
                VerifyUpdate(entity, entityMetadata, attributeMetadatas);
            }
            var query = new QueryExpression(entityName, _languageId);

            query.ColumnSet.AllColumns = true;
            query.Criteria.AddCondition(entities.First().IdName, ConditionOperator.In, entities.Select(x => x.GetIdValue()).ToArray());
            var existsEntities = _organizationDataRetriever.RetrieveAll(query, ignorePermissions);

            if (!ignorePermissions)
            {
                foreach (var entity in existsEntities)
                {
                    VerifyEntityPermission(entity, AccessRightValue.Update, entityMetadata);
                }
            }
            foreach (var entity in existsEntities)
            {
                var originalEntity = existsEntities.First(x => x.GetIdValue().Equals(entity.GetIdValue()));
                //保存前发布事件
                PublishEvents(originalEntity, entity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
            }
            var result = true;

            _organizationDataProvider.BeginTransaction();
            try
            {
                foreach (var entity in entities)
                {
                    var  originalEntity = existsEntities.First(x => x.GetIdValue().Equals(entity.GetIdValue()));
                    var  ownerObj       = entityMetadata.EntityMask == EntityMaskEnum.User && entity.ContainsKey("ownerid") ? (OwnerObject)entity["ownerid"] : null;
                    bool ownerChanged   = ownerObj != null && !ownerObj.OwnerId.Equals(originalEntity.GetGuidValue("ownerid"));//是否改变了所有者
                    if (ownerChanged)
                    {
                        entity.RemoveKeys("ownerid");
                    }
                    InternalOnUpdate(originalEntity, entity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                    result = _organizationDataProvider.Update(entity);
                    _mapUpdater.Update(entityMetadata, originalEntity);
                    _formulaUpdater.Update(entityMetadata, originalEntity);
                    if (ownerChanged)//改变了所有者
                    {
                        _dataAssigner.Assign(entityMetadata, entity, ownerObj);
                    }
                    InternalOnUpdate(originalEntity, entity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
                _organizationDataProvider.CommitTransaction();
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                OnException(e);
            }
            if (result)
            {
                foreach (var entity in existsEntities)
                {
                    var originalEntity = existsEntities.First(x => x.GetIdValue().Equals(entity.GetIdValue()));
                    //保存后发布事件
                    PublishEvents(originalEntity, entity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
            }
            return(result);
        }