예제 #1
0
        public IActionResult PostExcelToDmn(string inputs, string outputs, bool haveId)
        {
            var httpRequest = HttpContext.Request;

            if (httpRequest.Form.Files.Count != 1)
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error:", "Not file fount" }
                }));
            }
            var file = httpRequest.Form.Files[0];

            if (file != null)
            {
                ExcelPackage ep;
                try
                {
                    //Open Excel file
                    using (Stream excelFile = file.OpenReadStream())
                    {
                        ep = new ExcelPackage(excelFile);
                    }
                }
                catch (Exception e)
                {
                    return(BadRequest(new Dictionary <string, string>()
                    {
                        { "Error:", "Can't Open Excel File" }
                    }));
                }

                string dmnName;
                string dmnId;

                Dictionary <int, Dictionary <string, object> > inputsRulesFromExcel;
                Dictionary <int, Dictionary <string, object> > outputsRulesFromExcel;
                Dictionary <int, string> annotationsRulesDictionary;

                Dictionary <int, string> outputsRulesTypes;
                Dictionary <int, string> inputsRulesTypes;
                Dictionary <string, Dictionary <string, string> > inputsDictionary;
                Dictionary <string, Dictionary <string, string> > outputsDictionary;

                using (ExcelWorksheet worksheet = ep.Workbook.Worksheets.FirstOrDefault())
                {
                    if (worksheet == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName, "Can't find Excelsheet" }
                        }));
                    }

                    var table = worksheet.Tables.FirstOrDefault();

                    if (table == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName, "Excel file don't have a table" }
                        }));
                    }

                    //Fix cell were to set the information for the DMN table
                    dmnName = (string)(worksheet.Cells["C1"].Value ?? "DMN Table Name");
                    dmnId   = (string)(worksheet.Cells["C2"].Value ?? "dmnId");



                    var      columnsDictionary = GetTablesIndex(table, inputs, outputs);
                    string[] outputsIndex      = null;
                    string[] inputsIndex       = null;
                    if (columnsDictionary != null)
                    {
                        columnsDictionary.TryGetValue("outputsIndex", out outputsIndex);
                        columnsDictionary.TryGetValue("inputsIndex", out inputsIndex);
                    }

                    if (inputsIndex == null && outputsIndex == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error", "Can't get inputs/output rows" }
                        }));
                    }

                    try
                    {
                        inputsRulesFromExcel       = ExcelConverter.GetTableCellsAdressAndValue(worksheet, inputsIndex, haveId);
                        outputsRulesFromExcel      = ExcelConverter.GetTableCellsAdressAndValue(worksheet, outputsIndex, haveId);
                        annotationsRulesDictionary = ExcelConverter.GetTableAnnotationsCellsValue(worksheet, inputsIndex, outputsIndex, haveId);

                        inputsRulesTypes  = ExcelConverter.GetTableColumnsType(worksheet, inputsIndex, haveId);
                        outputsRulesTypes = ExcelConverter.GetTableColumnsType(worksheet, outputsIndex, haveId);

                        inputsDictionary  = ExcelConverter.GetTableHeader(worksheet, inputsIndex, haveId);
                        outputsDictionary = ExcelConverter.GetTableHeader(worksheet, outputsIndex, haveId);

                        if (!outputsRulesFromExcel.Any() || !inputsRulesFromExcel.Any() ||
                            !outputsRulesTypes.Any() || !inputsRulesTypes.Any() || !inputsDictionary.Any() ||
                            !outputsDictionary.Any())
                        {
                            return(BadRequest(new Dictionary <string, string>()
                            {
                                { "Error:", "Wrong information to create DMN from Excel" }
                            }));
                        }
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error:", "Can't Get Excel info" }
                        }));
                    }
                }



                var filename = Path.GetFileNameWithoutExtension(file.FileName);
                var newDmn   = new DmnV1Builder()
                               .AddDefinitionsInfo("Excel2Dmn_" + DateTime.Now.ToString("dd-mm-yy"), filename)
                               .AddDecision(dmnId, dmnName, "decisionTable")
                               .AddInputsToDecisionTable(inputsDictionary, inputsRulesTypes)
                               .AddOutputsToDecisionTable(outputsDictionary, outputsRulesTypes)
                               .AddDecisionRules(inputsRulesFromExcel, outputsRulesFromExcel, annotationsRulesDictionary)
                               .Build();

                // Create DMN Stream response
                try
                {
                    MemoryStream stream = new MemoryStream();
                    StreamWriter sw     = new StreamWriter(stream, Encoding.UTF8);

                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(DecisionModelNotation.Shema.tDefinitions));
                    xmlSerializer.Serialize(sw, newDmn);

                    stream.Position = 0;
                    return(File(stream, "aplication/dmn", $"{filename}.dmn"));
                }
                catch (Exception e)
                {
                    return(BadRequest(new Dictionary <string, string>()
                    {
                        { filename + ".dmn", "Unable Stream dmn file" }
                    }));
                }
            }
            else
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error", "Can't find any file" }
                }));
            }
        }