Ejemplo n.º 1
0
        private void ExtractMetaDataForAttribute(MetaReport metaReport, MetaReportAttributeModel model)
        {
            int tempi;

            XmlDocument meta = new XmlDocument();

            meta.LoadXml(metaReport.MetaDefinition);

            // Unpack structures
            XmlNode      rootNode   = meta.SelectSingleNode("//MetaReport");
            XmlAttribute typeAttr   = rootNode.Attributes["Type"];
            XmlAttribute entityAttr = rootNode.Attributes["CoreEntity"];

            var coreId = entityAttr != null?int.TryParse(entityAttr.Value, out tempi) ? Convert.ToInt32(entityAttr.Value) : 0 : 0;

            model.CoreEntity        = coreId;
            model.CoreEntityDisplay = _unitOfWork.Repository <MetaTable>().Queryable().Single(mt => mt.Id == coreId).TableName;
            var typeId = typeAttr != null?int.TryParse(typeAttr.Value, out tempi) ? Convert.ToInt32(typeAttr.Value) : 0 : 0;

            model.ReportType        = typeId;
            model.ReportTypeDisplay = typeId == 1 ? "List" : "Summary";

            XmlNode mainNode;

            // List or summary
            if (typeId == 1)
            {
                mainNode = rootNode.SelectSingleNode("//List");
                if (mainNode != null)
                {
                    foreach (XmlNode subNode in mainNode.ChildNodes)
                    {
                        MetaReportAttributeModel.ListItem list = new MetaReportAttributeModel.ListItem();
                        list.MetaColumnId  = Convert.ToInt32(subNode.Attributes["MetaColumnId"].Value);
                        list.AttributeName = subNode.Attributes.GetNamedItem("AttributeName").Value;
                        list.DisplayName   = subNode.Attributes.GetNamedItem("DisplayName").Value;
                        model.ListItems.Add(list);
                    }
                }
            }
            else
            {
                mainNode = rootNode.SelectSingleNode("//Summary");
                if (mainNode != null)
                {
                    foreach (XmlNode subNode in mainNode.ChildNodes)
                    {
                        MetaReportAttributeModel.ListItem strat = new MetaReportAttributeModel.ListItem();
                        strat.MetaColumnId  = Convert.ToInt32(subNode.Attributes["MetaColumnId"].Value);
                        strat.AttributeName = subNode.Attributes.GetNamedItem("AttributeName").Value;
                        strat.DisplayName   = subNode.Attributes.GetNamedItem("DisplayName").Value;
                        model.StratifyItems.Add(strat);
                    }
                }
            }

            // filter
            mainNode = rootNode.SelectSingleNode("//Filter");
            if (mainNode != null)
            {
                foreach (XmlNode subNode in mainNode.ChildNodes)
                {
                    MetaReportAttributeModel.FilterItem filter = new MetaReportAttributeModel.FilterItem();
                    filter.MetaColumnId  = Convert.ToInt32(subNode.Attributes["MetaColumnId"].Value);
                    filter.AttributeName = subNode.Attributes.GetNamedItem("AttributeName").Value;
                    filter.Operator      = subNode.Attributes.GetNamedItem("Operator").Value;
                    filter.Relation      = subNode.Attributes.GetNamedItem("Relation").Value;
                    model.FilterItems.Add(filter);
                }
            }
        }
