Ejemplo n.º 1
0
        public virtual List <dynamic> Execute(AggregateExpression agg)
        {
            var           queryResolver = _queryResolverFactory.Get(agg);
            var           sqlString     = queryResolver.ToSqlString();
            List <string> selectFields  = new List <string>();

            foreach (var a in agg.AggregateFields)
            {
                selectFields.Add(DataFieldExpressionHelper.GetAggregationExpression(a.AggregateType, a.AttributeName) + " AS " + a.AttributeName);
            }
            sqlString = string.Format("SELECT {0} FROM ({1}) a", string.Join(",", selectFields).ToLower(), sqlString);

            return(_dataRepository.ExecuteQuery(sqlString, queryResolver.Parameters.Args.ToArray()));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取元数据
 /// </summary>
 /// <param name="fetch"></param>
 public void GetMetaDatas(FetchDescriptor fetch)
 {
     if (fetch.Query == null)
     {
         ToQueryExpression(fetch.FetchConfig);
     }
     else
     {
         this.QueryExpression = fetch.Query;
     }
     this.QueryExpression.PageInfo = new PagingInfo()
     {
         PageNumber = fetch.Page, PageSize = fetch.PageSize
     };
     this.QueryResolver = _queryResolverFactory.Get(QueryExpression);
     GetNonePermissionFields();
 }
Ejemplo n.º 3
0
        public PagedList <Entity> RetrievePaged(QueryBase request, bool ignorePermissions = false)
        {
            var result = _queryResolverFactory.Get(request).QueryPaged(ignorePermissions);
            var ec     = new PagedList <Entity>();
            var items  = new List <Entity>();

            foreach (var item in result.Items)
            {
                var entity = new Entity(request.EntityName, item);
                items.Add(entity);
            }
            ec.Items        = items;
            ec.ItemsPerPage = result.ItemsPerPage;
            ec.CurrentPage  = result.CurrentPage;
            ec.TotalItems   = result.TotalItems;
            ec.TotalPages   = result.TotalPages;
            return(ec);
        }
Ejemplo n.º 4
0
        public bool Update(Entity entity, QueryExpression query, bool ignorePermissions = false)
        {
            var entityMetadata = GetEntityMetaData(entity.Name);

            if (!ignorePermissions)
            {
                BindUserEntityPermissions(query, AccessRightValue.Read);
                VerifyEntityPermission(entity, AccessRightValue.Update, entityMetadata);
            }
            query.ColumnSet.Columns.Clear();
            query.ColumnSet.Columns.Add(entity.IdName);

            return(_organizationDataProvider.Update(entity, _queryResolverFactory.Get(query), ignorePermissions));
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public bool Merge(Guid entityId, Guid mainRecordId, Guid mergedRecordId, Dictionary <string, Guid> attributeMaps, bool ignorePermissions = false)
        {
            var entityMetadata = _entityFinder.FindById(entityId);
            //retrive main record
            var queryMain = new QueryExpression(entityMetadata.Name, _languageId);

            queryMain.ColumnSet.AllColumns = true;
            queryMain.Criteria.AddCondition(entityMetadata.Name + "id", ConditionOperator.Equal, mainRecordId);
            var mainRecord = _dataFinder.Retrieve(queryMain, ignorePermissions);

            if (mainRecord.IsEmpty())
            {
                return(OnException(_loc["notfound_record"]));
            }
            //retrive merged record
            var queryMerged = new QueryExpression(entityMetadata.Name, _languageId);

            queryMerged.ColumnSet.AllColumns = true;
            queryMerged.Criteria.AddCondition(entityMetadata.Name + "id", ConditionOperator.Equal, mergedRecordId);
            var mergedRecord = _dataFinder.Retrieve(queryMerged, ignorePermissions);

            if (mergedRecord.IsEmpty())
            {
                return(OnException(_loc["notfound_record"]));
            }
            var attributeMetadatas = _attributeFinder.FindByEntityId(entityId);
            var updatedRecord      = new Entity(entityMetadata.Name);

            //set target record values
            foreach (var attr in attributeMetadatas)
            {
                if (attr.IsSystemControl())
                {
                    continue;
                }

                if (attributeMaps.ContainsKey(attr.Name))
                {
                    var eid = attributeMaps[attr.Name];
                    updatedRecord.SetAttributeValue(attr.Name, eid.Equals(mainRecordId) ? updatedRecord.WrapAttributeValue(_entityFinder, attr, mainRecord[attr.Name]) : updatedRecord.WrapAttributeValue(_entityFinder, attr, mergedRecord[attr.Name]));
                }
                else
                {
                    updatedRecord.SetAttributeValue(attr.Name, updatedRecord.WrapAttributeValue(_entityFinder, attr, mainRecord[attr.Name]));
                }
            }
            //this.VerifyUpdate(updatedRecord);
            updatedRecord.AddIfNotContain("modifiedon", DateTime.Now);
            updatedRecord.AddIfNotContain("modifiedby", _user.SystemUserId);
            var result = false;

            try
            {
                _organizationDataProvider.BeginTransaction();
                InternalOnMerge(mergedRecord, updatedRecord, OperationStage.PreOperation, entityMetadata, attributeMetadatas);
                //result = _dataUpdater.Update(updatedRecord, ignorePermissions);
                result = _organizationDataProvider.Update(updatedRecord);
                //update referencing records
                var relationships = _relationShipFinder.QueryByEntityId(null, entityId);
                foreach (var rs in relationships)
                {
                    var relatedEntity = new Entity(rs.ReferencingEntityName);
                    relatedEntity.SetAttributeValue(rs.ReferencingAttributeName, mainRecordId);
                    var queryRelated = new QueryExpression(rs.ReferencingEntityName, _languageId);
                    queryRelated.ColumnSet.AddColumns(rs.ReferencingEntityName + "id");
                    queryRelated.Criteria.AddCondition(rs.ReferencingAttributeName, ConditionOperator.Equal, mergedRecordId);
                    _organizationDataProvider.Update(relatedEntity, _queryResolverFactory.Get(queryRelated), ignorePermissions: true);
                }
                //disabled original record
                var disabledEntity = new Entity(entityMetadata.Name);
                disabledEntity.SetIdValue(mergedRecordId);
                disabledEntity.SetAttributeValue("statecode", false);
                //result = _dataUpdater.Update(disabledEntity, ignorePermissions);
                result = _organizationDataProvider.Update(disabledEntity);
                if (result)
                {
                    _organizationDataProvider.CommitTransaction();
                    InternalOnMerge(mergedRecord, updatedRecord, OperationStage.PostOperation, entityMetadata, attributeMetadatas);
                }
                else
                {
                    _organizationDataProvider.RollBackTransaction();
                }
            }
            catch (Exception e)
            {
                _organizationDataProvider.RollBackTransaction();
                OnException(e);
            }
            return(result);
        }