public BatchDocumentCommandRoutingResponse GetNextBatcDocumentCommand(Guid costCentreApplicationId, List<long> lastDeliveredCommandRouteItemIds, int batchSize)
        {

            if (lastDeliveredCommandRouteItemIds!=null && lastDeliveredCommandRouteItemIds.Count>0)
                _commandRoutingOnRequestRepository.MarkBatchAsDelivered(lastDeliveredCommandRouteItemIds, costCentreApplicationId);

            List<CommandRouteOnRequest> criList = _commandRoutingOnRequestRepository
                .GetUnexecutedBatchByDestinationCostCentreApplicationId(costCentreApplicationId,batchSize);
            BatchDocumentCommandRoutingResponse responce = new BatchDocumentCommandRoutingResponse();
            if (criList == null || criList.Count()==0)
            {
                responce.RoutingCommands = null;
                responce.CommandRoutingCount = 0;
                responce.ErrorInfo = "No Pending Download";
                responce.LastCommandRouteItemId = 0;
            }
            foreach (CommandRouteOnRequest cri in criList)
            {
                CommandType ct = (CommandType) Enum.Parse(typeof (CommandType), cri.CommandType);

                DocumentCommandRoutingResponse commandResponse=new DocumentCommandRoutingResponse
                           {
                               CommandRouteItemId = cri.Id,
                               CommandType = cri.CommandType,
                               ErrorInfo = "",
                               Command = JsonConvert.DeserializeObject<DocumentCommand>(cri.JsonCommand) 
                           };

                responce.RoutingCommands.Add(commandResponse);
                responce.LastCommandRouteItemId = cri.Id;
            }
            responce.CommandRoutingCount = criList.Count;
            responce.ErrorInfo = "Success";
            return responce;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="costCentreApplicationId"></param>
 /// <param name="lastDeliveredCommandRouteItemId">-1 if never requested before</param>
 /// <returns></returns>
 public ActionResult GetNextDocumentCommand(Guid costCentreApplicationId, long lastDeliveredCommandRouteItemId)
 {
     _log.InfoFormat("GetNextDocumentCommand from CCAPPId : {0} with lastDeliveredCommandRouteItemId : {1}", costCentreApplicationId, lastDeliveredCommandRouteItemId);
     Guid ccid = _costCentreApplicationService.GetCostCentreFromCostCentreApplicationId(costCentreApplicationId);
     
     DocumentCommandRoutingResponse response = null;
     try
     {
         if (_costCentreApplicationService.IsCostCentreActive(costCentreApplicationId))
             response = _commandRouterResponseBuilder.GetNextDocumentCommand(costCentreApplicationId, ccid, lastDeliveredCommandRouteItemId);
         else
             response = new DocumentCommandRoutingResponse { ErrorInfo = "Inactive CostCentre Application Id" };
     }
     catch (Exception ex )
     {
         response = new DocumentCommandRoutingResponse { ErrorInfo = "Failed to get next document command" };
         _log.InfoFormat("ERROR GetNextDocumentCommand  from CCAPPId : {0} with lastDeliveredCommandRouteItemId : {1}", costCentreApplicationId, lastDeliveredCommandRouteItemId);
         _log.Error(ex);
     }
     ContentResult result = new ContentResult
                                {
                                    Content = JsonConvert.SerializeObject(response),
                                    ContentType = "application/json",
                                    ContentEncoding = Encoding.UTF8
                                };
     AuditCCHit(ccid, "GetNextDocumentCommand", result.Content, "OK" );
     return result;
 }
        public HttpResponseMessage GetNextDocumentCommand(Guid costCentreApplicationId, long lastDeliveredCommandRouteItemId)
        {
            _log.InfoFormat("GetNextDocumentCommand from CCAPPId : {0} with lastDeliveredCommandRouteItemId : {1}", costCentreApplicationId, lastDeliveredCommandRouteItemId);
            Guid ccid = _costCentreApplicationService.GetCostCentreFromCostCentreApplicationId(costCentreApplicationId);

            DocumentCommandRoutingResponse response = null;
            try
            {
                if (_costCentreApplicationService.IsCostCentreActive(costCentreApplicationId))
                    response = _commandRouterResponseBuilder.GetNextDocumentCommand(costCentreApplicationId, ccid, lastDeliveredCommandRouteItemId);
                else
                    response = new DocumentCommandRoutingResponse { ErrorInfo = "Inactive CostCentre Application Id" };
            }
            catch (Exception ex)
            {
                response = new DocumentCommandRoutingResponse { ErrorInfo = "Failed to get next document command" };
                _log.InfoFormat("ERROR GetNextDocumentCommand  from CCAPPId : {0} with lastDeliveredCommandRouteItemId : {1}", costCentreApplicationId, lastDeliveredCommandRouteItemId);
                _log.Error(ex);
            }
           
            AuditCCHit(ccid, "GetNextDocumentCommand", JsonConvert.SerializeObject(response), "OK");
            
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
 public DocumentCommandRoutingResponse DeserializeDocumentCommandRoutingResponse(string jsonString)
 {
     DocumentCommandRoutingResponse response = new DocumentCommandRoutingResponse();
     JObject jo = JObject.Parse(jsonString);
     string _commandType =(string) jo["CommandType"];
     string _commandRouteItemId = (jo["CommandRouteItemId"]).ToString(); ;
     string _commandJson =   jo["Command"].ToString();
     string _errorInfo = (string) jo["ErrorInfo"];
     response.CommandType = _commandType;
     response.CommandRouteItemId = Int32.Parse(_commandRouteItemId);
     response.ErrorInfo = _errorInfo;
     try
     {
         response.Command = _commandDeserialize.DeserializeCommand(response.CommandType, _commandJson);
     }catch
     {
         response.Command = null;
         
     }
     return response;
 }