public void RouteCommandEnvelope(CommandEnvelope envelope)
        {
            _log.InfoFormat("RouteCommandEnvelope -- DocumentId {0} --  commandid {1} -- documentid {1} --  ",
                envelope.DocumentTypeId, envelope.Id, envelope.DocumentId);
            if (_envelopeQueueRepository.GetByEnvelopeId(envelope.Id) != null)
                return;


            var queueItem = new OutGoingCommandEnvelopeQueueItemLocal()
                            {
                                DocumentId = envelope.DocumentId,
                                DocumentType = (DocumentType) envelope.DocumentTypeId,
                                DateInserted = DateTime.Now,
                              
                                IsEnvelopeSent = false,
                                DateSent = DateTime.Now,
                                JsonEnvelope = JsonConvert.SerializeObject(envelope, new IsoDateTimeConverter()),
                                EnvelopeId = envelope.Id,
                                
                            };
            _envelopeQueueRepository.Add(queueItem);
            if (!envelope.IsSystemEnvelope)
            {
                foreach (var commandEnvelopeItem in envelope.CommandsList)
                {
                    CommandType commandType = _resolveCommand.Get(commandEnvelopeItem.Command).CommandType;
                    _executeCommandLocally.ExecuteCommand(commandType, commandEnvelopeItem.Command);
                }
            }
        }
        public void Add(OutGoingCommandEnvelopeQueueItemLocal itemToAdd)
        {
            OutGoingCommandEnvelopeQueueItemLocal existing = _ctx.OutGoingCommandEnvelopeQueueItems.FirstOrDefault(p => p.Id == itemToAdd.Id);
            if (existing == null)
            {
                existing = new OutGoingCommandEnvelopeQueueItemLocal();
                _ctx.OutGoingCommandEnvelopeQueueItems.Add(existing);
            }
            existing.DocumentId = itemToAdd.DocumentId;
            existing.DocumentType = itemToAdd.DocumentType;
            existing.DateInserted = itemToAdd.DateInserted;
            existing.DateSent = itemToAdd.DateSent;
            existing.DocumentId = itemToAdd.DocumentId;
            existing.IsEnvelopeSent = itemToAdd.IsEnvelopeSent;
            existing.JsonEnvelope = itemToAdd.JsonEnvelope;
            existing.EnvelopeId = itemToAdd.EnvelopeId;

            _ctx.SaveChanges();
        }