/// <summary>
 /// Deletes elements from not active queues.
 /// </summary>
 public void ProcessDeleteInactiveQueueItems()
 {
     try {
         var statusColumns = new List <QueryColumnExpression> {
             new QueryColumnExpression("QueueStatus", "IsFinal"),
             new QueryColumnExpression("QueueStatus", "IsInitial")
         };
         Select queueSelect =
             (Select) new Select(_userConnection)
             .Column("Queue", "Id")
             .From("Queue")
             .InnerJoin("QueueStatus").On("Queue", "StatusId").IsEqual("QueueStatus", "Id")
             .Where("Queue", "Id").IsEqual("QueueItem", "QueueId")
             .And("Queue", "IsManuallyFilling").IsEqual(new QueryParameter(false))
             .And(new QueryParameter(true)).In(statusColumns);
         var delete = (Delete) new Delete(_userConnection)
                      .From("QueueItem")
                      .Where("OperatorId").IsNull()
                      .And().Exists(queueSelect);
         var deletedRecords = delete.Execute();
         QueuesUtilities.LogDebug(string.Format(GetResourceValue("DeleteInactiveQueueItemsMessage"),
                                                deletedRecords));
     } catch (Exception e) {
         QueuesUtilities.LogError(string.Format(GetResourceValue("InvokeMethodErrorMessage"),
                                                "ProcessDeleteInactiveQueueItems", e.Message), e);
         throw;
     }
 }
 /// <summary>
 /// Updates content of auto updating queues.
 /// </summary>
 public virtual void ProcessAutoUpdateQueues()
 {
     _autoUpdateQueuesProcessId = Guid.NewGuid();
     QueuesUtilities.LogDebug(string.Format(GetResourceValue("StartProcessAutoUpdateQueuesMessage"),
                                            _autoUpdateQueuesProcessId.ToString()));
     try {
         int maximumQueueIdQueryParameters          = GetMaximumQueueIdQueryParameters();
         Dictionary <Guid, Entity> queueIdEntityMap = GetQueueIdEntityMap();
         var chunkItems = new HashSet <Guid>();
         ConcurrentDictionary <Guid, QueueInfo> queuesInfo = QueuesInfo;
         foreach (KeyValuePair <Guid, Entity> queue in queueIdEntityMap)
         {
             Guid      queueId = queue.Key;
             QueueInfo queueInfo;
             if (queuesInfo.ContainsKey(queueId))
             {
                 queueInfo = queuesInfo[queueId];
                 Entity queueEntity    = queue.Value;
                 int    queueFrequency = queueEntity.GetTypedColumnValue <int>("Frequency");
                 if (queueInfo.QueueItemsUpdatedOn.AddMinutes(queueFrequency) > DateTime.UtcNow)
                 {
                     continue;
                 }
             }
             else
             {
                 queueInfo = new QueueInfo();
             }
             queueInfo.QueueItemsUpdatedOn = DateTime.UtcNow;
             queuesInfo[queueId]           = queueInfo;
             chunkItems.Add(queueId);
             if (chunkItems.Count >= maximumQueueIdQueryParameters)
             {
                 ProcessQueueChunk(chunkItems, queueIdEntityMap);
                 chunkItems.Clear();
             }
         }
         if (chunkItems.Count > 0)
         {
             ProcessQueueChunk(chunkItems, queueIdEntityMap);
         }
     } catch (Exception e) {
         QueuesUtilities.LogError(string.Format(GetResourceValue("InvokeMethodErrorMessage"), "ProcessAutoUpdateQueues", e.Message), e);
         throw;
     } finally {
         QueuesUtilities.LogDebug(string.Format(GetResourceValue("EndProcessAutoUpdateQueuesMessage"),
                                                _autoUpdateQueuesProcessId.ToString()));
     }
 }
 /// <summary>
 /// Adds elements from queue with filtration.
 /// </summary>
 /// <param name="entitySchemaName">Entity schema name of queue element.</param>
 /// <param name="entitySelect">Query to queue object with filtration.</param>
 /// <param name="queueId">Identifier of queue.</param>
 /// <param name="queueName">Queue name.</param>
 /// <returns></returns>
 private int AddEntityQueueItemsByFilter(string entitySchemaName, Select entitySelect, Guid queueId,
                                         string queueName)
 {
     try {
         entitySelect.Top(_insertQueueItemTopValue)
         .Column(new QueryParameter("QueueId", queueId)).As("QueueId")
         .Column(new QueryParameter("StatusId", _newQueueItemStatusId)).As("StatusId");
         var selectCondition = new QueryCondition();
         selectCondition.IsNot = true;
         selectCondition.Exists(
             new Select(_userConnection)
             .Column("QueueItem", "Id")
             .From("QueueItem")
             .InnerJoin("QueueItemStatus")
             .On("QueueItemStatus", "Id").IsEqual("QueueItem", "StatusId")
             .Where(entitySchemaName, "Id").IsEqual("QueueItem", "EntityRecordId")
             .And("QueueItem", "QueueId").IsEqual(Column.Parameter(queueId))
             .And("QueueItemStatus", "IsFinal").IsNotEqual(Column.Parameter(true)));
         if (!entitySelect.HasCondition)
         {
             entitySelect.Where(selectCondition);
         }
         else
         {
             entitySelect.And(selectCondition);
         }
         var insertSelect = new InsertSelect(_userConnection)
                            .Into("QueueItem")
                            .Set("EntityRecordId", "QueueId", "StatusId")
                            .FromSelect(entitySelect);
         DateTime addedRecordsStartTime = DateTime.Now;
         int      addedRecords          = insertSelect.Execute();
         if (addedRecordsStartTime.AddSeconds(60) < DateTime.Now)
         {
             insertSelect.BuildParametersAsValue = true;
             QueuesUtilities.LogDebug(string.Format(GetResourceValue("LongInsertQueueItemsMessage"),
                                                    queueName, _autoUpdateQueuesProcessId.ToString(), insertSelect.GetSqlText()));
         }
         return(addedRecords);
     } catch (Exception e) {
         QueuesUtilities.LogError(string.Format(GetResourceValue("InvokeMethodErrorMessage"),
                                                string.Concat("AddEntityQueueItemsByFilter ", queueName), e.Message), e);
         throw;
     }
 }
 /// <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);
     }
 }
 /// <summary>
 /// Deletes elements from queue with filtration.
 /// </summary>
 /// <param name="entitySchemaName">Entity schema name in queue.</param>
 /// <param name="entitySelect">Query to queue object with filtration.</param>
 /// <param name="queueId">Identifier of queue.</param>
 /// <param name="queueName">Queue name.</param>
 /// <returns></returns>
 private int DeleteEntityQueueItemsByFilter(string entitySchemaName, Select entitySelect, Guid queueId,
                                            string queueName)
 {
     try {
         var idColumnExpression = new QueryColumnExpression(entitySchemaName, "Id");
         var selectCondition    = new QueryCondition(idColumnExpression);
         selectCondition.IsEqual("QueueItem", "EntityRecordId");
         if (!entitySelect.HasCondition)
         {
             entitySelect.Where(selectCondition);
         }
         else
         {
             entitySelect.And(selectCondition);
         }
         Select select =
             (Select) new Select(_userConnection)
             .Column("Id")
             .From("QueueItemStatus")
             .Where("IsFinal").IsEqual(Column.Parameter(false));
         var delete =
             (Delete) new Delete(_userConnection)
             .From("QueueItem")
             .Where("OperatorId").IsNull()
             .And("QueueItem", "QueueId").IsEqual(Column.Parameter(queueId))
             .And("StatusId").In(Column.SubSelect(select))
             .And().Not().Exists(entitySelect);
         DateTime addedRecordsStartTime = DateTime.Now;
         int      deletedRecords        = delete.Execute();
         if (addedRecordsStartTime.AddSeconds(60) < DateTime.Now)
         {
             delete.BuildParametersAsValue = true;
             QueuesUtilities.LogDebug(string.Format(GetResourceValue("LongDeleteQueueItemsMessage"),
                                                    queueName, _autoUpdateQueuesProcessId.ToString(), delete.GetSqlText()));
         }
         return(deletedRecords);
     } catch (Exception e) {
         QueuesUtilities.LogError(string.Format(GetResourceValue("InvokeMethodErrorMessage"),
                                                string.Concat("DeleteEntityQueueItemsByFilter ", queueName), e.Message), e);
         throw;
     }
 }