<commit_before>﻿/* Empiria Extensions Framework ******************************************************************************
*                                                                                                            *
*  Module   : Message Queue Services                       Component : Message Queue                         *
*  Assembly : Empiria.Messaging.dll                        Pattern   : Information Holder                    *
*  Type     : MessageQueue                                 License   : Please read LICENSE.txt file          *
*                                                                                                            *
*  Summary  : Queue of messages sent between applications, used to decouple systems                          *
*             through an asynchronous communication protocol.                                                *
*                                                                                                            *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

using System.Collections.Generic;

using Empiria.Json;
using Empiria.StateEnums;

namespace Empiria.Messaging {

  /// <summary>Queue of messages sent between applications, used to decouple systems through
  /// an asynchronous communication protocol.</summary>
  public class MessageQueue: BaseObject {

    #region Fields

    private Lazy<List<Message>> messages = new Lazy<List<Message>>(() => new List<Message>());

    #endregion Fields

    #region Constructor and parsers

    private MessageQueue() {
      // Required by Empiria Framework.
    }


    static public MessageQueue Parse(int id) {
      return BaseObject.ParseId<MessageQueue>(id);
    }


    static public MessageQueue Parse(string queueUID) {
      return BaseObject.ParseKey<MessageQueue>(queueUID);
    }


    static internal MessageQueue Empty {
      get {
        return BaseObject.ParseEmpty<MessageQueue>();
      }
    }


    protected override void OnLoadObjectData(System.Data.DataRow row) {
      messages = new Lazy<List<Message>>(() => MessageQueueData.GetQueueMessages(this));
    }


    #endregion Constructor and parsers

    #region Read properties and methods


    public FixedList<Message> Messages {
      get {
        return messages.Value.ToFixedList();
      }
    }


    public FixedList<Message> GetNextMessages() {
      return this.Messages.FindAll(x => x.IsInProcessStatus);
    }


    public FixedList<Message> GetUnitOfWorkMessages(string unitOfWorkUID) {
      Assertion.AssertObject(unitOfWorkUID, "unitOfWorkUID");

      return this.Messages.FindAll(x => x.UnitOfWorkUID == unitOfWorkUID &&
                                        x.IsInProcessStatus);
    }


    public Message TryGetNextMessage() {
      return this.Messages.Find(x => x.IsInProcessStatus);
    }


    public string TryGetNextUnitOfWork() {
      var message = this.Messages.Find(x => x.UnitOfWorkUID.Length != 0 &&
                                            x.IsInProcessStatus);

      if (message != null) {
        return message.UnitOfWorkUID;
      } else {
        return null;
      }<commit_msg>Fix Sort MessageQueue.GetNextMessages() list by PostingTime<commit_after>﻿/* Empiria Extensions Framework ******************************************************************************
*                                                                                                            *
*  Module   : Message Queue Services                       Component : Message Queue                         *
*  Assembly : Empiria.Messaging.dll                        Pattern   : Information Holder                    *
*  Type     : MessageQueue                                 License   : Please read LICENSE.txt file          *
*                                                                                                            *
*  Summary  : Queue of messages sent between applications, used to decouple systems                          *
*             through an asynchronous communication protocol.                                                *
*                                                                                                            *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

using System.Collections.Generic;

using Empiria.Json;
using Empiria.StateEnums;

namespace Empiria.Messaging {

  /// <summary>Queue of messages sent between applications, used to decouple systems through
  /// an asynchronous communication protocol.</summary>
  public class MessageQueue: BaseObject {

    #region Fields

    private Lazy<List<Message>> messages = new Lazy<List<Message>>(() => new List<Message>());

    #endregion Fields

    #region Constructor and parsers

    private MessageQueue() {
      // Required by Empiria Framework.
    }


    static public MessageQueue Parse(int id) {
      return BaseObject.ParseId<MessageQueue>(id);
    }


    static public MessageQueue Parse(string queueUID) {
      return BaseObject.ParseKey<MessageQueue>(queueUID);
    }


    static internal MessageQueue Empty {
      get {
        return BaseObject.ParseEmpty<MessageQueue>();
      }
    }


    protected override void OnLoadObjectData(System.Data.DataRow row) {
      messages = new Lazy<List<Message>>(() => MessageQueueData.GetQueueMessages(this));
    }


    #endregion Constructor and parsers

    #region Read properties and methods


    public FixedList<Message> Messages {
      get {
        return messages.Value.ToFixedList();
      }
    }


    public FixedList<Message> GetNextMessages() {
      var messages = this.Messages.FindAll(x => x.IsInProcessStatus);

      messages.Sort((x, y) => x.PostingTime.CompareTo(y.PostingTime));

      return messages;
    }


    public FixedList<Message> GetUnitOfWorkMessages(string unitOfWorkUID) {
      Assertion.AssertObject(unitOfWorkUID, "unitOfWorkUID");

      return this.Messages.FindAll(x => x.UnitOfWorkUID == unitOfWorkUID &&
                                        x.IsInProcessStatus);
    }


    public Message TryGetNextMessage() {
      return this.Messages.Find(x => x.IsInProcessStatus);
    }


    public string TryGetNextUnitOfWork() {
      var message = this.Messages.Find(x => x.UnitOfWorkUID.Length != 0 &&
                                            x.IsInProcessStatus);

      if (message != null) {
        return message.UnitOfWorkUID;
      } else {
        return null;
      }
{'fraction_non_alphanumeric': 0.10709969788519637, 'fraction_numerical': 0.00030211480362537764, 'mean_word_length': 1.7101923864101514, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3499804', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1445, 'n_words': 383}