Example #1
0
        private DuplicatesGroupResponse ConvertServerEntityToClientEntity(EntitySchemaQuery esq,
                                                                          List <EntityDuplicatesGroup> entityGroups)
        {
            DuplicatesGroupResponse result = new DuplicatesGroupResponse()
            {
                Groups = new Collection <EntityClientDuplicatesGroup>()
            };
            Dictionary <string, string> serverToClientColumnNameMap = GetQueryColumns(esq);

            foreach (EntityDuplicatesGroup groupItem in entityGroups)
            {
                EntityCollection convertedEntities =
                    QueryExtension.GetEntityCollection(groupItem.Duplicates,
                                                       serverToClientColumnNameMap);
                EntityClientDuplicatesGroup clientGroup = new EntityClientDuplicatesGroup()
                {
                    GroupId = groupItem.GroupId,
                    Rows    = convertedEntities
                };
                result.Groups.Add(clientGroup);
            }
            if (result.Groups.Any())
            {
                Dictionary <string, object> columnConfig =
                    QueryExtension.GetColumnConfig(esq, serverToClientColumnNameMap);
                result.RowConfig = columnConfig;
            }
            result.Groups.OrderBy(group => group.GroupId);
            return(result);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of ImmediateNotificationRegister class.
        /// </summary>
        /// <param name="context">an instance of DbContext is used to get an ObjectQuery object</param>
        /// <param name="query">an instance of IQueryable is used to get ObjectQuery object, and then get
        /// _connection string and _command string to register SqlDependency nitification. </param>
        public SqlNotificationListener(DbContext context, IQueryable query)
        {
            try
            {
                this._iquery = query;
                _context     = context;

                // Get the ObjectQuery directly or convert the DbQuery to ObjectQuery.
                _oquery = QueryExtension.GetObjectQuery <TEntity>(context, _iquery);

                _connection = QueryExtension.GetSqlConnection(_oquery);
                _command    = QueryExtension.GetSqlCommand(_oquery, _connection);
            }
            catch (ArgumentException ex)
            {
                if (ex.ParamName == "context")
                {
                    throw new ArgumentException("Paramter cannot be null", nameof(context), ex);
                }

                throw new ArgumentException("Paramter cannot be null", nameof(query), ex);
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Fails to initialize a new instance of SqlNotificationListener class.", ex);
            }
        }
Example #3
0
        public void Subscribe(IQueryable query)
        {
            if (null == _dependency)
            {
                //Get Query Object from IQuery Interface Value...
                _objectQuery = QueryExtension.GetObjectQuery <TEntity>(_context, query);

                //Get SQL connection string from Query Context...
                string connection = QueryExtension.GetConnectionString(_context);

                //Get SQL Command from Object Query...
                string commandString = QueryExtension.GetSqlString(_objectQuery);

                //Register SQL command
                using (SqlCommand command = new SqlCommand(commandString, new SqlConnection(connection)))
                {
                    //Open SQL Connection
                    command.Connection.Open();

                    command.Notification = null;

                    command.NotificationAutoEnlist = true;

                    // Create a dependency and associate it with the SqlCommand.
                    _dependency           = new SqlDependency(command);
                    _dependency.OnChange += dependency_OnChange;

                    // Execute the command.
                    SqlDataReader reader = command.ExecuteReader();
                }
            }
        }
Example #4
0
 private EntityCollection GetDuplicatesEntityCollection(EntitySchemaQuery esq,
                                                        Dictionary <string, string> serverToClientColumnNameMap, IEnumerable <Guid> entityIds)
 {
     esq.ResetSelectQuery();
     esq.Filters.Clear();
     esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, esq.PrimaryQueryColumn.Path,
                                                    entityIds.Select(id => id.ToString()).Distinct()));
     return(QueryExtension.GetEntityCollection(esq.GetEntityCollection(UserConnection), serverToClientColumnNameMap));
 }
