static int Main(string[] args) { Parameters parameters = ParseArguments(args); if (parameters == null) { ConsoleExtensions.WriteError("Invalid arguments"); ConsoleExtensions.WriteInfo("Usage: SpecCertGenerator -generate Edi|Xml|FlatFile [-upload] <maps zip file>"); ConsoleExtensions.WriteInfo("Usage: SpecCertGenerator -validate <spec cert file> <instance file>"); return(-1); } int returnValue = 0; switch (parameters.Option) { case "generate": returnValue = ReadAllBtmFiles(parameters); break; case "validate": returnValue = ValidateInstanceFile(parameters); break; } return(returnValue); }
private string GetAssemblyPath() { string assemblyName = null; string assemblyPath = null; if (TreeReference.StartsWith("x12", StringComparison.OrdinalIgnoreCase)) { assemblyName = TreeReference; assemblyName = assemblyName.Replace(".transactions", ""); } else if (TreeReference.StartsWith("GCommerce.EDI", StringComparison.OrdinalIgnoreCase)) { assemblyName = TreeReference; assemblyName = assemblyName.Replace("._", "."); assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(".Schemas")); } if (string.IsNullOrWhiteSpace(assemblyName) == false) { assemblyName += ".dll"; assemblyPath = Directory.GetFiles(BtmFileDirName, assemblyName, SearchOption.AllDirectories).FirstOrDefault(); if (assemblyPath != null) { ConsoleExtensions.WriteInfo("Found {0}", Path.GetFileName(assemblyPath)); } else { ConsoleExtensions.WriteError("No assembly {0} found for {1} ", assemblyName, TreeReference); } } return(assemblyPath); }
private static void LogMessage(StreamWriter logFile, string message, int type) { logFile.WriteLine(string.Format("{0} - {1}", type == 1 ? "INFO" : type == 2 ? "WARNING" : "ERROR", message)); message = message.Replace('{', '['); message = message.Replace('}', ']'); switch (type) { case 1: ConsoleExtensions.WriteInfo(message); break; case 2: ConsoleExtensions.WriteWarning(message); break; case 3: ConsoleExtensions.WriteError(message); break; } }
private static int ValidateInstanceFile(Parameters parameters) { string specCertFullPath = parameters.SpecCertName; string specCertFileName = Path.GetFileName(specCertFullPath); string instanceFilePath = parameters.InstanceFileName; string instanceFileName = Path.GetFileName(instanceFilePath); if (File.Exists(specCertFullPath) == false) { ConsoleExtensions.WriteError("{0} does not exist.", specCertFullPath); return(1); } if (Path.GetExtension(specCertFullPath) != ".xlsx") { ConsoleExtensions.WriteError("{0} is not a zip file.", specCertFullPath); return(1); } if (File.Exists(instanceFilePath) == false) { ConsoleExtensions.WriteError("{0} does not exist.", instanceFilePath); return(1); } string logFileName = Path.ChangeExtension(instanceFileName, "log"); ConsoleExtensions.WriteInfo("Log file for this processing: {0}", logFileName); string instanceFileData = File.ReadAllText(instanceFilePath); using (StreamWriter logFile = new StreamWriter(logFileName)) { try { TradingPartnerSpecCertMetadata metadata = new TradingPartnerSpecCertMetadata(); metadata.Initialize(specCertFileName); GCExcelToDocumentPlug excelToDocumentPlug; switch ((SpecCertFileType)metadata.FileType) { case SpecCertFileType.X12: excelToDocumentPlug = new X12GCExcelToDocumentPlug(); break; case SpecCertFileType.Xml: excelToDocumentPlug = new XmlGCExcelToDocumentPlug(); break; case SpecCertFileType.FlatFile: excelToDocumentPlug = new FlatFileGCExcelToDocumentPlug(); break; default: throw new NotSupportedException(string.Format("Spec cert type {0} is not supported", (SpecCertFileType)metadata.FileType)); break; } LogInformation(logFile, string.Format("Generating document plug from spec cert {0}", specCertFileName)); TradingPartnerSpecCertMetadata specCertMetadata = new TradingPartnerSpecCertMetadata(); specCertMetadata.Initialize(specCertFileName); IDocumentPlug documentPlug = null; using (StreamReader stream = new StreamReader(specCertFullPath)) documentPlug = excelToDocumentPlug.GenerateDocumentPlug( stream.BaseStream, specCertMetadata.TradingPartnerName, specCertMetadata.DocumentType, specCertMetadata.Direction, Maarg.Contracts.GCValidate.SpecCertFileType.X12); if (documentPlug == null) { LogError(logFile, "Document plug generation failed"); return(-1); } else { LogInformation(logFile, "Document plug generated successfully"); } LogInformation(logFile, "Validating instance file"); IFatpipeDocument fatpipeDocument; EDIValidationResult result = EdiValidator.ValidateEdi(instanceFileData, instanceFileName, specCertFileName, documentPlug, out fatpipeDocument); LogValidationResult(result, logFile); } catch (Exception ex) { LogError(logFile, string.Format("Error encountered during validating {0} file. Error: {1}", instanceFileName, ex.ToString())); } } return(0); }
private static int ReadAllBtmFiles(Parameters parameters) { string fileName = parameters.ZipFileName; if (File.Exists(fileName) == false) { ConsoleExtensions.WriteError("{0} does not exist.", fileName); return(1); } if (Path.GetExtension(fileName) != ".zip") { ConsoleExtensions.WriteError("{0} is not a zip file.", fileName); return(1); } string logFileName = Path.ChangeExtension(Path.GetFileName(fileName), "log"); ConsoleExtensions.WriteInfo("Log file for this processing: {0}", logFileName); using (StreamWriter logFile = new StreamWriter(logFileName)) { try { using (StreamReader sr = new StreamReader(fileName)) { LogInformation(logFile, string.Format("Reading maps from {0}", fileName)); List <MapDetail> mapsDetailList = BtmFileReader.ReadMap(sr.BaseStream, fileName, parameters.SpecCertType); if (mapsDetailList != null) { LogInformation(logFile, string.Format("{0} maps retrieved.", mapsDetailList.Count)); int specCertCount = 0; foreach (MapDetail mapDetail in mapsDetailList) { if (mapDetail.DocumentType != 810 && mapDetail.DocumentType != 850 && mapDetail.DocumentType != 856) { //LogInformation(logFile, string.Format("Spec cert generation for document type {0} is not supported", mapDetail.DocumentType)); continue; } LogInformation(logFile, "======================================"); LogInformation(logFile, "Map detail:"); LogInformation(logFile, string.Format("\t{0}", mapDetail.FileName)); LogInformation(logFile, string.Format("\t{0}", mapDetail.FolderName)); Maarg.Fatpipe.Plug.Authoring.BtmFileHandler.SpecCertGenerator specCertGenerator = new Maarg.Fatpipe.Plug.Authoring.BtmFileHandler.SpecCertGenerator(); try { SpecCertGenerationResult result = specCertGenerator.GenerateSpecCert(mapDetail, parameters.SpecCertType); if (result.PathsUsed.Count == 0) { LogError(logFile, "No path exist from map in template. No spec cert generated"); } else { LogInformation(logFile, string.Format("Paths used {0}", result.PathsUsed.Count)); specCertCount++; } if (result.Errors.Count != 0 || result.PathsUsed.Count == 0) { LogWarning(logFile, "Spec cert generated with errors"); } else { LogInformation(logFile, "Spec cert generated successfully"); } if (result.SpecCertGenerated) { ValidateAndUploadSpecCert(mapDetail, result, logFile, parameters.UploadSpecCert, parameters.SpecCertType); } if (result.Errors.Count > 0) { CreateErrorLogFile(fileName, mapDetail, result, logFile); } } catch (Exception e) { LogError(logFile, string.Format("Spec cert generation failed. Exception: {0}", e.ToString())); } } if (specCertCount == 0) { LogError(logFile, "No valid spec cert generated"); } } else { LogInformation(logFile, string.Format("No map present in {0}", fileName)); } } } catch (Exception ex) { LogError(logFile, string.Format("Error encountered during processing {0} file. Error: {1}", fileName, ex.ToString())); } } return(0); }