Example #1
0
        protected Tuple <List <dynamic>, List <dynamic> > GetAccounts(FilterCriteriaSet filter, Period period)
        {
            List <dynamic> details;

            List <dynamic> entities = DynamicRepository.GetByEntity(AccountObject.EntityName, filter);

            if (!AccountObject.EntityName.Equals(AccountObject.DetailEntityName, StringComparison.InvariantCultureIgnoreCase))
            {
                var filterForDetails =
                    FilterCriteriaSet.And.In(entities.Select(e => (Guid)(e as DynamicEntity).GetId()).ToList(),
                                             AccountObject.DetailSearchProperty);

                if (period != null && !string.IsNullOrEmpty(AccountObject.PeriodProperty))
                {
                    filterForDetails = filterForDetails.Equal(period.Id, AccountObject.PeriodProperty);
                }

                details = DynamicRepository.GetByEntity(AccountObject.DetailEntityName, filterForDetails);
            }
            else
            {
                details = entities;
            }

            return(new Tuple <List <dynamic>, List <dynamic> >(entities, details));
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="idProperty"></param>
        /// <param name="baseFilter"></param>
        /// <param name="replaceIds"></param>
        /// <param name="ignoreNotFoundReplaces">Если true - то при отсутствии ключа в словаре замен не вставляем новую запись</param>
        /// <returns></returns>
        public Dictionary <object, object> CopyDependsEntity(string entityName, string idProperty,
                                                             FilterCriteriaSet baseFilter,
                                                             Dictionary <string, Dictionary <object, object> > replaceIds, bool ignoreNotFoundReplaces = false)
        {
            List <dynamic> allocationRecords = DynamicRepository.GetByEntity(entityName, baseFilter);
            var            dependsReplaceId  = new Dictionary <object, object>();
            var            entitiesToInsert  = new List <dynamic>();

            foreach (DynamicEntity r in allocationRecords)
            {
                if (!string.IsNullOrWhiteSpace(idProperty))
                {
                    object oldId = r[idProperty];
                    r[idProperty] = Guid.NewGuid();
                    dependsReplaceId.Add(oldId, r[idProperty]);
                }

                var needInsert = true;

                foreach (var replaceId in replaceIds)
                {
                    object newValue;

                    if (!replaceId.Value.TryGetValue(r[replaceId.Key], out newValue))
                    {
                        if (!ignoreNotFoundReplaces)
                        {
                            throw new Exception(string.Format("Not found new value for property {0}. Old Value = {1}",
                                                              replaceId.Key, r[replaceId.Key]));
                        }
                        else
                        {
                            needInsert = false;
                            break;
                        }
                    }
                    else
                    {
                        r[replaceId.Key] = newValue;
                    }
                }

                if (needInsert)
                {
                    entitiesToInsert.Add(r);
                }
            }

            DynamicRepository.InsertByEntity(entityName, entitiesToInsert);

            return(dependsReplaceId);
        }
Example #3
0
        public static List <dynamic> GetRoutesForUser(Guid userId)
        {
            string filterStr = string.Format(@"Id in 
(select EntityRouteId from [dbo].[EntityRouteUser] 
    where SecurityUserId = @UserIdValue) OR (select COUNT(*) from [dbo].[EntityRouteUser] where EntityRoute.Id = EntityRouteId) = 0");

            var param = new Dictionary <string, object>();

            param.Add("UserIdValue", userId);
            FilterCriteriaSet filter = FilterCriteriaSet.And.Custom(filterStr, param);

            return(DynamicRepository.GetByEntity("EntityRoute", filter, OrderByCriteriaSet.Asc("Name")));
        }
Example #4
0
        public static bool VisibilityAccessCheck(EntityMetadata metadata, Guid id, FilterCriteriaSet visibilityFilter)
        {
            if (visibilityFilter.IsEmpty)
                return true;

            var accessFilter =
                FilterCriteriaSet.Or.Merge(visibilityFilter)
                    .Merge(CommonMethods.GetInboxFilter(metadata))
                    .Merge(CommonMethods.GetProcessedFilter(metadata));
            accessFilter = FilterCriteriaSet.And.Equal(id, metadata.PrimaryKeyPropertyName).Merge(accessFilter);

            var cnt = metadata.Count(accessFilter);
            return cnt == 1;
        }
Example #5
0
        protected RecalculationReport RecalculateAccount(FilterCriteriaSet filter, Guid processId, string periodAlias,
                                                         int attemptNumber)
        {
            var report = new RecalculationReport();

            List <Guid> idsToRecalc = null;

            var period =
                Periods.FirstOrDefault(p => p.Alias.Equals(periodAlias, StringComparison.InvariantCultureIgnoreCase));

            var accounts = GetAccounts(filter, period);

            var ids = accounts.Item1.Select(a => (Guid)(a as DynamicEntity).GetId()).ToList();

            try
            {
                idsToRecalc = ids.Where(id => Locker.TryLock(id, processId)).ToList();

                report.NotCalculatedDueToLock.AddRange(ids.Where(id => !idsToRecalc.Contains(id)));


                var etran = GetCalculationObjects(ids, period, CalculationObjectType.ExternalTransaction);
                var itran = GetCalculationObjects(ids, period, CalculationObjectType.InternalTransaction);

                var res = Recalc(accounts, etran, itran);

                foreach (var resItem in res)
                {
                    DynamicRepository.UpdateByEntity(resItem.Item1, resItem.Item2);
                }

                AfterRecalc(accounts.Item1);
            }
            finally
            {
                if (idsToRecalc != null)
                {
                    idsToRecalc.ForEach(id => Locker.Unlock(id, processId));
                }
            }

            if (attemptNumber < RetryCount && report.NotCalculatedDueToLock.Any())
            {
                Thread.Sleep(LimitLockWaitForRecalc);
                return(RecalculateAccount(report.NotCalculatedDueToLock, processId, periodAlias, attemptNumber + 1));
            }

            return(report);
        }
Example #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="idProperty"></param>
        /// <param name="baseFilter"></param>
        /// <param name="replaceIds"></param>
        /// <param name="ignoreNotFoundReplaces">Если true - то при отсутствии ключа в словаре замен не вставляем новую запись</param>
        /// <returns></returns>
        public Dictionary<object, object> CopyDependsEntity(string entityName, string idProperty,
            FilterCriteriaSet baseFilter,
            Dictionary<string, Dictionary<object, object>> replaceIds, bool ignoreNotFoundReplaces = false)
        {
            List<dynamic> allocationRecords = DynamicRepository.GetByEntity(entityName, baseFilter);
            var dependsReplaceId = new Dictionary<object, object>();
            var entitiesToInsert = new List<dynamic>();
            foreach (DynamicEntity r in allocationRecords)
            {
                if (!string.IsNullOrWhiteSpace(idProperty))
                {
                    object oldId = r[idProperty];
                    r[idProperty] = Guid.NewGuid();
                    dependsReplaceId.Add(oldId, r[idProperty]);
                }

                var needInsert = true;

                foreach (var replaceId in replaceIds)
                {
                    object newValue;

                    if (!replaceId.Value.TryGetValue(r[replaceId.Key], out newValue))
                    {
                        if (!ignoreNotFoundReplaces)
                            throw new Exception(string.Format("Not found new value for property {0}. Old Value = {1}",
                                replaceId.Key, r[replaceId.Key]));
                        else
                        {
                            needInsert = false;
                            break;
                        }
                    }
                    else
                    {
                        r[replaceId.Key] = newValue;
                    }

                }

                if (needInsert)
                    entitiesToInsert.Add(r);
            }

            DynamicRepository.InsertByEntity(entityName, entitiesToInsert);

            return dependsReplaceId;
        }
Example #7
0
        public static bool VisibilityAccessCheck(EntityMetadata metadata, Guid id, FilterCriteriaSet visibilityFilter)
        {
            if (visibilityFilter.IsEmpty)
            {
                return(true);
            }

            var accessFilter =
                FilterCriteriaSet.Or.Merge(visibilityFilter)
                .Merge(CommonMethods.GetInboxFilter(metadata))
                .Merge(CommonMethods.GetProcessedFilter(metadata));

            accessFilter = FilterCriteriaSet.And.Equal(id, metadata.PrimaryKeyPropertyName).Merge(accessFilter);

            var cnt = metadata.Count(accessFilter);

            return(cnt == 1);
        }
Example #8
0
        public static List <Tuple <Guid, string, List <Tuple <Guid, string> > > > GetDependingEntityAccess(string metaviewName, Guid[] ids, int deep = 1)
        {
            DynamicEntityRepository           repo     = new DynamicEntityRepository();
            EntityMetadata                    metadata = repo.GetEntityMetadataByEntityName(metaviewName);
            List <Guid>                       dependingEntityMetadataIds = MetadataRepositoty.GetDependingEntityIds(new TableDescription(metadata.SchemaName, metadata.TableName));
            Dictionary <Guid, EntityMetadata> dependingMetadata          = dependingEntityMetadataIds.Select(id => new KeyValuePair <Guid, EntityMetadata>(id, repo.GetEntityMetadataByEntityId(id)))
                                                                           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            var dependingEntities = new List <Tuple <Guid, string, List <Tuple <Guid, string> > > >();

            foreach (var entityMetadata in dependingMetadata)
            {
                FilterCriteriaSet    filters = FilterCriteriaSet.Empty;
                AttributeDescription entityCaptionAttribute = OptimaJet.BJet.CommonMethods.GetEntityCaptionAttribute(entityMetadata.Value);

                List <FilterCriteriaSet> filterSets = entityMetadata.Value.PlainAttributes.Where(a => a.IsReference && a.ReferencedSchemaName == metadata.SchemaName && a.ReferencedTableName == metadata.TableName)
                                                      .Select(a => FilterCriteriaSet.And.In(ids.ToList(), entityMetadata.Value.GetColumn(a))).ToList();

                filterSets.ForEach(f => filters = filters != FilterCriteriaSet.Empty ? filters.Merge(f) : f);

                var entities = repo.GetEntitiesByMetadata(entityMetadata.Value, filters).Entities;
                if (entities == null || entities.Count() == 0)
                {
                    continue;
                }

                var dependingEntity = new Tuple <Guid, string, List <Tuple <Guid, string> > >(
                    entityMetadata.Key,
                    entityMetadata.Value.TableName,
                    new List <Tuple <Guid, string> >(entities.Select(e =>
                                                                     new Tuple <Guid, string>((Guid)e.Id, e.GetProperty(entityCaptionAttribute.PropertyName).ToString())
                                                                     ).ToList()));
                dependingEntities.Add(dependingEntity);

                if (deep > 0)
                {
                    dependingEntities.AddRange(GetDependingEntityAccess(dependingEntity.Item2, dependingEntity.Item3.Select(t => t.Item1).ToArray(), deep - 1));
                }
            }
            return(dependingEntities);
        }
Example #9
0
File: Sync.cs Project: ljxu1/DWKit
        private static object GetSyncRecordId(object id, EntityMetadata metadata, Dictionary <string, object> syncParams)
        {
            if (!metadata.Attributes.Any(c => syncParams.ContainsKey(c.PropertyName)))
            {
                return(id);
            }

            var oldItem = DynamicRepository.Get(metadata, FilterCriteriaSet.And.Equal(id, metadata.PrimaryKeyPropertyName), ExternalMethodsCallMode.None).FirstOrDefault();

            foreach (var p in syncParams)
            {
                if (metadata.Attributes.Any(c => c.PropertyName == p.Key))
                {
                    if ((oldItem as DynamicEntity)[p.Key].Equals(p.Value))
                    {
                        return(id);
                    }
                }
            }

            FilterCriteriaSet compareFilter = FilterCriteriaSet.And;

            foreach (var p in syncParams)
            {
                if (metadata.Attributes.Any(c => c.PropertyName == p.Key))
                {
                    compareFilter.Merge(FilterCriteriaSet.And.Equal(p.Value, p.Key));
                }
            }

            foreach (var attributeToCompare in metadata.Attributes.Where(a => a.ForCompare))
            {
                compareFilter.Merge(FilterCriteriaSet.And.Equal((oldItem as DynamicEntity)[attributeToCompare.PropertyName], attributeToCompare.PropertyName));
            }

            var item = DynamicRepository.Get(metadata, compareFilter, ExternalMethodsCallMode.None).FirstOrDefault();

            return(item != null ? (item as DynamicEntity).GetId() : id);
        }
Example #10
0
        protected RecalculationReport RecalculateAccount(FilterCriteriaSet filter, Guid processId, string periodAlias,
            int attemptNumber)
        {
            var report = new RecalculationReport();

            List<Guid> idsToRecalc = null;

            var period =
                Periods.FirstOrDefault(p => p.Alias.Equals(periodAlias, StringComparison.InvariantCultureIgnoreCase));

            var accounts = GetAccounts(filter, period);

            var ids = accounts.Item1.Select(a => (Guid) (a as DynamicEntity).GetId()).ToList();

            try
            {
                idsToRecalc = ids.Where(id => Locker.TryLock(id, processId)).ToList();

                report.NotCalculatedDueToLock.AddRange(ids.Where(id => !idsToRecalc.Contains(id)));

                var etran = GetCalculationObjects(ids, period, CalculationObjectType.ExternalTransaction);
                var itran = GetCalculationObjects(ids, period, CalculationObjectType.InternalTransaction);

                var res = Recalc(accounts, etran, itran);

                foreach (var resItem in res)
                {
                    DynamicRepository.UpdateByEntity(resItem.Item1, resItem.Item2);
                }

                AfterRecalc(accounts.Item1);

            }
            finally
            {
                if (idsToRecalc != null) idsToRecalc.ForEach(id => Locker.Unlock(id, processId));
            }

            if (attemptNumber < RetryCount && report.NotCalculatedDueToLock.Any())
            {
                Thread.Sleep(LimitLockWaitForRecalc);
                return RecalculateAccount(report.NotCalculatedDueToLock, processId, periodAlias, attemptNumber + 1);
            }

            return report;
        }
Example #11
0
        protected Tuple<List<dynamic>, List<dynamic>> GetAccounts(FilterCriteriaSet filter, Period period)
        {
            List<dynamic> details;

                List<dynamic> entities = DynamicRepository.GetByEntity(AccountObject.EntityName, filter);

                if (!AccountObject.EntityName.Equals(AccountObject.DetailEntityName, StringComparison.InvariantCultureIgnoreCase))
                {
                    var filterForDetails =
                        FilterCriteriaSet.And.In(entities.Select(e => (Guid) (e as DynamicEntity).GetId()).ToList(),
                            AccountObject.DetailSearchProperty);

                    if (period != null && !string.IsNullOrEmpty(AccountObject.PeriodProperty))
                    {
                        filterForDetails = filterForDetails.Equal(period.Id, AccountObject.PeriodProperty);
                    }

                    details = DynamicRepository.GetByEntity(AccountObject.DetailEntityName, filterForDetails);
                }
                else
                {
                    details = entities;
                }

                return new Tuple<List<dynamic>, List<dynamic>>(entities, details);
        }
Example #12
0
 public RecalculationReport RecalculateAccount(FilterCriteriaSet filter, Guid processId, string periodAlias = null)
 {
     return RecalculateAccount(filter, processId, periodAlias, 0);
 }
Example #13
0
 public RecalculationReport RecalculateAccount(FilterCriteriaSet filter, Guid processId, string periodAlias = null)
 {
     return(RecalculateAccount(filter, processId, periodAlias, 0));
 }