Example #5
0
        /// <inheritdoc cref="IBulkDeduplicationManager.GetDuplicateEntitiesGroups"/>
        public BulkDuplicatesGroupResponse GetDuplicateEntitiesGroups(string entityName, string[] columns,
                                                                      int clientOffset, int clientCount)
        {
            var response = new BulkDuplicatesGroupResponse {
                Groups = new Collection <BulkEntityClientDuplicatesGroup>()
            };
            int totalCountFromBpm = 0;
            var offset            = clientOffset;
            var esq = GetDuplicateEntitiesEsq(entityName);
            var serverToClientColumnNameMap = GetServerToClientColumnNameMap(columns, esq);

            for (int i = 0; i < MaxFetchPages; i++)
            {
                var duplicateRecordGroups = FetchDuplicatesFromService(entityName, offset, clientCount);
                if (duplicateRecordGroups == null)
                {
                    break;
                }
                var duplicatesCount = duplicateRecordGroups.Sum(x => x.Count());
                offset += duplicatesCount;
                foreach (var duplicateRecords in duplicateRecordGroups)
                {
                    Guid groupId           = duplicateRecords.Key;
                    var  duplicateEntities = GetDuplicateEntities(esq, serverToClientColumnNameMap, duplicateRecords.ToList());
                    var  group             = response.Groups.Find(x => x.GroupId == groupId);
                    if (group == null)
                    {
                        group = new BulkEntityClientDuplicatesGroup {
                            GroupId = groupId,
                            Rows    = new EntityCollection()
                        };
                        response.Groups.Add(group);
                    }
                    int missingRecords = clientCount - totalCountFromBpm;
                    int skippedRecords = duplicateEntities.Count - missingRecords;
                    totalCountFromBpm  += duplicateEntities.Count;
                    response.NextOffset = offset - skippedRecords;
                    group.Rows.AddRange(duplicateEntities.Take(missingRecords));
                }
                if (duplicatesCount < clientCount || totalCountFromBpm >= clientCount)
                {
                    break;
                }
            }
            if (response.Groups.IsEmpty())
            {
                return(null);
            }
            AddIsUniqueColumn(esq);
            response.RowConfig         = QueryExtension.GetColumnConfig(esq, serverToClientColumnNameMap);
            response.TotalCountRecords = response.Groups.Sum(x => x.Rows.Count);
            return(response);
        }
Example #6
0
        private EntityCollection GetDuplicateEntities(EntitySchemaQuery esq,
                                                      Dictionary <string, string> serverToClientColumnNameMap, IList <DuplicateInfo> entities)
        {
            esq.ResetSelectQuery();
            esq.Filters.Clear();
            esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, esq.PrimaryQueryColumn.Path,
                                                           entities.Select(entity => entity.RecordId.ToString()).Distinct()));
            var duplicatesCollection = QueryExtension.GetEntityCollection(esq.GetEntityCollection(UserConnection), serverToClientColumnNameMap);

            foreach (var item in duplicatesCollection)
            {
                item.Add(IsUniqueAttributeName, entities.First(x => x.RecordId == GetRowRecordId(item, esq)).IsUnique);
            }
            return(duplicatesCollection);
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of ImmediateNotificationRegister class.
        /// </summary>
        /// <param name="query">an instance of ObjectQuery is used to get _connection string and
        /// _command string to register SqlDependency nitification. </param>
        public SqlNotificationListener(ObjectQuery query)
        {
            try
            {
                this._oquery = query;

                _connection = QueryExtension.GetSqlConnection(_oquery);
                _command    = QueryExtension.GetSqlCommand(query, _connection);
            }
            catch (ArgumentException ex)
            {
                throw new ArgumentException("Paramter cannot be null", nameof(query), ex);
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Fails to initialize a new instance of SqlNotificationListener class.", ex);
            }
        }