Ejemplo n.º 2
0
        public ActionResult ReportAttributeItem(MetaReportAttributeModel model)
        {
            ViewBag.MenuItem = CurrentMenuItem;

            switch (model.ViewType)
            {
            case Models.ViewType.List:
                if (!String.IsNullOrWhiteSpace(model.DisplayForList))
                {
                    if (Regex.Matches(model.DisplayForList, @"[a-zA-Z ']").Count < model.DisplayForList.Length)
                    {
                        ModelState.AddModelError("DisplayForList", "Display contains invalid characters(Enter A-Z, a-z, space).");
                    }
                }

                break;

            case Models.ViewType.Summary:
                if (!String.IsNullOrWhiteSpace(model.DisplayForSummary))
                {
                    if (Regex.Matches(model.DisplayForSummary, @"[a-zA-Z ']").Count < model.ReportName.Length)
                    {
                        ModelState.AddModelError("DisplayForSummary", "Display contains invalid characters(Enter A-Z, a-z, space).");
                    }
                }

                break;

            case Models.ViewType.Filter:
                if (model.MetaColumnForFilterId == 0)
                {
                    ModelState.AddModelError("MetaColumnForFilterId", "Column must be selected.");
                }
                if (String.IsNullOrWhiteSpace(model.Operator))
                {
                    ModelState.AddModelError("Operator", "Operator must be selected.");
                }
                if (String.IsNullOrWhiteSpace(model.Relation))
                {
                    ModelState.AddModelError("Relation", "Relation must be selected.");
                }

                break;

            default:
                break;
            }

            if (ModelState.IsValid)
            {
                var metaReport = _unitOfWork.Repository <MetaReport>()
                                 .Queryable()
                                 .SingleOrDefault(r => r.Id == model.MetaReportId);

                ExtractMetaDataForAttribute(metaReport, model);

                // Add new item to relevant list
                switch (model.ViewType)
                {
                case Models.ViewType.List:
                    if (model.MetaColumnForListId > 0)
                    {
                        MetaReportAttributeModel.ListItem list = new MetaReportAttributeModel.ListItem();
                        list.MetaColumnId  = model.MetaColumnForListId;
                        list.AttributeName = _unitOfWork.Repository <MetaColumn>().Queryable().Single(mc => mc.Id == model.MetaColumnForListId).ColumnName;
                        list.DisplayName   = String.IsNullOrWhiteSpace(model.DisplayForList) ? list.AttributeName : model.DisplayForList;
                        model.ListItems.Add(list);
                    }

                    // Now save final definition
                    SaveDefinitionForAttribute(model, metaReport);

                    break;

                case Models.ViewType.Summary:
                    if (model.MetaColumnForSummaryId > 0)
                    {
                        MetaReportAttributeModel.ListItem strat = new MetaReportAttributeModel.ListItem();
                        strat.MetaColumnId  = model.MetaColumnForSummaryId;
                        strat.AttributeName = _unitOfWork.Repository <MetaColumn>().Queryable().Single(mc => mc.Id == model.MetaColumnForSummaryId).ColumnName;
                        strat.DisplayName   = String.IsNullOrWhiteSpace(model.DisplayForSummary) ? strat.AttributeName : model.DisplayForSummary;
                        model.StratifyItems.Add(strat);
                    }

                    // Now save final definition
                    SaveDefinitionForAttribute(model, metaReport);

                    break;

                case Models.ViewType.Filter:
                    if (model.MetaColumnForFilterId > 0)
                    {
                        MetaReportAttributeModel.FilterItem filter = new MetaReportAttributeModel.FilterItem();
                        filter.MetaColumnId  = model.MetaColumnForFilterId;
                        filter.AttributeName = _unitOfWork.Repository <MetaColumn>().Queryable().Single(mc => mc.Id == model.MetaColumnForFilterId).ColumnName;
                        filter.Operator      = model.Operator.ToString();
                        filter.Relation      = model.Relation.ToString();
                        model.FilterItems.Add(filter);
                    }

                    // Now save final definition
                    SaveDefinitionForAttribute(model, metaReport);

                    break;

                default:
                    break;
                }

                HttpCookie cookie = new HttpCookie("PopUpMessage");
                cookie.Value = "Column added successfully";
                Response.Cookies.Add(cookie);

                return(Redirect("/Reports/ReportAttributeItem?metaReportId=" + model.MetaReportId.ToString() + "&viewType=" + model.ViewType.ToString()));
            }

            IOrderedQueryable <MetaColumn> metaColumns = _unitOfWork.Repository <MetaColumn>().Queryable()
                                                         .Where(mc => mc.Table.Id == model.CoreEntity)
                                                         .OrderBy(mc => mc.ColumnName);
            List <SelectListItem> listMetaColumns = new List <SelectListItem>();

            // Add new item to relevant list
            switch (model.ViewType)
            {
            case Models.ViewType.List:
                foreach (MetaColumn metaColumn in metaColumns)
                {
                    // ensure not selected
                    if (!model.ListItems.Any(li => li.MetaColumnId == metaColumn.Id))
                    {
                        listMetaColumns.Add(new SelectListItem()
                        {
                            Text = metaColumn.ColumnName, Value = metaColumn.Id.ToString()
                        });
                    }
                }
                if (listMetaColumns.Count == 0)
                {
                    listMetaColumns.Add(new SelectListItem()
                    {
                        Text = "-- ALL COLUMNS ASSIGNED --", Value = "0"
                    });
                }

                break;

            case Models.ViewType.Summary:
                foreach (MetaColumn metaColumn in metaColumns)
                {
                    // ensure not selected
                    if (!model.StratifyItems.Any(li => li.MetaColumnId == metaColumn.Id))
                    {
                        listMetaColumns.Add(new SelectListItem()
                        {
                            Text = metaColumn.ColumnName, Value = metaColumn.Id.ToString()
                        });
                    }
                }
                if (listMetaColumns.Count == 0)
                {
                    listMetaColumns.Add(new SelectListItem()
                    {
                        Text = "-- ALL COLUMNS ASSIGNED --", Value = "0"
                    });
                }

                break;

            case Models.ViewType.Filter:
                if (metaColumns.Count() > 0)
                {
                    listMetaColumns.Add(new SelectListItem()
                    {
                        Text = "-- Please select a column --", Value = "0"
                    });
                    foreach (MetaColumn metaColumn in metaColumns)
                    {
                        // ensure not selected
                        if (!model.FilterItems.Any(li => li.MetaColumnId == metaColumn.Id))
                        {
                            listMetaColumns.Add(new SelectListItem()
                            {
                                Text = metaColumn.ColumnName, Value = metaColumn.Id.ToString()
                            });
                        }
                    }
                }
                if (listMetaColumns.Count == 0)
                {
                    listMetaColumns.Add(new SelectListItem()
                    {
                        Text = "-- ALL COLUMNS ASSIGNED --", Value = "0"
                    });
                }

                break;

            default:
                break;
            }

            ViewBag.MetaColumns = listMetaColumns;

            ViewBag.Relationships = new[]
            {
                new SelectListItem {
                    Value = "And", Text = "And", Selected = true
                },
                new SelectListItem {
                    Value = "Or", Text = "Or"
                }
            };

            ViewBag.Operators = new[]
            {
                new SelectListItem {
                    Value = "", Text = "-- Please select an operator --", Selected = true
                }
            };

            return(View(model));
        }