예제 #1
0
        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();
        }