Example #8
0
        /// <inheritdoc cref="IBulkDeduplicationManager.GetGroupsOfDuplicates"/>
        public BulkDuplicatesGroupResponse GetGroupsOfDuplicates(string entityName, string[] columns,
                                                                 int offset, int count, int topDuplicatesPerGroup)
        {
            var response = new BulkDuplicatesGroupResponse {
                Groups = new Collection <BulkEntityClientDuplicatesGroup>()
            };
            var esq = GetDuplicateEntitiesEsq(entityName);
            var serverToClientColumnNameMap = GetServerToClientColumnNameMap(columns, esq);
            var serviceGroupOffset          = offset;
            var serviceGroupCount           = count;

            for (int i = 0; i < MaxFetchPages; i++)
            {
                var duplicateGroupsInfo = FetchGroupOfDuplicatesFromService(entityName, serviceGroupOffset, serviceGroupCount, topDuplicatesPerGroup);
                foreach (var duplicateGroup in duplicateGroupsInfo)
                {
                    var duplicateGroupResponse = GetDuplicatesGroupResponse(duplicateGroup, esq, serverToClientColumnNameMap);
                    if (duplicateGroupResponse == null)
                    {
                        continue;
                    }
                    response.Groups.Add(duplicateGroupResponse);
                }
                var allRowsFetchedFromService = duplicateGroupsInfo.Count < serviceGroupCount;
                if (allRowsFetchedFromService)
                {
                    serviceGroupOffset = -1;
                    break;
                }
                serviceGroupOffset += serviceGroupCount;
                serviceGroupCount   = count - response.Groups.Count;
                var requestedRowsCountFetched = response.Groups.Count >= count;
                if (requestedRowsCountFetched)
                {
                    break;
                }
            }
            AddIsUniqueColumn(esq);
            response.RowConfig  = QueryExtension.GetColumnConfig(esq, serverToClientColumnNameMap);
            response.NextOffset = serviceGroupOffset;
            return(response);
        }
        private SelectQueryResponse GetVisaData(Select entitiesSelect)
        {
            Dictionary <string, string> serverToClientColumnNameMap = GetColumnNameMap(entitiesSelect);
            var collection = new EntityCollection(_userConnection, _baseVisaSchema);

            using (DBExecutor dbExecutor = _userConnection.EnsureDBConnection()) {
                using (IDataReader dataReader = entitiesSelect.ExecuteReader(dbExecutor)) {
                    collection.Load(dataReader);
                }
            }
            var convertedEntities = QueryExtension.GetEntityCollection(collection, serverToClientColumnNameMap);
            Dictionary <string, object> config   = QueryExtension.GetColumnConfig(_baseVisaESQ, serverToClientColumnNameMap);
            SelectQueryResponse         response = new SelectQueryResponse {
                Rows         = convertedEntities,
                RowsAffected = convertedEntities.Count,
                RowConfig    = config
            };

            return(response);
        }
        public SelectQueryResponse GetEntities(RequestOptions requestOptions)
        {
            Select selectFromProviders = GetSelectFromProviders();

            if (selectFromProviders == null)
            {
                return(new SelectQueryResponse {
                    Rows = null,
                    RowsAffected = 0,
                    RowConfig = null
                });
            }
            Select entitiesSelect = WrapSelect(selectFromProviders);

            ApplyOptions(ref entitiesSelect, requestOptions);
            Dictionary <string, string> serverToClientColumnNameMap = GetColumnNameMap(entitiesSelect);
            CoreEntitySchema            baseVisaSchema = GetExtendedBaseVisaEntitySchema();
            EntitySchemaQuery           baseVisaESQ    = GetExtendedBaseVisaESQ(baseVisaSchema);

            Terrasoft.Core.Entities.EntityCollection collection =
                new Terrasoft.Core.Entities.EntityCollection(_userConnection, baseVisaSchema);
            using (DBExecutor dbExecutor = _userConnection.EnsureDBConnection()) {
                using (IDataReader dataReader = entitiesSelect.ExecuteReader(dbExecutor)) {
                    collection.Load(dataReader);
                }
            }
            var convertedEntities = QueryExtension.GetEntityCollection(collection, serverToClientColumnNameMap);
            Dictionary <string, object> config   = QueryExtension.GetColumnConfig(baseVisaESQ, serverToClientColumnNameMap);
            SelectQueryResponse         response = new SelectQueryResponse {
                Rows         = convertedEntities,
                RowsAffected = convertedEntities.Count,
                RowConfig    = config
            };

            return(response);
        }
