/// <summary> /// Creates rule criteria. /// </summary> /// <param name="serializedFilters">Serialized rule filters.</param> /// <param name="scoringObjectId">Unique identifier of score object.</param> /// <param name="userConnection">A <see cref="instance"/> of the current user connection.</param> /// <param name="generateQuery">True if requared query text; otherwise, false.</param> /// <returns></returns> public string CreateRuleCriteria(string serializedFilters, Guid scoringObjectId, UserConnection userConnection, bool generateQuery = false) { if (string.IsNullOrEmpty(serializedFilters)) { return(string.Empty); } EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager; DataContract.Filters jsonFilters = Json.Deserialize <DataContract.Filters>(serializedFilters); EntitySchema scoringObjectSchema = entitySchemaManager.GetInstanceByUId(scoringObjectId); IEntitySchemaQueryFilterItem esqFilters = jsonFilters.BuildEsqFilter(scoringObjectSchema.UId, userConnection); var scoringObjectEsq = new EntitySchemaQuery(userConnection.EntitySchemaManager, scoringObjectSchema.Name); scoringObjectEsq.UseAdminRights = false; scoringObjectEsq.PrimaryQueryColumn.IsVisible = true; scoringObjectEsq.Filters.Add(esqFilters); Select scoringObjectSelect = scoringObjectEsq.GetSelectQuery(userConnection); if (generateQuery) { string query = scoringObjectSelect.GetSqlText(); return(query); } string serializedSelectQuery = SerializeSelectQuery(scoringObjectSelect); return(serializedSelectQuery); }
/// <summary> /// Convert client filters to Select object. /// </summary> /// <param name="userConnection">A <see cref="instance"/> of the current user connection.</param> /// <param name="ruleId">Unique identifier of the scoring rule.</param> /// <param name="entitySchemaName">Rule entity schema name.</param> /// <returns>Select object</returns> public Select GetRuleSelectQuery(UserConnection userConnection, Guid ruleId, string entitySchemaName) { EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager; EntitySchemaQuery formESQ = new EntitySchemaQuery(entitySchemaManager, entitySchemaName); formESQ.PrimaryQueryColumn.IsAlwaysSelect = true; formESQ.AddColumn("SearchData"); formESQ.Filters.Add(formESQ.CreateFilterWithParameters( FilterComparisonType.Equal, "Id", new object[] { ruleId } )); EntityCollection formEntityCollection = formESQ.GetEntityCollection(userConnection); Entity entity = formEntityCollection.First(); byte[] searchData = entity.GetColumnValue("SearchData") as byte[]; string serializedFilters = UTF8Encoding.UTF8.GetString(searchData, 0, searchData.Length); DataContract.Filters jsonFilters = Json.Deserialize <DataContract.Filters>(serializedFilters); var leadSchema = entitySchemaManager.GetInstanceByName("Lead"); var esqFilters = jsonFilters.BuildEsqFilter(leadSchema.UId, userConnection); var leadEsq = new EntitySchemaQuery(userConnection.EntitySchemaManager, leadSchema.Name); leadEsq.UseAdminRights = false; leadEsq.PrimaryQueryColumn.IsVisible = true; leadEsq.Filters.Add(esqFilters); return(leadEsq.GetSelectQuery(userConnection)); }
/// <summary> /// Returns queue object query with filtration. /// </summary> /// <param name="entitySchemaName">Queue object schema name.</param> /// <param name="filters">Queue object filters.</param> /// <returns>Queue object query with filtration</returns> protected Select GetQueueEntitySelectWithFilter(string entitySchemaName, ServiceDataContract.Filters filters) { EntitySchemaManager entitySchemaManager = _userConnection.EntitySchemaManager; var esq = new EntitySchemaQuery(entitySchemaManager, entitySchemaName); esq.PrimaryQueryColumn.IsAlwaysSelect = true; IEntitySchemaQueryFilterItem esqFilters = filters.BuildEsqFilter(entitySchemaName, _userConnection); esq.Filters.Add(esqFilters); return(esq.GetSelectQuery(_userConnection)); }
/// <summary> /// Updates content of autoupdating queue. /// </summary> /// <param name="queueId">Identifier of queue.</param> /// <param name="queueName">Name of queue.</param> /// <param name="filterData">Serialized filter.</param> /// <param name="itemsCount">Count of not handled elements in queue.</param> protected void ProcessAutoUpdateQueueItems(Guid queueId, string queueName, string filterData, int itemsCount) { try { if (filterData.IsNullOrEmpty()) { QueuesUtilities.LogWarn(string.Format(GetResourceValue("EmptyQueueFiltersWarning"), queueName)); return; } ServiceDataContract.Filters filters = Json.Deserialize <ServiceDataContract.Filters>(filterData); if ((filters == null) || ((filters.FilterType == ServiceDataContract.FilterType.FilterGroup) && (filters.Items.Count == 0))) { QueuesUtilities.LogWarn(string.Format(GetResourceValue("EmptyQueueFiltersWarning"), queueName)); return; } string entitySchemaName = filters.RootSchemaName; int addedRecords = 0; if (itemsCount < _maximumQueueItemsInQueue) { QueuesUtilities.LogDebug(string.Format(GetResourceValue("StartInsertQueueItemsMessage"), queueName, _autoUpdateQueuesProcessId.ToString())); Select addEntitySelect = GetQueueEntitySelectWithFilter(entitySchemaName, filters); addedRecords = AddEntityQueueItemsByFilter(entitySchemaName, addEntitySelect, queueId, queueName); QueuesUtilities.LogDebug(string.Format(GetResourceValue("EndInsertQueueItemsMessage"), queueName, _autoUpdateQueuesProcessId.ToString())); } else { QueuesUtilities.LogWarn(string.Format(GetResourceValue("QueueMaximumItemsWarning"), queueName)); } QueuesUtilities.LogDebug(string.Format(GetResourceValue("StartDeleteQueueItemsMessage"), queueName, _autoUpdateQueuesProcessId.ToString())); Select deleteEntitySelect = GetQueueEntitySelectWithFilter(entitySchemaName, filters); var deletedRecords = DeleteEntityQueueItemsByFilter(entitySchemaName, deleteEntitySelect, queueId, queueName); QueuesUtilities.LogDebug(string.Format(GetResourceValue("EndDeleteQueueItemsMessage"), queueName, _autoUpdateQueuesProcessId.ToString())); QueuesUtilities.LogDebug(string.Format(GetResourceValue("ProcessAutoUpdateQueueResultMessage"), queueName, addedRecords, deletedRecords)); } catch (Exception e) { QueuesUtilities.LogError(string.Format(GetResourceValue("InvokeMethodErrorMessage"), string.Concat("ProcessAutoUpdateQueueItems ", queueName), e.Message), e); } }