public void Test1() { var file = "dmnTest1.dmn"; string ifcDataFile = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\TestData\", file); tDefinitions dmn; using (Stream dmnStream = File.Open(ifcDataFile, FileMode.Open)) { dmn = new DmnServices().DeserializeStreamDmnFile(dmnStream); } var Items = dmn.Items; var decision = Items.Where(t => t.GetType() == typeof(tDecision)); var excelPkg = new ExcelPackage(); foreach (var tdecision in decision) { tDecisionTable decisionTable = null; try { var dt = ((tDecision)tdecision).Item; decisionTable = (tDecisionTable)Convert.ChangeType(dt, typeof(tDecisionTable)); ExcelWorksheet wsSheet = excelPkg.Workbook.Worksheets.Add(tdecision.id); //Add Table Title ExcelServices.AddTableTitle(tdecision.name, wsSheet, decisionTable, tdecision.id); // Add "input" and "output" headet to Excel table ExcelServices.AddTableInputOutputTitle(wsSheet, decisionTable); //Add DMN Table to excel Sheet ExcelServices.CreateExcelTableFromDecisionTable(decisionTable, wsSheet, tdecision.id); } catch { // } } var filename = Path.GetFileNameWithoutExtension(ifcDataFile); var path = string.Concat(@"c:\temp\"); Directory.CreateDirectory(path); var filePath = Path.Combine(path, string.Concat(filename, ".xlsx")); excelPkg?.SaveAs(new FileInfo(filePath)); File.Exists(filePath).Should().BeTrue(); }
private string ParseCellValue(object cellValue) { //cellValue = cellValue.ToString().Trim(); if (cellValue == null || string.IsNullOrEmpty(cellValue.ToString())) { return(string.Empty); } //if (string.IsNullOrEmpty(cellValue.ToString()))return String.Empty; string cellValueString = DmnServices.GetComparisonNumber(cellValue.ToString()) ?? cellValue.ToString(); var cellRangeNumber = DmnServices.GetRangeNumber(cellValue.ToString()); if (cellRangeNumber != null) { var type1 = ParseCellValue(cellRangeNumber[0]); var type2 = ParseCellValue(cellRangeNumber[1]); if (type1 != type2) { return(GetType(type1, type2)); } cellValueString = cellRangeNumber[0]; } if (int.TryParse(cellValueString, out var intType)) { return("integer"); } if (long.TryParse(cellValueString, out var longType)) { return("long"); } if (double.TryParse(cellValueString, out var doubleType)) { return("double"); } if (bool.TryParse(cellValueString, out var booleanType)) { return("boolean"); } return("string"); }
public IActionResult PosDmnToExcel() { var httpRequest = HttpContext.Request; HttpResponseMessage response = null; string okResponsText = null; var httpFiles = httpRequest.Form.Files; var okDictionary = new Dictionary <string, string>(); var ErrorDictionary = new Dictionary <string, string>(); if (httpFiles == null && !httpFiles.Any()) { return(NotFound("Can't find any file")); } for (var i = 0; i < httpFiles.Count; i++) { string errorResponsText = null; string errorTemp = string.Empty; var file = httpFiles[i]; tDefinitions dmn = null; //Deserialize DMN file if (file != null) { using (Stream dmnfile = httpFiles[i].OpenReadStream()) { dmn = new DmnServices().DeserializeStreamDmnFile(dmnfile); } } if (dmn == null) { ErrorDictionary.Add(file.FileName, "Can't validate Shema"); continue; } // check if DMN have desicion table var items = dmn.Items; var decision = items.Where(t => t.GetType() == typeof(tDecision)); var tDrgElements = decision as tDRGElement[] ?? decision.ToArray(); if (!tDrgElements.Any()) { ErrorDictionary.Add(file.FileName, "Dmn file have non decision"); continue; } // create Excel Package ExcelPackage excelPkg = null; try { excelPkg = new ExcelPackage(); foreach (var tdecision in tDrgElements) { tDecisionTable decisionTable = null; try { var dt = ((tDecision)tdecision).Item; decisionTable = (tDecisionTable)Convert.ChangeType(dt, typeof(tDecisionTable)); ExcelWorksheet wsSheet = excelPkg.Workbook.Worksheets.Add(tdecision.id); //Add Table Title ExcelServices.AddTableTitle(tdecision.name, wsSheet, decisionTable, tdecision.id); // Add "input" and "output" headet to Excel table ExcelServices.AddTableInputOutputTitle(wsSheet, decisionTable); //Add DMN Table to excel Sheet ExcelServices.CreateExcelTableFromDecisionTable(decisionTable, wsSheet, tdecision.id); } catch { ErrorDictionary.Add(file.FileName, string.Concat("Dmn: ", tdecision.name, " Can't be create")); } } } catch { ErrorDictionary.Add(file.FileName, "Can't create Excel file"); continue; } // Save Excel Package try { var filename = Path.GetFileNameWithoutExtension(file.FileName); var path = Path.Combine(@"C:\", "DmnToExcel"); Directory.CreateDirectory(path); excelPkg.SaveAs(new FileInfo(Path.Combine(path, string.Concat(filename, ".xlsx")))); okDictionary.Add(file.FileName, "Created in:" + path); } catch { ErrorDictionary.Add(file.FileName, "Can't be saved"); } } if (ErrorDictionary.Any()) { if (okDictionary.Any()) { List <Dictionary <string, string> > dictionaries = new List <Dictionary <string, string> >(); dictionaries.Add(okDictionary); dictionaries.Add(ErrorDictionary); var result = dictionaries.SelectMany(dict => dict) .ToLookup(pair => pair.Key, pair => pair.Value) .ToDictionary(group => group.Key, group => group.First()); return(Ok(result)); } return(BadRequest(ErrorDictionary)); } return(Ok(okDictionary)); }
public IActionResult PostModelsToExcelDataDictionary() { var httpRequest = HttpContext.Request; HttpResponseMessage response = null; string okResponsText = null; var httpFiles = httpRequest.Form.Files; var httpForms = httpRequest.Form.Keys; var okDictionary = new Dictionary <string, string>(); var ErrorDictionary = new Dictionary <string, string>(); var dmnDataDictionaryModels = new List <DmnDataDictionaryModel>(); var bpmnDataDictionaryModels = new List <BpmnDataDictionaryModel>(); var dataDictionaryModels = new List <DataDictionaryModel>(); if (httpFiles == null && !httpFiles.Any()) { return(NotFound("Can't find any file")); } for (var i = 0; i < httpFiles.Count; i++) { string errorResponsText = null; string errorTemp = string.Empty; var file = httpFiles[i]; tDefinitions dmn = null; var fileExtention = Path.GetExtension(file.FileName); if (fileExtention == ".dmn") { //Deserialize DMN file if (file != null) { using (Stream dmnfile = httpFiles[i].OpenReadStream()) { dmn = new DmnServices().DeserializeStreamDmnFile(dmnfile); } } if (dmn == null) { ErrorDictionary.Add(file.FileName, "Can't validate Shema"); continue; } // check if DMN have desicion table var items = dmn.Items; var decision = items.Where(t => t.GetType() == typeof(tDecision)); var tDrgElements = decision as tDRGElement[] ?? decision.ToArray(); if (!tDrgElements.Any()) { ErrorDictionary.Add(file.FileName, "Dmn file have non decision"); continue; } foreach (tDecision tdecision in decision) { tDecisionTable decisionTable = null; try { DmnServices.GetDecisionsVariables(tdecision, Path.GetFileNameWithoutExtension(file.FileName), ref dmnDataDictionaryModels); } catch { ErrorDictionary.Add(file.FileName, "Can't add serialize info from DMN"); } } } if (fileExtention == ".bpmn") { XDocument bpmnXml = null; try { using (Stream dmnfile = httpFiles[i].OpenReadStream()) { bpmnXml = XDocument.Load(dmnfile); } } catch { ErrorDictionary.Add(file.FileName, "Can't add serialize bpmn to xml"); } if (bpmnXml != null) { try { DmnServices.GetDmnInfoFromBpmnModel(bpmnXml, ref bpmnDataDictionaryModels); } catch { ErrorDictionary.Add(file.FileName, "Can't add serialize bpmn to Data Model Dictionary"); } } } } foreach (var dmnDataInfo in dmnDataDictionaryModels) { var submodel = new BpmnDataDictionaryModel(); try { submodel = bpmnDataDictionaryModels.Single(b => b.DmnId == dmnDataInfo.DmnId); } catch { } dataDictionaryModels.Add(new DataDictionaryModel() { BpmnData = submodel, DmnData = dmnDataInfo }); } // create Excel Package ExcelPackage excelPkg = null; var fileName = "DataDictionaryFromModels"; try { excelPkg = new ExcelPackage(); ExcelWorksheet wsSheet = excelPkg.Workbook.Worksheets.Add("DmnTEK"); var dmnIds = dmnDataDictionaryModels.GroupBy(x => x.DmnId).Select(y => y.First()); var objectPropertyNames = new[] { "DmnId", "DmnNavn", "TekKapitel", "TekLedd", "TekTabell", "TekForskriften", "TekWebLink" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnIds, wsSheet, "dmnTek", objectPropertyNames); ExcelWorksheet wsSheet1 = excelPkg.Workbook.Worksheets.Add("Variables"); var dmnVariablesIds = dmnDataDictionaryModels.GroupBy(x => x.VariabelId).Select(y => y.First()); var dmnVariablesIdstPropertyNames = new[] { "VariabelId", "VariabelNavn", "VariabelBeskrivelse" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnVariablesIds, wsSheet1, "Variables", dmnVariablesIdstPropertyNames); ExcelWorksheet wsSheet2 = excelPkg.Workbook.Worksheets.Add("Dmn+Variables"); var objectPropertyNames1 = new[] { "DmnId", "VariabelId", "Type" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnDataDictionaryModels, wsSheet2, "Dmn+Variables", objectPropertyNames1); ExcelWorksheet wsSheet3 = excelPkg.Workbook.Worksheets.Add("summary"); var summaryPropertyNames = new[] { "DmnData.FilNavn", "BpmnData.BpmnId", "DmnData.DmnId", "DmnData.VariabelId", "DmnData.VariabelType", "DmnData.Type", "DmnData.Kilde" }; ExcelServices.CreateSummaryExcelTableDataDictionary(dataDictionaryModels, wsSheet3, "summary", summaryPropertyNames); } catch { ErrorDictionary.Add("Error", "Can't create Excel file"); } // Save Excel Package try { var path = Path.Combine(@"C:\", "DmnToExcel"); Directory.CreateDirectory(path); excelPkg.SaveAs(new FileInfo(Path.Combine(path, string.Concat(fileName, ".xlsx")))); okDictionary.Add(fileName, "Created in:" + path); } catch { ErrorDictionary.Add(fileName, "Can't be saved"); } if (ErrorDictionary.Any()) { if (okDictionary.Any()) { List <Dictionary <string, string> > dictionaries = new List <Dictionary <string, string> >(); dictionaries.Add(okDictionary); dictionaries.Add(ErrorDictionary); var result = dictionaries.SelectMany(dict => dict) .ToLookup(pair => pair.Key, pair => pair.Value) .ToDictionary(group => group.Key, group => group.First()); return(Ok(result)); } return(BadRequest(ErrorDictionary)); } return(Ok(okDictionary)); }
public void Test1() { var file = "dmnTest1.dmn"; var file2 = "dmnTest2.dmn"; var file3 = "BpmnTest01.bpmn"; var dmns = new List <tDefinitions>(); var filePath1 = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\TestData\", file); var filePath2 = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\TestData\", file2); var bpmn = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\TestData\", file3); XDocument bpmnXml = XDocument.Load(bpmn); using (Stream dmnStream = File.Open(filePath1, FileMode.Open)) { dmns.Add(new DmnServices().DeserializeStreamDmnFile(dmnStream)); } using (Stream dmnStream = File.Open(filePath2, FileMode.Open)) { dmns.Add(new DmnServices().DeserializeStreamDmnFile(dmnStream)); } var dmnDataDictionaryModels = new List <DmnDataDictionaryModel>(); var excelPkg = new ExcelPackage(); foreach (var tdefinitions in dmns) { var Items = tdefinitions.Items; var decision = Items.Where(t => t.GetType() == typeof(tDecision)); foreach (tDecision tdecision in decision) { tDecisionTable decisionTable = null; try { DmnServices.GetDecisionsVariables(tdecision, Path.GetFileNameWithoutExtension(filePath1), ref dmnDataDictionaryModels); } catch { // } } } var bpmnDataDictionary = new List <BpmnDataDictionaryModel>(); DmnServices.GetDmnInfoFromBpmnModel(bpmnXml, ref bpmnDataDictionary); List <DataDictionaryModel> dataDictionaryModels = new List <DataDictionaryModel>(); foreach (var dmnData in dmnDataDictionaryModels) { var submodel = new BpmnDataDictionaryModel(); try { var value = dmnData.GetType(); var property = value.GetProperty("DmnId"); String name = (String)(property.GetValue(dmnData, null)); submodel = bpmnDataDictionary.Single(b => b.DmnId == "sdsds"); } catch { } dataDictionaryModels.Add(new DataDictionaryModel() { BpmnData = submodel, DmnData = dmnData }); } ExcelWorksheet wsSheet = excelPkg.Workbook.Worksheets.Add("DmnTEK"); var dmnIds = dmnDataDictionaryModels.GroupBy(x => x.DmnId).Select(y => y.First()); var objectPropertyNames = new[] { "DmnId", "DmnNavn", "TekKapitel", "TekLedd", "TekTabell", "TekForskriften", "TekWebLink" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnIds, wsSheet, "dmnTek", objectPropertyNames); ExcelWorksheet wsSheet1 = excelPkg.Workbook.Worksheets.Add("Variables"); var dmnVariablesIds = dmnDataDictionaryModels.GroupBy(x => x.VariabelId).Select(y => y.First()); var dmnVariablesIdstPropertyNames = new[] { "VariabelId", "VariabelNavn", "VariabelBeskrivelse" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnVariablesIds, wsSheet1, "Variables", dmnVariablesIdstPropertyNames); ExcelWorksheet wsSheet2 = excelPkg.Workbook.Worksheets.Add("Dmn+Variables"); var objectPropertyNames1 = new[] { "DmnId", "VariabelId", "Type" }; ExcelServices.CreateDmnExcelTableDataDictionary(dmnDataDictionaryModels, wsSheet2, "Dmn+Variables", objectPropertyNames1); ExcelWorksheet wsSheet3 = excelPkg.Workbook.Worksheets.Add("summary"); var summaryPropertyNames = new[] { "DmnData.FilNavn", "BpmnData.BpmnId", "DmnData.DmnId", "DmnData.VariabelId", "DmnData.VariabelType", "DmnData.Type", "DmnData.Kilde" }; ExcelServices.CreateSummaryExcelTableDataDictionary(dataDictionaryModels, wsSheet3, "summary", summaryPropertyNames); var path = string.Concat(@"c:\temp\"); Directory.CreateDirectory(path); var filePath = Path.Combine(path, string.Concat("dataDictionary", ".xlsx")); excelPkg?.SaveAs(new FileInfo(filePath)); File.Exists(filePath).Should().BeTrue(); }