// GET: api/DOJSubmit public async Task <EntityState> GetStops(Submissions submission) { JsonResultModel jsonResult = new JsonResultModel(); DataSet dsStop = new DataSet(); DojResultModel dojRes = new DojResultModel(); ExtractJNode eJson; string logFilename; DOJWebApiUrl = ConfigurationManager.AppSettings["DOJWebApiUrl"]; try { entitiesdb.Submissions.Add(submission); entitiesdb.SaveChanges(); int submissionID = submission.ID; logFilename = submissionID + "-" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt"; submission.LogFile = logFilename; submission.TotalHTTPErrors = jsonResult.httpErrCount; // Writing logs on both servers LogFilePath1 = ConfigurationManager.AppSettings["LogFilePath1"]; server2 = ConfigurationManager.AppSettings["Server2"]; server1 = ConfigurationManager.AppSettings["Server1"]; if (server2 == "") { logFile1 = LogFilePath1 + logFilename; } else { logFile1 = LogFilePath1 + logFilename; if (server1.IndexOf(Environment.MachineName) == -1) { LogFilePath2 = server1 + "RIPALogs\\"; logFile2 = LogFilePath2 + logFilename; } if (server2.IndexOf(Environment.MachineName) == -1) { LogFilePath2 = server2 + "RIPALogs\\"; logFile2 = LogFilePath2 + logFilename; } } if (!Directory.Exists(LogFilePath1)) { Directory.CreateDirectory(LogFilePath1); } if (server2 != "") { if (!Directory.Exists(LogFilePath2)) { Directory.CreateDirectory(LogFilePath2); } } if (!File.Exists(logFile1)) { using (FileStream fs = File.Create(logFile1)) { using (StreamWriter sw = new StreamWriter(fs)) { SQLDBDataAccessorClass sql = new SQLDBDataAccessorClass(); string sqlStr = ""; sqlStr = "Select ID from StopOfficerIDDateTime_JSON_vw as J " + " Where Convert(date,'" + submission.StartDate + "') <= J.stopDate and J.stopDate <= convert(date,'" + submission.EndDate + "')"; dsStop = sql.mds_ExecuteQuery(sqlStr, "StopOfficeVWTbl"); string DOJjson = ""; //foreach (int stopId in stopsIDs) int stopId; if (dsStop != null) { for (int i = 0; i < dsStop.Tables["StopOfficeVWTbl"].Rows.Count; i++) { stopId = Convert.ToInt32(dsStop.Tables["StopOfficeVWTbl"].Rows[i]["ID"]); Stop st = db.Stop .Where(x => x.ID == stopId && x.Status != "success" && x.Status != "postSubRedact") .Select(x => x).FirstOrDefault(); JObject submissionO; JArray subInfoArry; bool edited = false; if (st != null) { // If JsonSubmissions is not null, check the last SubmissionInfo to see if this Stop has been edited if (st.JsonSubmissions != null) { submissionO = JObject.Parse(st.JsonSubmissions); JObject lastSubmission = (JObject)submissionO["SubmissionInfo"].Last(); edited = (bool)lastSubmission["edited"]; } if (st.JsonSubmissions == null || edited) { jsonResult.processedCount++; submission.TotalProcessed = jsonResult.processedCount; DOJjson = st.JsonDojStop; HTTP_PUT(Url: DOJWebApiUrl, Data: DOJjson, model: jsonResult, sw: sw); // the next lines are for testing //jsonResult.Results = "{ \"MandatoryValidationFlag\": false, \"Status\": \"failed\", \"BatchID\": \"\", \"LEARecordID\": \"63\", \"ORI\": \"CA0370000\", \"OfficerUID\": \"11\", \"Proxy\": \"\", \"StopDate\": \"06 / 05 / 2018\", \"StopTime\": \"14:25:51\", \"Messages\": [ { \"Code\": \"DV007\", \"Field\": \"UID\", \"Message\": \"Officer Unique ID is invalid or missing, it must be 9 alphanumerical characters.\", \"PersonNumber\": null } ]}"; //jsonResult.Results = "{\"MandatoryValidationFlag\":false,\"Status\":\"failed\",\"BatchID\":\"\",\"LEARecordID\":\"14140\",\"ORI\":\"CA0370000\",\"OfficerUID\":\"111106405\",\"Proxy\":\"\",\"StopDate\":\"06 / 05 / 2018\",\"StopTime\":\"23:58:56\",\"Messages\":[{\"Code\":\"DV236\",\"Field\":\"Tx_type\",\"Message\":\"Duplicate record exists with the same ORI, LEA record ID combination.\",\"PersonNumber\":null},{\"Code\":\"DV004\",\"Field\":\"sTime\",\"Message\":\"Duplicate record; a stop exists for this ORI, Officer UID, Date & Time.\",\"PersonNumber\":null}]}"; //jsonResult.IsSuccess = true; if (jsonResult.IsSuccess) { dojRes.ReturnPayload = jsonResult.Results; st.SubmissionsID = submissionID; if (st.JsonSubmissions == null) { submissionO = JObject.FromObject(new { SubmissionInfo = new JArray() }); } else { submissionO = JObject.Parse(st.JsonSubmissions); } subInfoArry = (JArray)submissionO["SubmissionInfo"]; subInfoArry.Add(new JObject(new JProperty("submissionID", submissionID), new JProperty("edited", false))); st.JsonSubmissions = JsonConvert.SerializeObject(submissionO); JObject o = JObject.Parse(dojRes.ReturnPayload); o.Add("CustomProperty_DOJReceiptTime", DateTime.Now.ToString()); eJson = new ExtractJNode("MandatoryValidationFlag", o); dojRes.VFlag = eJson.traverseNode(); eJson = new ExtractJNode("Status", o); dojRes.Status = eJson.traverseNode(); if (dojRes.VFlag == "True") { if (dojRes.Status == "failed") { st.Status = "fail"; jsonResult.failedCount++; submission.TotalWithErrors = jsonResult.failedCount; } else { st.Status = "success"; jsonResult.succeededCount++; submission.TotalSuccess = jsonResult.succeededCount; } } else { if (dojRes.Status == "failed") { st.Status = "fatal"; jsonResult.fatalCount++; submission.TotalRejected = jsonResult.fatalCount; } } st.StatusMessage = o.ToString(); // The following section of code, saving messages in dojRes is not used at this point if (st.Status != "success") { eJson = new ExtractJNode("Messages.Message", o); string messages = eJson.traverseNode(); messages = messages.Replace(".,", "~"); dojRes.SetMessages(messages.Split('~')); } } else { submission.TotalHTTPErrors = jsonResult.httpErrCount + 1; } await db.SaveChangesAsync(); entitiesdb.Entry(submission).State = EntityState.Modified; await entitiesdb.SaveChangesAsync(); } } } } else { jsonResult.Log += "No Stop Records Found." + Environment.NewLine; } jsonResult.Log += "Records processed = " + jsonResult.processedCount + Environment.NewLine + "Records successfully submitted = " + jsonResult.succeededCount + Environment.NewLine + "Records with errors = " + jsonResult.failedCount + Environment.NewLine + "Rejected records = " + jsonResult.fatalCount + Environment.NewLine + "HTTP error count = " + jsonResult.httpErrCount + Environment.NewLine; //File.AppendAllText(logFile1, jsonResult.Log); sw.WriteLine(jsonResult.Log); } } } if (server2 != "") { File.Copy(logFile1, logFile2, true); } var state = entitiesdb.Entry(submission).State; return(state); } catch (Exception error) { submission.Status = "Resubmit"; submission.TotalHTTPErrors = jsonResult.httpErrCount + 1; entitiesdb.Entry(submission).State = EntityState.Modified; entitiesdb.SaveChanges(); string err = error.Message; if (error.InnerException != null) { err += Environment.NewLine + error.InnerException.Message; } jsonResult.Log += err + Environment.NewLine; jsonResult.Log += "Records processed = " + jsonResult.processedCount + Environment.NewLine + "Records successfully submitted = " + jsonResult.succeededCount + Environment.NewLine + "Records with errors = " + jsonResult.failedCount + Environment.NewLine + "Rejected records = " + jsonResult.fatalCount + Environment.NewLine + "HTTP error count = " + jsonResult.httpErrCount + Environment.NewLine; File.AppendAllText(logFile1, jsonResult.Log); throw error; } //return Ok(db.Stop.Where(x => startDate <= x.Time && x.Time <= endDate && (x.Status == "fail" || x.Status == "fatal")).ToList()); }
// GET: api/DOJSubmit public async Task <EntityState> GetStops(Submissions submission) { JsonResultModel jsonResult = new JsonResultModel(); DojResultModel dojRes = new DojResultModel(); ExtractJNode eJson; string logFilename; try { entitiesdb.Submissions.Add(submission); entitiesdb.SaveChanges(); int submissionID = submission.ID; logFilename = submissionID + "-" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt"; submission.LogFile = logFilename; // Writing logs on both servers LogFilePath1 = ConfigurationManager.AppSettings["LogFilePath1"]; LogFilePath2 = ConfigurationManager.AppSettings["LogFilePath2"]; if (LogFilePath2 == "") { logFile1 = LogFilePath1 + logFilename; } else { logFile1 = LogFilePath1 + logFilename; logFile2 = LogFilePath2 + logFilename; } if (!Directory.Exists(LogFilePath1)) { Directory.CreateDirectory(LogFilePath1); } if (LogFilePath2 != "") { if (!Directory.Exists(LogFilePath2)) { Directory.CreateDirectory(LogFilePath2); } } var stopsIDs = entitiesdb.StopOfficerIDDateTime_JSON_vw.ToList() .Where(x => submission.StartDate <= Convert.ToDateTime(x.stopDate) && Convert.ToDateTime(x.stopDate) <= submission.EndDate) //.Where(x => submission.StartDate <= Convert.ToDateTime(x.stopDate) && Convert.ToDateTime(x.stopDate) <= submission.EndDate && x.ID >= 15187 ) .Select(x => x.ID); string DOJjson = ""; foreach (int stopId in stopsIDs) { Stop st = db.Stop .Where(x => x.ID == stopId && x.Status.Trim() != "success" && x.Status.Trim() != "postSubRedact") .Select(x => x).FirstOrDefault(); if (st != null) { jsonResult.processedCount++; submission.TotalProcessed = jsonResult.processedCount; DOJjson = st.JsonDojStop; HTTP_PUT(Url: DOJWebApiUrl, Data: DOJjson, model: jsonResult); // the next lines are for testing //jsonResult.Results = "{ \"MandatoryValidationFlag\": false, \"Status\": \"failed\", \"BatchID\": \"\", \"LEARecordID\": \"63\", \"ORI\": \"CA0370000\", \"OfficerUID\": \"11\", \"Proxy\": \"\", \"StopDate\": \"06 / 05 / 2018\", \"StopTime\": \"14:25:51\", \"Messages\": [ { \"Code\": \"DV007\", \"Field\": \"UID\", \"Message\": \"Officer Unique ID is invalid or missing, it must be 9 alphanumerical characters.\", \"PersonNumber\": null } ]}"; //jsonResult.Results = "{\"MandatoryValidationFlag\":false,\"Status\":\"failed\",\"BatchID\":\"\",\"LEARecordID\":\"14140\",\"ORI\":\"CA0370000\",\"OfficerUID\":\"111106405\",\"Proxy\":\"\",\"StopDate\":\"06 / 05 / 2018\",\"StopTime\":\"23:58:56\",\"Messages\":[{\"Code\":\"DV236\",\"Field\":\"Tx_type\",\"Message\":\"Duplicate record exists with the same ORI, LEA record ID combination.\",\"PersonNumber\":null},{\"Code\":\"DV004\",\"Field\":\"sTime\",\"Message\":\"Duplicate record; a stop exists for this ORI, Officer UID, Date & Time.\",\"PersonNumber\":null}]}"; //jsonResult.IsSuccess = true; if (jsonResult.IsSuccess) { dojRes.ReturnPayload = jsonResult.Results; st.SubmissionsID = submissionID; JObject submissionO; JArray subInfoArry; if (st.JsonSubmissions == null) { submissionO = JObject.FromObject(new { SubmissionInfo = new JArray() }); } else { submissionO = JObject.Parse(st.JsonSubmissions); } subInfoArry = (JArray)submissionO["SubmissionInfo"]; subInfoArry.Add(new JObject(new JProperty("submissionID", submissionID), new JProperty("edited", false))); st.JsonSubmissions = JsonConvert.SerializeObject(submissionO); JObject o = JObject.Parse(dojRes.ReturnPayload); o.Add("CustomProperty_DOJReceiptTime", DateTime.Now.ToString()); eJson = new ExtractJNode("MandatoryValidationFlag", o); dojRes.VFlag = eJson.traverseNode(); eJson = new ExtractJNode("Status", o); dojRes.Status = eJson.traverseNode(); if (dojRes.VFlag == "True") { if (dojRes.Status == "failed") { st.Status = "fail"; jsonResult.failedCount++; submission.TotalWithErrors = jsonResult.failedCount; } else { st.Status = "success"; jsonResult.succeededCount++; submission.TotalSuccess = jsonResult.succeededCount; } } else { if (dojRes.Status == "failed") { st.Status = "fatal"; jsonResult.fatalCount++; submission.TotalRejected = jsonResult.fatalCount; } } st.StatusMessage = o.ToString(); // The following section of code, saving messages in dojRes is not used at this point if (st.Status != "success") { eJson = new ExtractJNode("Messages.Message", o); string messages = eJson.traverseNode(); messages = messages.Replace(".,", "~"); dojRes.SetMessages(messages.Split('~')); } } else { submission.TotalHTTPErrors = jsonResult.httpErrCount; } await db.SaveChangesAsync(); entitiesdb.Entry(submission).State = EntityState.Modified; await entitiesdb.SaveChangesAsync(); } } jsonResult.Log += "Records processed = " + jsonResult.processedCount + "\r\n" + "Records successfully submitted = " + jsonResult.succeededCount + "\r\n" + "Records with errors = " + jsonResult.failedCount + "\r\n" + "Rejected records = " + jsonResult.fatalCount + "\r\n" + "HTTP error count = " + jsonResult.httpErrCount + "\r\n"; File.WriteAllText(logFile1, jsonResult.Log); if (LogFilePath2 != "") { File.WriteAllText(logFile2, jsonResult.Log); } submission.LogFile = logFilename; var state = entitiesdb.Entry(submission).State; return(state); } catch (Exception error) { string err = error.Message; throw error; } //return Ok(db.Stop.Where(x => startDate <= x.Time && x.Time <= endDate && (x.Status == "fail" || x.Status == "fatal")).ToList()); }