예제 #1
0
파일: DataUpdater.cs 프로젝트: xixiky/xms
        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);
        }
예제 #2
0
파일: DataFinder.cs 프로젝트: xixiky/xms
 /// <summary>
 /// 查询一条记录
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public Entity Retrieve(QueryBase request, bool ignorePermissions = false)
 {
     VerifyQuery(request);
     if (!ignorePermissions)
     {
         //get entity permissions
         BindUserEntityPermissions(request, AccessRightValue.Read);
     }
     return(_organizationDataRetriever.Retrieve(request, ignorePermissions));
 }
예제 #3
0
        public bool Delete(string name, Guid id, bool ignorePermissions = false)
        {
            var entityMetadata     = GetEntityMetaData(name);
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityMetadata.EntityId);
            var primaryAttr        = attributeMetadatas.Find(n => n.TypeIsPrimaryKey());
            var primarykey         = primaryAttr.Name;
            var query = new QueryExpression(name, _languageId);

            query.ColumnSet.AllColumns = true;
            query.Criteria.AddCondition(primarykey, ConditionOperator.Equal, id);
            var record = _organizationDataRetriever.Retrieve(query, true);

            return(this.DeleteCore(entityMetadata, attributeMetadatas, record, ignorePermissions));
        }
예제 #4
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);
        }