Exemple #1
0
        public bool Update(Entity entity, bool ignorePermissions = false)
        {
            var entityMetadata     = GetEntityMetaData(entity.Name);
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityMetadata.EntityId);

            VerifyUpdate(entity, entityMetadata, attributeMetadatas);
            var query = new QueryExpression(entity.Name, _languageId);

            query.ColumnSet.AllColumns = true;
            query.Criteria.AddCondition(entity.IdName, ConditionOperator.Equal, entity.GetIdValue());
            var originalEntity = _organizationDataRetriever.Retrieve(query, ignorePermissions);

            if (!ignorePermissions)
            {
                VerifyEntityPermission(originalEntity, AccessRightValue.Update, entityMetadata);
            }
            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");
            }
            var result = true;

            //保存前发布事件
            PublishEvents(originalEntity, entity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
            try
            {
                _organizationDataProvider.BeginTransaction();
                InternalOnUpdate(originalEntity, entity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                result = _organizationDataProvider.Update(entity);
                if (result)
                {
                    _mapUpdater.Update(entityMetadata, originalEntity);
                    _formulaUpdater.Update(entityMetadata, originalEntity);
                    if (ownerChanged)//改变了所有者
                    {
                        _dataAssigner.Assign(entityMetadata, entity, ownerObj);
                    }
                    _organizationDataProvider.CommitTransaction();
                    InternalOnUpdate(originalEntity, entity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
                else
                {
                    _organizationDataProvider.RollBackTransaction();
                }
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                OnException(e);
            }
            if (result)
            {
                //保存成功后发布事件
                PublishEvents(originalEntity, entity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
            }
            return(result);
        }
Exemple #2
0
        public Guid Create(Entity entity, bool ignorePermissions = false)
        {
            var entityMetadata     = GetEntityMetaData(entity.Name);
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityMetadata.EntityId);

            VerifyCreate(entity, entityMetadata, attributeMetadatas, ignorePermissions);
            Guid id = Guid.Empty;

            try
            {
                _organizationDataProvider.BeginTransaction();
                InternalOnCreate(entity, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                id = _organizationDataProvider.Create(entity);
                if (entityMetadata.EntityMask == EntityMaskEnum.User)
                {
                    var owner = entity["ownerid"] as OwnerObject;
                    //assign to other
                    if (!owner.OwnerId.Equals(this._user.SystemUserId))
                    {
                        _dataAssigner.Assign(entityMetadata.EntityId, id, owner);
                    }
                }
                if (!id.Equals(Guid.Empty))
                {
                    var existsData = entity.UnWrapAttributeValue();
                    _mapUpdater.Update(entityMetadata, existsData);
                    //字段表达式计算
                    _formulaUpdater.Update(entityMetadata, existsData);
                }
                _organizationDataProvider.CommitTransaction();
                InternalOnCreate(entity, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                id = Guid.Empty;
                OnException(new XmsException(e));
            }
            return(id);
        }
Exemple #3
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);
        }