Example #11
0
        /// <summary>
        ///  Validates duplicate entities.
        /// </summary>
        /// <param name="schemaName">Schema name.</param>
        /// <param name="duplicateRecordIds">Collection of identifiers of duplicate entities.</param>
        /// <param name="resolvedConflicts">Config for resolving conflicts.</param>
        /// <returns>Validation result.</returns>
        public ValidateDuplicatesResponse ValidateDuplicates(string schemaName, List <Guid> duplicateRecordIds,
                                                             Dictionary <string, string> resolvedConflicts)
        {
            ValidateDuplicatesResponse response = new ValidateDuplicatesResponse();
            EntitySchema     entitySchema       = UserConnection.EntitySchemaManager.GetInstanceByName(schemaName);
            EntityCollection entityCollection   = GetEntityDublicates(schemaName, duplicateRecordIds);
            Entity           goldenEntity       = entityCollection.FirstOrDefault();

            if (goldenEntity == null)
            {
                return(response);
            }
            entityCollection.RemoveFirst();
            List <string> resolvedColumns = new List <string>();

            if (resolvedConflicts != null)
            {
                resolvedColumns = resolvedConflicts.Keys.ToList();
            }
            List <string> conflictColumns = new List <string>();

            foreach (EntitySchemaColumn column in goldenEntity.Schema.Columns)
            {
                if (IsColumnInIgnoreList(column.Name))
                {
                    continue;
                }
                if (GetIsSystemColumn(entitySchema, column))
                {
                    continue;
                }
                if (resolvedColumns.Contains(column.Name))
                {
                    continue;
                }
                bool isConflictColumn = false;
                EntityColumnValue goldenColumnValue = goldenEntity.FindEntityColumnValue(column.ColumnValueName);
                foreach (Entity entity in entityCollection)
                {
                    EntityColumnValue columnValue = entity.FindEntityColumnValue(column.ColumnValueName);
                    if (DataTypeUtilities.ValueIsNullOrEmpty(goldenColumnValue.Value))
                    {
                        goldenColumnValue = columnValue;
                        continue;
                    }
                    if (DataTypeUtilities.ValueIsNullOrEmpty(columnValue.Value))
                    {
                        continue;
                    }
                    if (IsEquals(goldenColumnValue.Value, columnValue.Value) == false)
                    {
                        isConflictColumn = true;
                        break;
                    }
                }
                if (isConflictColumn)
                {
                    conflictColumns.Add(column.Name);
                }
            }
            if (conflictColumns.Any())
            {
                conflictColumns.AddRange(resolvedColumns);
                EntityCollection              conflicts         = GetEntityDublicates(schemaName, duplicateRecordIds, conflictColumns);
                Dictionary <string, string>   columnMap         = GetQueryColumns(conflicts.Schema.Columns);
                DataContract.EntityCollection convertedEntities =
                    QueryExtension.GetEntityCollection(conflicts, columnMap);
                response.Conflicts = convertedEntities;
            }
            return(response);
        }
Example #12
0
 /// <summary>
 /// Bulk inserts in to the table
 /// </summary>
 /// <param name="dataTable">Datatable</param>
 /// <returns>Success or failure</returns>
 public static bool InsertBulk(
     this ISqlQuery _, 
     DataTable dataTable) => QueryExtension.InsertBulk(dataTable);
Example #13
0
 /// <summary>
 /// Non-Select queries: Insert/Update/Delete
 /// </summary>
 /// <param name="sql">Select statement</param>
 /// <param name="parameters">Parameters</param>
 /// <returns>Success or failure</returns>
 public static bool Execute(
     this ISqlQuery _, 
     string sql, params object[] parameters) => QueryExtension.Execute(sql, parameters);
Example #14
0
 /// <summary>
 /// Fetches the last record in the sequence
 /// </summary>
 /// <typeparam name="TSource">Type of the select object</typeparam>
 /// <param name="sql">Select statement</param>
 /// <param name="parameters">Parameters</param>
 /// <returns>TSource instance</returns>
 public static TSource GetLast<TSource>(
     this ISqlQuery _, 
     string sql, 
     params object[] parameters) => QueryExtension.ExecuteSqlSingle<TSource>(sql, true, parameters);
Example #15
0
 /// <summary>
 /// Filters a sequence of TSource elements based on the sql statement, otherwise all.
 /// </summary>
 /// <typeparam name="TSource">Type of the select object</typeparam>
 /// <param name="sql">Select statement</param>
 /// <param name="parameters">Parameters</param>
 /// <returns>List of TSource elements</returns>
 public static IList<TSource> GetList<TSource>(
     this ISqlQuery _,
     string sql, 
     params object[] parameters) => QueryExtension.ExecuteSql<TSource>(sql, parameters);