private void AddToMongoDB(ICommand createCommand)
 {
     var commandProcessingAudit = new CommandProcessingAudit
     {
         CommandType = createCommand.CommandTypeRef,
         CostCentreApplicationId =
             createCommand.CommandGeneratedByCostCentreApplicationId,
         CostCentreCommandSequence = createCommand.CommandSequence,
         DateInserted = DateTime.Now,
         Id = createCommand.CommandId,
         JsonCommand = JsonConvert.SerializeObject(createCommand),
         RetryCounter = 0,
         Status = CommandProcessingStatus.OnQueue,
         SendDateTime = DateTime.Now.ToShortDateString(),
         DocumentId = createCommand.DocumentId,
         ParentDocumentId = createCommand.PDCommandId
     };
     _commandProcessingAuditRepository.AddCommand(commandProcessingAudit);
 }
        public HttpResponseMessage Run(JObject jcommand)
        {

            Guid commandId = Guid.Empty;
            var responseBasic = new ResponseBasic();
            HttpStatusCode returnCode = HttpStatusCode.OK;
            try
            {
                responseBasic.ResultInfo = "invalid jsoncommand";
                DocumentCommand command = JsonConvert.DeserializeObject<DocumentCommand>(jcommand.ToString());
                responseBasic.ResultInfo = "valid jsoncommand";
                _log.InfoFormat("Received command id {0} : command type {1} : document id {2}", command.CommandId, command.CommandTypeRef, command.DocumentId);
                bool isValid = command != null;

                commandId = command.CommandId;
                //if (isValid && !_costCentreApplicationService.IsCostCentreActive(command.CommandGeneratedByCostCentreApplicationId))
                //{
                //    isValid = false;
                //    returnCode = HttpStatusCode.NotAcceptable;
                //    responseBasic.Result = "Inactive CostCentre Application Id ";
                //    _log.InfoFormat("Cost centre is not active for command id {0} cost centre application id {1}", command.CommandId, command.CommandGeneratedByCostCentreApplicationId);
                //}
                if (isValid)
                {
                    _log.InfoFormat("CommandId {0} Placed on bus", command.CommandId);
                    var message = new BusMessage
                    {
                        CommandType = command.CommandTypeRef,
                        MessageId = command.CommandId,
                        BodyJson = JsonConvert.SerializeObject(command),
                        SendDateTime = DateTime.Now.ToString(),
                        IsSystemMessage = command.IsSystemCommand
                    };
                    var commandProcessingAudit = new CommandProcessingAudit
                    {
                        CommandType = command.CommandTypeRef,
                        CostCentreApplicationId = command.CommandGeneratedByCostCentreApplicationId,
                        CostCentreCommandSequence = command.CommandSequence,
                        DateInserted = DateTime.Now,
                        Id = command.CommandId,
                        JsonCommand = message.BodyJson,
                        RetryCounter = 0,
                        Status = CommandProcessingStatus.OnQueue,
                        SendDateTime = message.SendDateTime,
                        DocumentId = command.DocumentId,
                        ParentDocumentId = command.PDCommandId
                    };

                    _busPublisher.Publish(message);
                    AuditCCHit(command.CommandGeneratedByCostCentreId, "CommandProcessing",
                               string.Format("Publish : {0} Command {1} of Type {2}",
                                             CommandProcessingStatus.OnQueue.ToString(), command.CommandId.ToString(),
                                             message.CommandType), CommandProcessingStatus.OnQueue.ToString());
                    _commandProcessingAuditRepository.AddCommand(commandProcessingAudit);
                    responseBasic.Result = "Command Processed";


                }
            }
            catch (Exception ex)
            {
                responseBasic.Result = "Processing Failed";
                responseBasic.ErrorInfo = ex.Message;
                _log.Error("Failed to process command", ex);
            }
            HttpResponseMessage response = Request.CreateResponse(returnCode, responseBasic);
            _log.InfoFormat("ResponseMessage : commandId = {0}  : response code = {1}  : Response Result = {2}", commandId, returnCode, responseBasic.Result);
            return response;
        }
        private ResponseBasic ProcessMessage(string commandType, string jsoncommand, string sendDateTime, string source)
        {
            string result = "";
            string errorInfo = "";
            Guid ccId = Guid.Empty;
            _log.InfoFormat("Processing command : {0} with {1} from source {2} ", commandType, jsoncommand, source);
            try
            {
                DateTime _sendDateTime = _commandDeserialize.DeserializeSendDateTime(sendDateTime);
                ICommand deserializedCommand = _commandDeserialize.DeserializeCommand(commandType, jsoncommand);

                bool isValid = deserializedCommand != null;
                result = isValid ? "OK" : "Invalid";
                _log.InfoFormat("Deserialization is valid " + result);
                errorInfo = isValid ? "" : "Failed to deserialize " + commandType;

                if (isValid && !_costCentreApplicationService.IsCostCentreActive(deserializedCommand.CommandGeneratedByCostCentreApplicationId))
                {
                    isValid = false;
                    result = "Invalid";
                    errorInfo = "Inactive CostCentre Application Id ";
                }
                if (isValid && !QHealth.IsQueueHealthy)
                {
                    isValid = false;
                    result = "Invalid";
                    errorInfo = "Message Q not available";
                }

                if (isValid)
                {
                    ccId = deserializedCommand.CommandGeneratedByCostCentreId;
                    deserializedCommand.SendDateTime = _sendDateTime;
                    _log.Info("Client SendDateTime " + sendDateTime);
                    _log.InfoFormat("CommandId {0} Placed on bus", deserializedCommand.CommandId);
                    var message = new BusMessage
                    {
                        CommandType = commandType,
                        MessageId = deserializedCommand.CommandId,
                        BodyJson = jsoncommand,
                        SendDateTime = sendDateTime
                    };
                    var commandProcessingAudit = new CommandProcessingAudit
                                                                        {
                                                                            CommandType = commandType,
                                                                            CostCentreApplicationId = deserializedCommand.CommandGeneratedByCostCentreApplicationId,
                                                                            CostCentreCommandSequence = deserializedCommand.CommandSequence,
                                                                            DateInserted = DateTime.Now,
                                                                            Id = deserializedCommand.CommandId,
                                                                            JsonCommand = jsoncommand,
                                                                            RetryCounter = 0,
                                                                            Status = CommandProcessingStatus.OnQueue,
                                                                            SendDateTime=sendDateTime,
                                                                            DocumentId = deserializedCommand.DocumentId,
                                                                            ParentDocumentId=deserializedCommand.PDCommandId
                                                                        };

                   
                    _busPublisher.Publish(message);
                    AuditCCHit(deserializedCommand.CommandGeneratedByCostCentreId, "CommandProcessing", string.Format("Publish : {0} Command {1} of Type {2}", CommandProcessingStatus.OnQueue.ToString(), deserializedCommand.CommandId.ToString(), message.CommandType), CommandProcessingStatus.OnQueue.ToString());
                   _commandProcessingAuditRepository.AddCommand(commandProcessingAudit);
                }
                else
                    _log.Error(errorInfo);
            }
            catch (Exception ex)
            {
                result = "Invalid";
                _log.InfoFormat("ERROR Processing command : {0}", commandType);
                _log.Error(ex);
            }

            ResponseBasic responce = new ResponseBasic { Result = result, ErrorInfo = errorInfo };
            _log.Info("Final responce " + JsonConvert.SerializeObject(responce));
            AuditCCHit(ccId, "slprocess", "CommandType : " + commandType, result);
            return responce;
        }
        public void AddCommand(CommandProcessingAudit commandProcessingAudit)
        {
            CommandProcessingAuditTable commandProcessingAuditTable = commandProcessingAudit.Map();
            var tasks = new List<Task<TableResult>>();

            tasks.Add(Task.Run(() =>
            {
                TableOperation tableOperation1 = TableOperation.Insert(commandProcessingAuditTable);
                return _commandTable.Execute(tableOperation1);
            }));

            //commandindex
            tasks.Add(Task.Run(() =>
                {

                    var commandIndex = new CommandProcessingAuditCommandIndexTable
                    {
                        PartitionKey = "commandlu",
                        CostCentreId = new Guid(commandProcessingAuditTable.PartitionKey),
                        RowKey = commandProcessingAudit.Id.ToString()
                    };
                    TableOperation to1 = TableOperation.Insert(commandIndex);
                    TableResult r1 = _commandIndexTable.Execute(to1);
                    return r1;
                }));

            //documentindex
            tasks.Add(Task.Run(() =>
                {

                    var documentIndex = new CommandProcessingAuditDocumentIndexTable
                        {
                            PartitionKey = commandProcessingAudit.DocumentId.ToString(),
                            RowKey = commandProcessingAudit.Id.ToString(),
                            CostCentreId = new Guid(commandProcessingAuditTable.PartitionKey)
                        };
                    TableOperation to2 = TableOperation.Insert(documentIndex);
                    TableResult r2 = _documentIndexTable.Execute(to2);
                    return r2;
                }));

            //ccappid index
            tasks.Add(Task.Run(() =>
            {
                var ccappidindex = new CommandProcessingAuditCCApplicationIndexTable
                {
                    PartitionKey = commandProcessingAudit.CostCentreApplicationId.ToString(),
                    RowKey = commandProcessingAudit.Id.ToString(),
                    CostCentreId = new Guid(commandProcessingAuditTable.PartitionKey)
                };
                TableOperation to3 = TableOperation.Insert(ccappidindex);
                TableResult r3 = _ccAppIdIndexTable.Execute(to3);
                return r3;
            }));

            //command status
            tasks.Add(Task.Run(() =>
                {
                    return AddCommandStatusIndex(commandProcessingAuditTable.Id, CommandProcessingStatus.OnQueue,
                                                 commandProcessingAuditTable.PartitionKey);
                }));


            Task.WaitAll(tasks.ToArray());

        }
 public void AddCommand(CommandProcessingAudit commandProcessingAudit)
 {
     _commandProcessingAuditCollection.Save(commandProcessingAudit);
 }