public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); List <EDI315_Detail_R4_DTM> dbEDI315_Detail_R4_DTMList = new List <EDI315_Detail_R4_DTM>(); int convertToInt = 0; decimal convertToDecimal = 0; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = SubstringValue(rowLine[1] ?? "", 2), ISA_auth_info = SubstringValue(rowLine[2] ?? "", 10), ISA_security_info_qualifier = SubstringValue(rowLine[3] ?? "", 2), ISA_security_info = SubstringValue(rowLine[4] ?? "", 10), ISA_interchange_id_qualifier_1 = SubstringValue(rowLine[5] ?? "", 2), ISA_interchange_sender_id = SubstringValue(rowLine[6] ?? "", 15), ISA_interchange_id_qualifier_2 = SubstringValue(rowLine[7] ?? "", 2), ISA_interchange_receiver_id = SubstringValue(rowLine[8] ?? "", 15), ISA_interchage_date = SubstringValue(rowLine[9] ?? "", 8), ISA_interchange_time = SubstringValue(rowLine[10] ?? "", 4), ISA_interchange_control_standards_id = SubstringValue(rowLine[11] ?? "", 1), ISA_interchange_control_version_number = SubstringValue(rowLine[12] ?? "", 5), ISA_interchange_control_number = SubstringValue(rowLine[13] ?? "", 9), ISA_ack_requested = SubstringValue(rowLine[14] ?? "", 1), ISA_usage_indicator = SubstringValue(rowLine[15] ?? "", 1), ISA_component_element_separator = SubstringValue(rowLine[16] ?? "", 1), }; #endregion break; case "GS": // insert DB #region GS dbEDI315.GS_functional_id_code = SubstringValue(rowLine[1] ?? "", 2); dbEDI315.GS_app_sender_code = SubstringValue(rowLine[2] ?? "", 15); dbEDI315.GS_app_receiver_code = SubstringValue(rowLine[3] ?? "", 15); dbEDI315.GS_date = SubstringValue(rowLine[4] ?? "", 8); dbEDI315.GS_time = SubstringValue(rowLine[5] ?? "", 8); dbEDI315.GS_group_control_number = SubstringValue(rowLine[6] ?? "", 9); dbEDI315.GS_reponsible_agency_code = SubstringValue(rowLine[7] ?? "", 2); dbEDI315.GS_industry_id_code = SubstringValue(rowLine[8] ?? "", 12); /* Insert table EDI315 after done with ISA & GS */ if (dbEDI315.msg_idnum != 0) { if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); context.Dispose(); } } else { string logMsg = "Function: MeesageParsing - Table EDI315\r\n"; logMsg += "Error Message: Not able to access DB. Process rollbacked.\r\n"; util.insertLog("315", msg_idnum, dbEDI315.EDI315_idnum, 0, logMsg); rollbackProcess(msg_idnum); return(false); } } #endregion break; case "ST": #region ST /* Detail start. Init. */ dbEDI315_Detail = new EDI315_Detail(); dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); dbEDI315_Detail_R4_DTMList = new List <EDI315_Detail_R4_DTM>(); #endregion break; case "SE": /* Detail End. Insert into Tables. */ #region SE if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) { dbEDI315_Detail.SE_included_segments_number = convertToInt; } dbEDI315_Detail.SE_transaction_set_control_number = rowLine[2]; /* insert Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4 & EDI315_Detail_R4_DTM */ if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { /* EDI315_Detail Insert */ dbEDI315_Detail.EDI315_idnum = dbEDI315.EDI315_idnum; context.EDI315_Detail.Add(dbEDI315_Detail); context.SaveChanges(); /* EDI315_Detail_N9 Insert */ dbEDI315_Detail_N9List.ForEach(x => x.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum); context.EDI315_Detail_N9.AddRange(dbEDI315_Detail_N9List); context.SaveChanges(); /* EDI315_Detail_R4 & EDI315_Detail_R4_DTM Insert */ foreach (EDI315_Detail_R4 dbR4Row in dbEDI315_Detail_R4List) { List <EDI315_Detail_R4_DTM> dtmList = dbEDI315_Detail_R4_DTMList.Where(x => x.Detail_R4_idnum == dbR4Row.Detail_R4_idnum).ToList(); dbR4Row.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; context.EDI315_Detail_R4.Add(dbR4Row); context.SaveChanges(); foreach (EDI315_Detail_R4_DTM dtmRow in dtmList) { dtmRow.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; dtmRow.Detail_R4_idnum = dbR4Row.Detail_R4_idnum; } context.EDI315_Detail_R4_DTM.AddRange(dtmList); context.SaveChanges(); } context.Dispose(); } } else { string logMsg = "Function: MeesageParsing - Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4, EDI315_Detail_R4_DTM\r\n"; logMsg += "Error Message: Not able to access DB. Process rollbacked.\r\n"; util.insertLog("315", msg_idnum, dbEDI315.EDI315_idnum, dbEDI315_Detail.EDI315_Detail_idnum, logMsg); rollbackProcess(msg_idnum); return(false); } #endregion break; case "B4": #region B4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.B4_SHC = SubstringValue(rowLine[1], 3); convertToInt = 0; if (rowLine[2] != null && rowLine[2].Trim() != string.Empty && Int32.TryParse(rowLine[2], out convertToInt)) { dbEDI315_Detail.B4_request_number = convertToInt; } dbEDI315_Detail.B4_status_code = SubstringValue(rowLine[3], 2); dbEDI315_Detail.B4_date = SubstringValue(rowLine[4] ?? "", 8); dbEDI315_Detail.B4_status_time = SubstringValue(rowLine[5] ?? "", 4); dbEDI315_Detail.B4_status_location = SubstringValue(rowLine[6] ?? "", 5); dbEDI315_Detail.B4_equip_initial = SubstringValue(rowLine[7], 4); dbEDI315_Detail.B4_equip_number = SubstringValue(rowLine[8], 10); dbEDI315_Detail.B4_equip_status_code = SubstringValue(rowLine[9], 2); dbEDI315_Detail.B4_equip_type = SubstringValue(rowLine[10], 4); dbEDI315_Detail.B4_location_code = SubstringValue(rowLine[11], 30); dbEDI315_Detail.B4_location_id = SubstringValue(rowLine[12], 2); if (rowLine.Count() > 13) { convertToInt = 0; if (rowLine[13] != null && rowLine[13].Trim() != string.Empty && Int32.TryParse(rowLine[13], out convertToInt)) { dbEDI315_Detail.B4_equip_check_digit = convertToInt; } } #endregion break; case "Q2": #region Q2 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.Q2_vessel_code = SubstringValue(rowLine[1], 8); dbEDI315_Detail.Q2_country_code = SubstringValue(rowLine[2], 3); dbEDI315_Detail.Q2_date_1 = SubstringValue(rowLine[3], 8); dbEDI315_Detail.Q2_date_2 = SubstringValue(rowLine[4], 8); dbEDI315_Detail.Q2_date_3 = SubstringValue(rowLine[5], 8); convertToInt = 0; if (rowLine[6] != null && rowLine[6].Trim() != string.Empty && Int32.TryParse(rowLine[6], out convertToInt)) { dbEDI315_Detail.Q2_lading_quantity = convertToInt; } convertToDecimal = 0; if (rowLine[7] != null && rowLine[7].Trim() != string.Empty && Decimal.TryParse(rowLine[7], out convertToDecimal)) { dbEDI315_Detail.Q2_weight = convertToDecimal; } dbEDI315_Detail.Q2_weight_qualifier = SubstringValue(rowLine[8], 2); dbEDI315_Detail.Q2_voyage_number = SubstringValue(rowLine[9], 10); dbEDI315_Detail.Q2_reference_id_qualifier = SubstringValue(rowLine[10], 3); dbEDI315_Detail.Q2_reference_id = SubstringValue(rowLine[11], 30); dbEDI315_Detail.Q2_vessel_code_qualifier = SubstringValue(rowLine[12], 1); dbEDI315_Detail.Q2_vessel_name = SubstringValue(rowLine[13], 28); convertToDecimal = 0; if (rowLine[14] != null && rowLine[14].Trim() != string.Empty && Decimal.TryParse(rowLine[14], out convertToDecimal)) { dbEDI315_Detail.Q2_volume = convertToDecimal; } dbEDI315_Detail.Q2_volume_unit_qualifier = SubstringValue(rowLine[15], 1); dbEDI315_Detail.Q2_weight_unit_code = SubstringValue(rowLine[16], 1); #endregion break; case "N9": #region N9 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_N9 dbTemp = new EDI315_Detail_N9(); dbTemp.reference_id_qualifier = SubstringValue(rowLine[1] ?? "", 3); if (rowLine.Count() > 2) { dbTemp.reference_id = SubstringValue(rowLine[2], 30); } if (rowLine.Count() > 3) { dbTemp.free_form_description = SubstringValue(rowLine[3], 45); } if (rowLine.Count() > 4) { dbTemp.date = SubstringValue(rowLine[4], 8); } if (rowLine.Count() > 5) { dbTemp.time = SubstringValue(rowLine[5], 8); } if (rowLine.Count() > 6) { dbTemp.time_code = SubstringValue(rowLine[6], 2); } dbEDI315_Detail_N9List.Add(dbTemp); } #endregion break; case "R4": #region R4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_R4 dbTemp = new EDI315_Detail_R4(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count() + 1; dbTemp.port_function_code = SubstringValue(rowLine[1] ?? "", 1); if (rowLine.Count() > 2) { dbTemp.location_qualifier = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.location_id = rowLine[3]; } if (rowLine.Count() > 4) { dbTemp.port_name = rowLine[4]; } if (rowLine.Count() > 5) { dbTemp.country_code = rowLine[5]; } if (rowLine.Count() > 6) { dbTemp.terminal_name = rowLine[6]; } if (rowLine.Count() > 7) { dbTemp.pier_number = rowLine[7]; } if (rowLine.Count() > 8) { dbTemp.province_code = rowLine[8]; } dbEDI315_Detail_R4List.Add(dbTemp); } #endregion break; case "DTM": #region DTM if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_R4_DTM dbTemp = new EDI315_Detail_R4_DTM(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count(); convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) { dbTemp.datetime_qualifier = convertToInt; } if (rowLine.Count() > 2) { dbTemp.date = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.time = rowLine[3]; } if (rowLine.Count() > 4) { dbTemp.time_code = rowLine[4]; } dbEDI315_Detail_R4_DTMList.Add(dbTemp); } #endregion break; } } } result = true; } catch (DbEntityValidationException ex) { string logMsg = "Function: MeesageParsing\r\n"; logMsg += "Process rollbacked.\r\n"; logMsg += "Error Message:\r\n"; foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; foreach (DbValidationError subItem in item.ValidationErrors) { logMsg += string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName) + "\r\n"; } } util.insertLog("315", msg_idnum, 0, 0, logMsg); rollbackProcess(msg_idnum); result = false; } catch (Exception ex) { string logMsg = "Function: MeesageParsing\r\n"; logMsg += "Process rollbacked.\r\n"; logMsg += "Error Message: " + ex.ToString(); util.insertLog("315", msg_idnum, 0, 0, logMsg); rollbackProcess(msg_idnum); result = false; } return(result); }
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List<EDI315_Detail_N9> dbEDI315_Detail_N9List = new List<EDI315_Detail_N9>(); List<EDI315_Detail_R4> dbEDI315_Detail_R4List = new List<EDI315_Detail_R4>(); List<EDI315_Detail_R4_DTM> dbEDI315_Detail_R4_DTMList = new List<EDI315_Detail_R4_DTM>(); int convertToInt = 0; decimal convertToDecimal = 0; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) header = rowLine[0]; switch (header) { case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = rowLine[1] ?? "", ISA_auth_info = rowLine[2] ?? "", ISA_security_info_qualifier = rowLine[3] ?? "", ISA_security_info = rowLine[4] ?? "", ISA_interchange_id_qualifier_1 = rowLine[5] ?? "", ISA_interchange_sender_id = rowLine[6] ?? "", ISA_interchange_id_qualifier_2 = rowLine[7] ?? "", ISA_interchange_receiver_id = rowLine[8] ?? "", ISA_interchage_date = rowLine[9] ?? "", ISA_interchange_time = rowLine[10] ?? "", ISA_interchange_control_standards_id = rowLine[11] ?? "", ISA_interchange_control_version_number = rowLine[12] ?? "", ISA_interchange_control_number = rowLine[13] ?? "", ISA_ack_requested = rowLine[14] ?? "", ISA_usage_indicator = rowLine[15] ?? "", ISA_component_element_separator = rowLine[16] ?? "", }; #endregion break; case "GS": // insert DB #region GS dbEDI315.GS_functional_id_code = rowLine[1] ?? ""; dbEDI315.GS_app_sender_code = rowLine[2] ?? ""; dbEDI315.GS_app_receiver_code = rowLine[3] ?? ""; dbEDI315.GS_date = rowLine[4] ?? ""; dbEDI315.GS_time = rowLine[5] ?? ""; dbEDI315.GS_group_control_number = rowLine[6] ?? ""; dbEDI315.GS_reponsible_agency_code = rowLine[7] ?? ""; dbEDI315.GS_industry_id_code = rowLine[8] ?? ""; /* Insert table EDI315 after done with ISA & GS */ if (dbEDI315.msg_idnum != 0) { if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { /* check DB is open */ dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); context.Dispose(); } } else { // not able to access DB //return false; } } #endregion break; case "ST": #region ST /* Detail start. Init. */ dbEDI315_Detail = new EDI315_Detail(); dbEDI315_Detail_N9List = new List<EDI315_Detail_N9>(); dbEDI315_Detail_R4List = new List<EDI315_Detail_R4>(); dbEDI315_Detail_R4_DTMList = new List<EDI315_Detail_R4_DTM>(); #endregion break; case "SE": // inset DB #region SE if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) dbEDI315_Detail.SE_included_segments_number = convertToInt; dbEDI315_Detail.SE_transaction_set_control_number = rowLine[2]; /* insert Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4 & EDI315_Detail_R4_DTM */ if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { /* EDI315_Detail Insert */ dbEDI315_Detail.EDI315_idnum = dbEDI315.EDI315_idnum; context.EDI315_Detail.Add(dbEDI315_Detail); context.SaveChanges(); /* EDI315_Detail_N9 Insert */ dbEDI315_Detail_N9List.ForEach(x => x.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum); context.EDI315_Detail_N9.AddRange(dbEDI315_Detail_N9List); context.SaveChanges(); /* EDI315_Detail_R4 & EDI315_Detail_R4_DTM Insert */ foreach (EDI315_Detail_R4 dbR4Row in dbEDI315_Detail_R4List) { List<EDI315_Detail_R4_DTM> dtmList = dbEDI315_Detail_R4_DTMList.Where(x => x.Detail_R4_idnum == dbR4Row.Detail_R4_idnum).ToList(); context.EDI315_Detail_R4.Add(dbR4Row); context.SaveChanges(); foreach (EDI315_Detail_R4_DTM dtmRow in dtmList) { dtmRow.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; dtmRow.Detail_R4_idnum = dbR4Row.Detail_R4_idnum; } context.EDI315_Detail_R4_DTM.AddRange(dtmList); context.SaveChanges(); } } } #endregion break; case "B4": #region B4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; dbEDI315_Detail.B4_SHC = rowLine[1]; convertToInt = 0; if (rowLine[2] != null && rowLine[2].Trim() != string.Empty && Int32.TryParse(rowLine[2], out convertToInt)) dbEDI315_Detail.B4_request_number = convertToInt; dbEDI315_Detail.B4_status_code = rowLine[3]; dbEDI315_Detail.B4_date = rowLine[4] ?? ""; dbEDI315_Detail.B4_status_time = rowLine[5] ?? ""; dbEDI315_Detail.B4_status_location = rowLine[6] ?? ""; dbEDI315_Detail.B4_equip_initial = rowLine[7]; dbEDI315_Detail.B4_equip_number = rowLine[8]; dbEDI315_Detail.B4_equip_status_code = rowLine[9]; dbEDI315_Detail.B4_equip_type = rowLine[10]; dbEDI315_Detail.B4_location_code = rowLine[11]; dbEDI315_Detail.B4_location_id = rowLine[12]; if (rowLine.Count() > 13) { convertToInt = 0; if (rowLine[13] != null && rowLine[13].Trim() != string.Empty && Int32.TryParse(rowLine[13], out convertToInt)) dbEDI315_Detail.B4_equip_check_digit = convertToInt; } #endregion break; case "Q2": #region Q2 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; dbEDI315_Detail.Q2_vessel_code = rowLine[1]; dbEDI315_Detail.Q2_country_code = rowLine[2]; dbEDI315_Detail.Q2_date_1 = rowLine[3]; dbEDI315_Detail.Q2_date_2 = rowLine[4]; dbEDI315_Detail.Q2_date_3 = rowLine[5]; convertToInt = 0; if (rowLine[6] != null && rowLine[6].Trim() != string.Empty && Int32.TryParse(rowLine[6], out convertToInt)) dbEDI315_Detail.Q2_lading_quantity = convertToInt; convertToDecimal = 0; if (rowLine[7] != null && rowLine[7].Trim() != string.Empty && Decimal.TryParse(rowLine[7], out convertToDecimal)) dbEDI315_Detail.Q2_weight = convertToDecimal; dbEDI315_Detail.Q2_weight_qualifier = rowLine[8]; dbEDI315_Detail.Q2_voyage_number = rowLine[9]; dbEDI315_Detail.Q2_reference_id_qualifier = rowLine[10]; dbEDI315_Detail.Q2_reference_id = rowLine[11]; dbEDI315_Detail.Q2_vessel_code_qualifier = rowLine[12]; dbEDI315_Detail.Q2_vessel_name = rowLine[13]; convertToDecimal = 0; if (rowLine[14] != null && rowLine[14].Trim() != string.Empty && Decimal.TryParse(rowLine[14], out convertToDecimal)) dbEDI315_Detail.Q2_volume = convertToDecimal; dbEDI315_Detail.Q2_volume_unit_qualifier = rowLine[15]; dbEDI315_Detail.Q2_weight_unit_code = rowLine[16]; #endregion break; case "N9": #region N9 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; { EDI315_Detail_N9 dbTemp = new EDI315_Detail_N9(); dbTemp.reference_id_qualifier = rowLine[1]; if (rowLine.Count() > 2) dbTemp.reference_id = rowLine[2]; if (rowLine.Count() > 3) dbTemp.free_form_description = rowLine[3]; if (rowLine.Count() > 4) dbTemp.free_form_description = rowLine[4]; if (rowLine.Count() > 5) dbTemp.date = rowLine[5]; if (rowLine.Count() > 6) dbTemp.time = rowLine[6]; if (rowLine.Count() > 7) dbTemp.time_code = rowLine[7]; dbEDI315_Detail_N9List.Add(dbTemp); } #endregion break; case "R4": #region R4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; { EDI315_Detail_R4 dbTemp = new EDI315_Detail_R4(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count() + 1; dbTemp.port_function_code = rowLine[1]; if (rowLine.Count() > 2) dbTemp.location_qualifier = rowLine[2]; if (rowLine.Count() > 3) dbTemp.location_id = rowLine[3]; if (rowLine.Count() > 4) dbTemp.port_name = rowLine[4]; if (rowLine.Count() > 5) dbTemp.country_code = rowLine[5]; if (rowLine.Count() > 6) dbTemp.terminal_name = rowLine[6]; if (rowLine.Count() > 7) dbTemp.pier_number = rowLine[7]; if (rowLine.Count() > 8) dbTemp.province_code = rowLine[8]; dbEDI315_Detail_R4List.Add(dbTemp); } #endregion break; case "DTM": #region DTM if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) continue; { EDI315_Detail_R4_DTM dbTemp = new EDI315_Detail_R4_DTM(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count(); convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) dbTemp.datetime_qualifier = convertToInt; if (rowLine.Count() > 2) dbTemp.date = rowLine[2]; if (rowLine.Count() > 3) dbTemp.time_code = rowLine[3]; dbEDI315_Detail_R4_DTMList.Add(dbTemp); } #endregion break; } } } } catch (Exception ex) { result = false; } return result; }
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; int convertToInt = 0; decimal convertToDecimal = 0; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = rowLine[1] ?? "", ISA_auth_info = rowLine[2] ?? "", ISA_security_info_qualifier = rowLine[3] ?? "", ISA_security_info = rowLine[4] ?? "", ISA_interchange_id_qualifier_1 = rowLine[5] ?? "", ISA_interchange_sender_id = rowLine[6] ?? "", ISA_interchange_id_qualifier_2 = rowLine[7] ?? "", ISA_interchange_receiver_id = rowLine[8] ?? "", ISA_interchage_date = rowLine[9] ?? "", ISA_interchange_time = rowLine[10] ?? "", ISA_interchange_control_standards_id = rowLine[11] ?? "", ISA_interchange_control_version_number = rowLine[12] ?? "", ISA_interchange_control_number = rowLine[13] ?? "", ISA_ack_requested = rowLine[14] ?? "", ISA_usage_indicator = rowLine[15] ?? "", ISA_component_element_separator = rowLine[16] ?? "", }; #endregion break; case "GS": #region GS dbEDI315.GS_functional_id_code = rowLine[1] ?? ""; dbEDI315.GS_app_sender_code = rowLine[2] ?? ""; dbEDI315.GS_app_receiver_code = rowLine[3] ?? ""; dbEDI315.GS_date = rowLine[4] ?? ""; dbEDI315.GS_time = rowLine[5] ?? ""; dbEDI315.GS_group_control_number = rowLine[6] ?? ""; dbEDI315.GS_reponsible_agency_code = rowLine[7] ?? ""; dbEDI315.GS_industry_id_code = rowLine[8] ?? ""; /* Insert table EDI315 after done with ISA & GS */ if (dbEDI315.msg_idnum != 0) { using (DBContext context = new DBContext()) { /* check DB is open */ dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); } } #endregion break; case "ST": dbEDI315_Detail = new EDI315_Detail(); break; case "SE": // insert EDI315_Detail // init. all vaues break; case "B4": if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.B4_SHC = rowLine[1]; convertToInt = 0; if (rowLine[2] != null && rowLine[2].Trim() != string.Empty && Int32.TryParse(rowLine[2], out convertToInt)) { dbEDI315_Detail.B4_request_number = convertToInt; } dbEDI315_Detail.B4_status_code = rowLine[3]; dbEDI315_Detail.B4_date = rowLine[4] ?? ""; dbEDI315_Detail.B4_status_time = rowLine[5] ?? ""; dbEDI315_Detail.B4_status_location = rowLine[6] ?? ""; dbEDI315_Detail.B4_equip_initial = rowLine[7]; dbEDI315_Detail.B4_equip_number = rowLine[8]; dbEDI315_Detail.B4_equip_status_code = rowLine[9]; dbEDI315_Detail.B4_equip_type = rowLine[10]; dbEDI315_Detail.B4_location_code = rowLine[11]; dbEDI315_Detail.B4_location_id = rowLine[12]; if (rowLine.Count() > 13) { convertToInt = 0; if (rowLine[13] != null && rowLine[13].Trim() != string.Empty && Int32.TryParse(rowLine[13], out convertToInt)) { dbEDI315_Detail.B4_equip_check_digit = convertToInt; } } break; case "Q2": if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.Q2_vessel_code = rowLine[1]; dbEDI315_Detail.Q2_country_code = rowLine[2]; dbEDI315_Detail.Q2_date_1 = rowLine[3]; dbEDI315_Detail.Q2_date_2 = rowLine[4]; dbEDI315_Detail.Q2_date_3 = rowLine[5]; convertToInt = 0; if (rowLine[6] != null && rowLine[6].Trim() != string.Empty && Int32.TryParse(rowLine[6], out convertToInt)) { dbEDI315_Detail.Q2_lading_quantity = convertToInt; } convertToDecimal = 0; if (rowLine[7] != null && rowLine[7].Trim() != string.Empty && Decimal.TryParse(rowLine[7], out convertToDecimal)) { dbEDI315_Detail.Q2_weight = convertToDecimal; } dbEDI315_Detail.Q2_weight_qualifier = rowLine[8]; dbEDI315_Detail.Q2_voyage_number = rowLine[9]; dbEDI315_Detail.Q2_reference_id_qualifier = rowLine[10]; dbEDI315_Detail.Q2_reference_id = rowLine[11]; dbEDI315_Detail.Q2_vessel_code_qualifier = rowLine[12]; dbEDI315_Detail.Q2_vessel_name = rowLine[13]; convertToDecimal = 0; if (rowLine[14] != null && rowLine[14].Trim() != string.Empty && Decimal.TryParse(rowLine[14], out convertToDecimal)) { dbEDI315_Detail.Q2_volume = convertToDecimal; } dbEDI315_Detail.Q2_volume_unit_qualifier = rowLine[15]; dbEDI315_Detail.Q2_weight_unit_code = rowLine[16]; break; case "N9": if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } EDI315_Detail_N9 dbTemp = new EDI315_Detail_N9(); dbTemp.reference_id_qualifier = rowLine[1]; if (rowLine.Count() > 2) { dbTemp.reference_id = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.free_form_description = rowLine[3]; } if (rowLine.Count() > 4) { dbTemp.free_form_description = rowLine[4]; } dbEDI315_Detail_N9List.Add(dbTemp); break; } } } } catch (Exception ex) { result = false; } return(result); }
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); /* ISA Init. */ string ISA_auth_info_qualifier; string ISA_auth_info; string ISA_security_info_qualifier; string ISA_security_info; string ISA_interchange_id_qualifier_1; string ISA_interchange_sender_id; string ISA_interchange_id_qualifier_2; string ISA_interchange_receiver_id; string ISA_interchage_date; string ISA_interchange_time; string ISA_interchange_control_standards_id; string ISA_interchange_control_version_number; string ISA_interchange_control_number; string ISA_ack_requested; string ISA_usage_indicator; string ISA_component_element_separator; /* GS Init. */ string GS_functional_id_code; string GS_app_sender_code; string GS_app_receiver_code; string GS_date; string GS_time; string GS_group_control_number; string GS_reponsible_agency_code; string GS_industry_id_code; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { case "ISA": #region ISA // NEED BELOW???? ISA_auth_info_qualifier = rowLine[1] ?? ""; ISA_auth_info = rowLine[2] ?? ""; ISA_security_info_qualifier = rowLine[3] ?? ""; ISA_security_info = rowLine[4] ?? ""; ISA_interchange_id_qualifier_1 = rowLine[5] ?? ""; ISA_interchange_sender_id = rowLine[6] ?? ""; ISA_interchange_id_qualifier_2 = rowLine[7] ?? ""; ISA_interchange_receiver_id = rowLine[8] ?? ""; ISA_interchage_date = rowLine[9] ?? ""; ISA_interchange_time = rowLine[10] ?? ""; ISA_interchange_control_standards_id = rowLine[11] ?? ""; ISA_interchange_control_version_number = rowLine[12] ?? ""; ISA_interchange_control_number = rowLine[13] ?? ""; ISA_ack_requested = rowLine[14] ?? ""; ISA_usage_indicator = rowLine[15] ?? ""; ISA_component_element_separator = rowLine[16] ?? ""; dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = ISA_auth_info_qualifier, ISA_auth_info = ISA_auth_info, ISA_security_info_qualifier = ISA_security_info_qualifier, ISA_security_info = ISA_security_info, ISA_interchange_id_qualifier_1 = ISA_interchange_id_qualifier_1, ISA_interchange_sender_id = ISA_interchange_sender_id, ISA_interchange_id_qualifier_2 = ISA_interchange_id_qualifier_2, ISA_interchange_receiver_id = ISA_interchange_receiver_id, ISA_interchage_date = ISA_interchage_date, ISA_interchange_time = ISA_interchange_time, ISA_interchange_control_standards_id = ISA_interchange_control_standards_id, ISA_interchange_control_version_number = ISA_interchange_control_version_number, ISA_interchange_control_number = ISA_interchange_control_number, ISA_ack_requested = ISA_ack_requested, ISA_usage_indicator = ISA_usage_indicator, ISA_component_element_separator = ISA_component_element_separator, }; #endregion break; case "GS": #region GS dbEDI315.GS_functional_id_code = rowLine[1] ?? ""; dbEDI315.GS_app_sender_code = rowLine[2] ?? ""; dbEDI315.GS_app_receiver_code = rowLine[3] ?? ""; dbEDI315.GS_date = rowLine[4] ?? ""; dbEDI315.GS_time = rowLine[5] ?? ""; dbEDI315.GS_group_control_number = rowLine[6] ?? ""; dbEDI315.GS_reponsible_agency_code = rowLine[7] ?? ""; dbEDI315.GS_industry_id_code = rowLine[8] ?? ""; /* Insert table EDI315 after done with ISA & GS */ if (dbEDI315.msg_idnum != 0) { using (DBContext context = new DBContext()) { /* check DB is open */ dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); } } #endregion break; case "ST": dbEDI315_Detail = new EDI315_Detail(); break; case "SE": // insert EDI315_Detail // init. all vaues break; case "B4": if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.B4_SHC = rowLine[1]; break; } } } } catch (Exception ex) { result = false; } return(result); }
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); int convertToInt = 0; decimal convertToDecimal = 0; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = rowLine[1] ?? "", ISA_auth_info = rowLine[2] ?? "", ISA_security_info_qualifier = rowLine[3] ?? "", ISA_security_info = rowLine[4] ?? "", ISA_interchange_id_qualifier_1 = rowLine[5] ?? "", ISA_interchange_sender_id = rowLine[6] ?? "", ISA_interchange_id_qualifier_2 = rowLine[7] ?? "", ISA_interchange_receiver_id = rowLine[8] ?? "", ISA_interchage_date = rowLine[9] ?? "", ISA_interchange_time = rowLine[10] ?? "", ISA_interchange_control_standards_id = rowLine[11] ?? "", ISA_interchange_control_version_number = rowLine[12] ?? "", ISA_interchange_control_number = rowLine[13] ?? "", ISA_ack_requested = rowLine[14] ?? "", ISA_usage_indicator = rowLine[15] ?? "", ISA_component_element_separator = rowLine[16] ?? "", }; #endregion break; case "GS": // Table EDI315 insert here. #region GS dbEDI315.GS_functional_id_code = rowLine[1] ?? ""; dbEDI315.GS_app_sender_code = rowLine[2] ?? ""; dbEDI315.GS_app_receiver_code = rowLine[3] ?? ""; dbEDI315.GS_date = rowLine[4] ?? ""; dbEDI315.GS_time = rowLine[5] ?? ""; dbEDI315.GS_group_control_number = rowLine[6] ?? ""; dbEDI315.GS_reponsible_agency_code = rowLine[7] ?? ""; dbEDI315.GS_industry_id_code = rowLine[8] ?? ""; /* Insert table EDI315 after done with ISA & GS */ { if (dbEDI315.msg_idnum != 0) { if (dbConnectionCheck()) { } using (DBContext context = new DBContext()) { /* check DB is open */ dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); context.Dispose(); } } } else { // not able to access DB //return false; } #endregion break; case "ST": dbEDI315_Detail = new EDI315_Detail(); break;
public bool MeesageParsing(string msgType, string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List<EDI315_Detail_N9> dbEDI315_Detail_N9List = new List<EDI315_Detail_N9>(); List<EDI315_Detail_R4> dbEDI315_Detail_R4List = new List<EDI315_Detail_R4>(); List<EDI315_Detail_R4_DTM> dbEDI315_Detail_R4_DTMList = new List<EDI315_Detail_R4_DTM>(); int convertToInt = 0; decimal convertToDecimal = 0; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Length > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) header = rowLine[0]; switch (header) { case "ISA": #region ISA if (rowLine.Count() == 17) // header 1 + element 16 == 17 { dbEDI315 = new EDI315() { ISA_auth_info_qualifier = SubstringValue(rowLine[1] ?? "", 2), ISA_auth_info = SubstringValue(rowLine[2] ?? "", 10), ISA_security_info_qualifier = SubstringValue(rowLine[3] ?? "", 2), ISA_security_info = SubstringValue(rowLine[4] ?? "", 10), ISA_interchange_id_qualifier_1 = SubstringValue(rowLine[5] ?? "", 2), ISA_interchange_sender_id = SubstringValue(rowLine[6] ?? "", 15), ISA_interchange_id_qualifier_2 = SubstringValue(rowLine[7] ?? "", 2), ISA_interchange_receiver_id = SubstringValue(rowLine[8] ?? "", 15), ISA_interchage_date = SubstringValue(rowLine[9] ?? "", 8), ISA_interchange_time = SubstringValue(rowLine[10] ?? "", 4), ISA_interchange_control_standards_id = SubstringValue(rowLine[11] ?? "", 1), ISA_interchange_control_version_number = SubstringValue(rowLine[12] ?? "", 5), ISA_interchange_control_number = SubstringValue(rowLine[13] ?? "", 9), ISA_ack_requested = SubstringValue(rowLine[14] ?? "", 1), ISA_usage_indicator = SubstringValue(rowLine[15] ?? "", 1), ISA_component_element_separator = SubstringValue(rowLine[16] ?? "", 1), }; } else { throw new Exception("Mandatory elements are missing for " + header + ". " + header + " message line is \r\n" + row + "\r\n"); } #endregion break; case "GS": // insert DB #region GS if (rowLine.Count() == 9) // header 1 + element 8 == 9 { dbEDI315.GS_functional_id_code = SubstringValue(rowLine[1] ?? "", 2); dbEDI315.GS_app_sender_code = SubstringValue(rowLine[2] ?? "", 15); dbEDI315.GS_app_receiver_code = SubstringValue(rowLine[3] ?? "", 15); dbEDI315.GS_date = SubstringValue(rowLine[4] ?? "", 8); dbEDI315.GS_time = SubstringValue(rowLine[5] ?? "", 8); dbEDI315.GS_group_control_number = SubstringValue(rowLine[6] ?? "", 9); dbEDI315.GS_reponsible_agency_code = SubstringValue(rowLine[7] ?? "", 2); dbEDI315.GS_industry_id_code = SubstringValue(rowLine[8] ?? "", 12); /* Insert table EDI315 after done with ISA & GS */ if (msg_idnum != 0) { if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { dbEDI315.msg_idnum = msg_idnum; dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); /* DB context. END. */ context.Dispose(); } } else { string logMsg = "Date: " + DateTime.Now.ToString(); logMsg += "\r\nFunction: MeesageParsing - Table EDI315"; logMsg += "\r\nError Message: Not able to access DB. Process rollbacked."; logMsg += "\r\nValues Info:"; logMsg += "\r\nmstType: 315"; if (msg_idnum != 0) logMsg += "\r\nmsg_idnum: " + msg_idnum; if (dbEDI315.EDI315_idnum != 0) logMsg += "\r\nEDI_idnum: " + dbEDI315.EDI315_idnum; util.insertLog_TextFile(msgType, msg_idnum, dbEDI315.EDI315_idnum, 0, logMsg); rollbackProcess(msg_idnum, msgType); return false; } } } else { throw new Exception("Mandatory elements are missing for " + header + ". " + header + " message line is \r\n" + row + "\r\n"); } #endregion break; case "ST": #region ST /* Detail start. Init. */ dbEDI315_Detail = new EDI315_Detail(); dbEDI315_Detail_N9List = new List<EDI315_Detail_N9>(); dbEDI315_Detail_R4List = new List<EDI315_Detail_R4>(); dbEDI315_Detail_R4_DTMList = new List<EDI315_Detail_R4_DTM>(); #endregion break; case "SE": /* Detail End. Insert into Tables. */ #region SE if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) return result; if (rowLine.Count() > 2) { convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) dbEDI315_Detail.SE_included_segments_number = convertToInt; dbEDI315_Detail.SE_transaction_set_control_number = SubstringValue(rowLine[2] ?? "", 9); /* insert Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4 & EDI315_Detail_R4_DTM */ if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { /* EDI315_Detail Insert */ dbEDI315_Detail.msg_idnum = msg_idnum; dbEDI315_Detail.EDI315_idnum = dbEDI315.EDI315_idnum; context.EDI315_Detail.Add(dbEDI315_Detail); context.SaveChanges(); /* EDI315_Detail_N9 Insert */ foreach (EDI315_Detail_N9 dbN9Row in dbEDI315_Detail_N9List) { dbN9Row.msg_idnum = msg_idnum; dbN9Row.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; } context.EDI315_Detail_N9.AddRange(dbEDI315_Detail_N9List); context.SaveChanges(); /* EDI315_Detail_R4 & EDI315_Detail_R4_DTM Insert */ foreach (EDI315_Detail_R4 dbR4Row in dbEDI315_Detail_R4List) { List<EDI315_Detail_R4_DTM> dtmList = dbEDI315_Detail_R4_DTMList.Where(x => x.Detail_R4_idnum == dbR4Row.Detail_R4_idnum).ToList(); dbR4Row.msg_idnum = msg_idnum; dbR4Row.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; context.EDI315_Detail_R4.Add(dbR4Row); context.SaveChanges(); if (dtmList != null && dtmList.Count() > 0) { foreach (EDI315_Detail_R4_DTM dtmRow in dtmList) { dtmRow.msg_idnum = msg_idnum; dtmRow.Detail_R4_idnum = dbR4Row.Detail_R4_idnum; } context.EDI315_Detail_R4_DTM.AddRange(dtmList); context.SaveChanges(); } } } /* DB context. END. */ context.Dispose(); } } else { string logMsg = "Date: " + DateTime.Now.ToString(); logMsg += "\r\nFunction: MeesageParsing - Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4, EDI315_Detail_R4_DTM"; logMsg += "\r\nError Message: Not able to access DB. Process rollbacked."; logMsg += "\r\nValues Info:"; logMsg += "\r\nmstType: 315"; if (msg_idnum != 0) logMsg += "\r\nmsg_idnum: " + msg_idnum; if (dbEDI315.EDI315_idnum != 0) logMsg += "\r\nEDI_idnum: " + dbEDI315.EDI315_idnum; if (dbEDI315_Detail.EDI315_Detail_idnum != 0) logMsg += "\r\nDetail_idnum: " + dbEDI315_Detail.EDI315_Detail_idnum; util.insertLog_TextFile(msgType, msg_idnum, dbEDI315.EDI315_idnum, dbEDI315_Detail.EDI315_Detail_idnum, logMsg); rollbackProcess(msg_idnum, msgType); return false; } } else { throw new Exception("Mandatory elements are missing for " + header + ". " + header + " message line is \r\n" + row + "\r\n"); } #endregion break; case "B4": #region B4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) return result; if(rowLine.Count() > 6) { dbEDI315_Detail.B4_SHC = SubstringValue(rowLine[1], 3); convertToInt = 0; if (rowLine[2] != null && rowLine[2].Trim() != string.Empty && Int32.TryParse(rowLine[2], out convertToInt)) dbEDI315_Detail.B4_request_number = convertToInt; dbEDI315_Detail.B4_status_code = SubstringValue(rowLine[3], 2); dbEDI315_Detail.B4_date = SubstringValue(rowLine[4] ?? "", 8); dbEDI315_Detail.B4_status_time = SubstringValue(rowLine[5] ?? "", 4); dbEDI315_Detail.B4_status_location = SubstringValue(rowLine[6] ?? "", 5); if(rowLine.Count() > 7) dbEDI315_Detail.B4_equip_initial = SubstringValue(rowLine[7], 4); if (rowLine.Count() > 8) dbEDI315_Detail.B4_equip_number = SubstringValue(rowLine[8], 10); if (rowLine.Count() > 9) dbEDI315_Detail.B4_equip_status_code = SubstringValue(rowLine[9], 2); if (rowLine.Count() > 10) dbEDI315_Detail.B4_equip_type = SubstringValue(rowLine[10], 4); if (rowLine.Count() > 11) dbEDI315_Detail.B4_location_code = SubstringValue(rowLine[11], 30); if (rowLine.Count() > 12) dbEDI315_Detail.B4_location_id = SubstringValue(rowLine[12], 2); if (rowLine.Count() > 13) { convertToInt = 0; if (rowLine[13] != null && rowLine[13].Trim() != string.Empty && Int32.TryParse(rowLine[13], out convertToInt)) dbEDI315_Detail.B4_equip_check_digit = convertToInt; } } else { throw new Exception("Mandatory elements are missing for " + header + ". " + header + " message line is \r\n" + row + "\r\n"); } #endregion break;
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { int tempConvertToInt; case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = rowLine[1] ?? "", ISA_auth_info = rowLine[2] ?? "", ISA_security_info_qualifier = rowLine[3] ?? "", ISA_security_info = rowLine[4] ?? "", ISA_interchange_id_qualifier_1 = rowLine[5] ?? "", ISA_interchange_sender_id = rowLine[6] ?? "", ISA_interchange_id_qualifier_2 = rowLine[7] ?? "", ISA_interchange_receiver_id = rowLine[8] ?? "", ISA_interchage_date = rowLine[9] ?? "", ISA_interchange_time = rowLine[10] ?? "", ISA_interchange_control_standards_id = rowLine[11] ?? "", ISA_interchange_control_version_number = rowLine[12] ?? "", ISA_interchange_control_number = rowLine[13] ?? "", ISA_ack_requested = rowLine[14] ?? "", ISA_usage_indicator = rowLine[15] ?? "", ISA_component_element_separator = rowLine[16] ?? "", }; #endregion break;
public bool MeesageParsing(string msg_body, int msg_idnum) { bool result = false; try { #region Initialize values string[] msgBody; string[] rowLine; string header; EDI315 dbEDI315 = new EDI315(); EDI315_Detail dbEDI315_Detail = new EDI315_Detail(); List <EDI315_Detail_N9> dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); List <EDI315_Detail_R4> dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); List <EDI315_Detail_R4_DTM> dbEDI315_Detail_R4_DTMList = new List <EDI315_Detail_R4_DTM>(); int convertToInt = 0; decimal convertToDecimal = 0; #endregion msg_body = Regex.Replace(msg_body, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); msgBody = msg_body.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string row in msgBody) { if (row != null && row.Count() > 0) { rowLine = row.Split('*'); header = ""; if (EDI315_HEADERS.Any(x => x.ToUpper() == rowLine[0])) { header = rowLine[0]; } switch (header) { case "ISA": #region ISA dbEDI315 = new EDI315() { msg_idnum = msg_idnum, ISA_auth_info_qualifier = rowLine[1] ?? "", ISA_auth_info = rowLine[2] ?? "", ISA_security_info_qualifier = rowLine[3] ?? "", ISA_security_info = rowLine[4] ?? "", ISA_interchange_id_qualifier_1 = rowLine[5] ?? "", ISA_interchange_sender_id = rowLine[6] ?? "", ISA_interchange_id_qualifier_2 = rowLine[7] ?? "", ISA_interchange_receiver_id = rowLine[8] ?? "", ISA_interchage_date = rowLine[9] ?? "", ISA_interchange_time = rowLine[10] ?? "", ISA_interchange_control_standards_id = rowLine[11] ?? "", ISA_interchange_control_version_number = rowLine[12] ?? "", ISA_interchange_control_number = rowLine[13] ?? "", ISA_ack_requested = rowLine[14] ?? "", ISA_usage_indicator = rowLine[15] ?? "", ISA_component_element_separator = rowLine[16] ?? "", }; #endregion break; case "GS": // insert DB #region GS dbEDI315.GS_functional_id_code = rowLine[1] ?? ""; dbEDI315.GS_app_sender_code = rowLine[2] ?? ""; dbEDI315.GS_app_receiver_code = rowLine[3] ?? ""; dbEDI315.GS_date = rowLine[4] ?? ""; dbEDI315.GS_time = rowLine[5] ?? ""; dbEDI315.GS_group_control_number = rowLine[6] ?? ""; dbEDI315.GS_reponsible_agency_code = rowLine[7] ?? ""; dbEDI315.GS_industry_id_code = rowLine[8] ?? ""; /* Insert table EDI315 after done with ISA & GS */ if (dbEDI315.msg_idnum != 0) { if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { dbEDI315.created_date = DateTime.Now; context.EDI315.Add(dbEDI315); context.SaveChanges(); context.Dispose(); } } else { string logMsg = "Function: MeesageParsing - Table EDI315\r\n"; logMsg += "Error Message: Not able to access DB. Process rollbacked.\r\n"; util.insertLog("315", msg_idnum, dbEDI315.EDI315_idnum, 0, logMsg); rollbackProcess(msg_idnum); return(false); } } #endregion break; case "ST": #region ST /* Detail start. Init. */ dbEDI315_Detail = new EDI315_Detail(); dbEDI315_Detail_N9List = new List <EDI315_Detail_N9>(); dbEDI315_Detail_R4List = new List <EDI315_Detail_R4>(); dbEDI315_Detail_R4_DTMList = new List <EDI315_Detail_R4_DTM>(); #endregion break; case "SE": // inset DB #region SE if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) { dbEDI315_Detail.SE_included_segments_number = convertToInt; } dbEDI315_Detail.SE_transaction_set_control_number = rowLine[2]; /* insert Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4 & EDI315_Detail_R4_DTM */ if (util.dbConnectionCheck()) { using (DBContext context = new DBContext()) { /* EDI315_Detail Insert */ dbEDI315_Detail.EDI315_idnum = dbEDI315.EDI315_idnum; context.EDI315_Detail.Add(dbEDI315_Detail); context.SaveChanges(); /* EDI315_Detail_N9 Insert */ dbEDI315_Detail_N9List.ForEach(x => x.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum); context.EDI315_Detail_N9.AddRange(dbEDI315_Detail_N9List); context.SaveChanges(); /* EDI315_Detail_R4 & EDI315_Detail_R4_DTM Insert */ foreach (EDI315_Detail_R4 dbR4Row in dbEDI315_Detail_R4List) { List <EDI315_Detail_R4_DTM> dtmList = dbEDI315_Detail_R4_DTMList.Where(x => x.Detail_R4_idnum == dbR4Row.Detail_R4_idnum).ToList(); context.EDI315_Detail_R4.Add(dbR4Row); context.SaveChanges(); foreach (EDI315_Detail_R4_DTM dtmRow in dtmList) { dtmRow.EDI315_Detail_idnum = dbEDI315_Detail.EDI315_Detail_idnum; dtmRow.Detail_R4_idnum = dbR4Row.Detail_R4_idnum; } context.EDI315_Detail_R4_DTM.AddRange(dtmList); context.SaveChanges(); } context.Dispose(); } } else { string logMsg = "Function: MeesageParsing - Table EDI315_Detail, EDI315_Detail_N9, EDI315_Detail_R4, EDI315_Detail_R4_DTM\r\n"; logMsg += "Error Message: Not able to access DB. Process rollbacked.\r\n"; util.insertLog("315", msg_idnum, dbEDI315.EDI315_idnum, dbEDI315_Detail.EDI315_Detail_idnum, logMsg); rollbackProcess(msg_idnum); return(false); } #endregion break; case "B4": #region B4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.B4_SHC = rowLine[1]; convertToInt = 0; if (rowLine[2] != null && rowLine[2].Trim() != string.Empty && Int32.TryParse(rowLine[2], out convertToInt)) { dbEDI315_Detail.B4_request_number = convertToInt; } dbEDI315_Detail.B4_status_code = rowLine[3]; dbEDI315_Detail.B4_date = rowLine[4] ?? ""; dbEDI315_Detail.B4_status_time = rowLine[5] ?? ""; dbEDI315_Detail.B4_status_location = rowLine[6] ?? ""; dbEDI315_Detail.B4_equip_initial = rowLine[7]; dbEDI315_Detail.B4_equip_number = rowLine[8]; dbEDI315_Detail.B4_equip_status_code = rowLine[9]; dbEDI315_Detail.B4_equip_type = rowLine[10]; dbEDI315_Detail.B4_location_code = rowLine[11]; dbEDI315_Detail.B4_location_id = rowLine[12]; if (rowLine.Count() > 13) { convertToInt = 0; if (rowLine[13] != null && rowLine[13].Trim() != string.Empty && Int32.TryParse(rowLine[13], out convertToInt)) { dbEDI315_Detail.B4_equip_check_digit = convertToInt; } } #endregion break; case "Q2": #region Q2 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } dbEDI315_Detail.Q2_vessel_code = rowLine[1]; dbEDI315_Detail.Q2_country_code = rowLine[2]; dbEDI315_Detail.Q2_date_1 = rowLine[3]; dbEDI315_Detail.Q2_date_2 = rowLine[4]; dbEDI315_Detail.Q2_date_3 = rowLine[5]; convertToInt = 0; if (rowLine[6] != null && rowLine[6].Trim() != string.Empty && Int32.TryParse(rowLine[6], out convertToInt)) { dbEDI315_Detail.Q2_lading_quantity = convertToInt; } convertToDecimal = 0; if (rowLine[7] != null && rowLine[7].Trim() != string.Empty && Decimal.TryParse(rowLine[7], out convertToDecimal)) { dbEDI315_Detail.Q2_weight = convertToDecimal; } dbEDI315_Detail.Q2_weight_qualifier = rowLine[8]; dbEDI315_Detail.Q2_voyage_number = rowLine[9]; dbEDI315_Detail.Q2_reference_id_qualifier = rowLine[10]; dbEDI315_Detail.Q2_reference_id = rowLine[11]; dbEDI315_Detail.Q2_vessel_code_qualifier = rowLine[12]; dbEDI315_Detail.Q2_vessel_name = rowLine[13]; convertToDecimal = 0; if (rowLine[14] != null && rowLine[14].Trim() != string.Empty && Decimal.TryParse(rowLine[14], out convertToDecimal)) { dbEDI315_Detail.Q2_volume = convertToDecimal; } dbEDI315_Detail.Q2_volume_unit_qualifier = rowLine[15]; dbEDI315_Detail.Q2_weight_unit_code = rowLine[16]; #endregion break; case "N9": #region N9 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_N9 dbTemp = new EDI315_Detail_N9(); dbTemp.reference_id_qualifier = rowLine[1]; if (rowLine.Count() > 2) { dbTemp.reference_id = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.free_form_description = rowLine[3]; } if (rowLine.Count() > 4) { dbTemp.free_form_description = rowLine[4]; } if (rowLine.Count() > 5) { dbTemp.date = rowLine[5]; } if (rowLine.Count() > 6) { dbTemp.time = rowLine[6]; } if (rowLine.Count() > 7) { dbTemp.time_code = rowLine[7]; } dbEDI315_Detail_N9List.Add(dbTemp); } #endregion break; case "R4": #region R4 if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_R4 dbTemp = new EDI315_Detail_R4(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count() + 1; dbTemp.port_function_code = rowLine[1]; if (rowLine.Count() > 2) { dbTemp.location_qualifier = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.location_id = rowLine[3]; } if (rowLine.Count() > 4) { dbTemp.port_name = rowLine[4]; } if (rowLine.Count() > 5) { dbTemp.country_code = rowLine[5]; } if (rowLine.Count() > 6) { dbTemp.terminal_name = rowLine[6]; } if (rowLine.Count() > 7) { dbTemp.pier_number = rowLine[7]; } if (rowLine.Count() > 8) { dbTemp.province_code = rowLine[8]; } dbEDI315_Detail_R4List.Add(dbTemp); } #endregion break; case "DTM": #region DTM if (dbEDI315 == null || dbEDI315.EDI315_idnum == 0) { continue; } { EDI315_Detail_R4_DTM dbTemp = new EDI315_Detail_R4_DTM(); dbTemp.Detail_R4_idnum = dbEDI315_Detail_R4List.Count(); convertToInt = 0; if (rowLine[1] != null && rowLine[1].Trim() != string.Empty && Int32.TryParse(rowLine[1], out convertToInt)) { dbTemp.datetime_qualifier = convertToInt; } if (rowLine.Count() > 2) { dbTemp.date = rowLine[2]; } if (rowLine.Count() > 3) { dbTemp.time_code = rowLine[3]; } dbEDI315_Detail_R4_DTMList.Add(dbTemp); } #endregion break; } } } } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); string errorFunc = "AcceptanceService.saveAWB"; string parameters = "MID - " + awbModel.MID + ", Prefix - " + awbModel.Prefix + ", AWB - " + awbModel.AWB; parameters += "\r\n"; parameters += "awbModel Values\r\n"; parameters += getawbModelValues(awbModel); string errorMsg = exceptionMessage; buildLOG(errorFunc, parameters, errorMsg); } catch (Exception ex) { string logMsg = "Function: MeesageParsing\r\n"; logMsg += "Process rollbacked.\r\n"; logMsg += "Error Message: " + ex.ToString(); util.insertLog("315", msg_idnum, 0, 0, logMsg); rollbackProcess(msg_idnum); result = false; } return(result); }