private void AddScreenAddField_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            bool isValid = true;

            var selectedNode = (TreeViewItemModel)AddScreenSectionFieldsTreeView.SelectedItem;
            if (selectedNode == null)
            {
                MessageBox.Show("Select Section Item");
                isValid = false;
            }

            if (isValid)
            {
                WireGenerator.Model.Section section;
                var entity = appModel.Entities.Where(a => a.Name.Equals(lstEntities.SelectedItem.ToString())).First();
                var entitySection = entity.AddScreenSections.Where(s => s.SectionName == selectedNode.Value).First();

                if (entitySection.Fields != null)
                {
                    if (entitySection.Fields.Any(a => a.FieldName.Equals(txtAddScreenFieldName.Text)))
                        isValid = false;
                }

                if (isValid)
                {

                    if (entitySection.Fields == null)
                        entitySection.Fields = new List<Field>();

                    var root = rootNode.Items.Where(s => s.ParentId == selectedNode.ParentId).Single();
                    var parentNodeIndex = rootNode.Items.IndexOf(root);
                    var fieldNode = rootNode.Items.ElementAt(parentNodeIndex).Items.LastOrDefault();
                    var tabIndex = (fieldNode != null) ? rootNode.Items.ElementAt(parentNodeIndex).Items.IndexOf(fieldNode) + 1 : default(int);

                    Field field = new Field();
                    field.FieldName = txtAddScreenFieldName.Text.Trim();
                    field.Type = cmbAddScreenFieldType.SelectedIndex;
                    field.Index = tabIndex;
                    section = new Model.Section();
                    entitySection.Fields.Add(field);

                    var treeNode = new TreeViewItemModel(field.FieldName);
                    treeNode.ParentId = entitySection.SectionId;
                    selectedNode.Items.Add(treeNode);

                    txtAddScreenFieldName.Clear();
                }
            }
        }
        private void cmbSchemaNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbSchemaNames.SelectedIndex == 0)
            {
                txtSchemaName.Text = "Enter Configuration Name";
                txtSchemaName.Visibility = System.Windows.Visibility.Visible;
                txtAppName.Clear();
                txtAppUserName.Clear();
            }
            else
                txtSchemaName.Visibility = System.Windows.Visibility.Hidden;

            if (cmbSchemaNames.SelectedIndex > 0)
            {
                string executableLocation = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                appModel = new AppModel();
                appModel.Navigation = new List<WireGenerator.Model.MenuItem>();
                schemaName = cmbSchemaNames.SelectedItem.ToString();
                schemaAbsoluteFileName = System.IO.Path.Combine(executableLocation, cmbSchemaNames.SelectedItem.ToString() + "_appConfig.xml");
                if (File.Exists(schemaAbsoluteFileName))
                {
                    appConfig = XElement.Load(schemaAbsoluteFileName);
                    #region load appModel from xml configuration
                    if (!appConfig.IsEmpty)
                    {
                        txtAppName.Text = appConfig.Element("Name").Value;
                        txtAppUserName.Text = appConfig.Element("LoginUser").Value;

                        //navigation
                        var navigation = new XElement("Navigation");
                        TreeViewItem node, subnode;
                        WireGenerator.Model.MenuItem menuItem;
                        IEnumerable<XElement> navMenuItems = from el in appConfig.Elements("Navigation").Elements("MenuItem") select el;
                        if (navMenuItems.ToList().Count > 0)
                        {
                            foreach (XElement el in navMenuItems.ToList())
                            {
                                menuItem = new WireGenerator.Model.MenuItem();
                                menuItem.Name = el.Attribute("name").Value;
                                menuItem.LinkToUrl = (el.Attribute("linkTo") != null) ? el.Attribute("linkTo").Value : string.Empty;

                                node = new TreeViewItem();
                                if (string.IsNullOrEmpty(menuItem.LinkToUrl))
                                    node.Header = el.Attribute("name").Value;
                                else
                                    node.Header = el.Attribute("name").Value + "(" + menuItem.LinkToUrl + ")";
                                IEnumerable<XElement> menuSubItems = el.Descendants();
                                menuItem.MenuSubItems = new List<MenuSubItem>();
                                foreach (XElement subel in menuSubItems)
                                {
                                    menuSubItem = new MenuSubItem();
                                    menuSubItem.SubItem = subel.Value;
                                    menuSubItem.LinkToURL = (subel.Attribute("linkTo") != null) ? subel.Attribute("linkTo").Value : string.Empty;
                                    menuItem.MenuSubItems.Add(menuSubItem);

                                    subnode = new TreeViewItem();
                                    if (string.IsNullOrEmpty(menuSubItem.LinkToURL))
                                        subnode.Header = subel.Value;
                                    else
                                        if (menuSubItem.LinkToURL.Contains("_"))
                                            subnode.Header = subel.Value + " (" + (menuSubItem.LinkToURL).Substring(0, menuSubItem.LinkToURL.IndexOf("_")) + ")";
                                        else
                                            subnode.Header = subel.Value + " (" + (menuSubItem.LinkToURL) + ")";

                                    node.Items.Add(subnode);
                                }
                                NavigationTreeView.Items.Add(node);
                                appModel.Navigation.Add(menuItem);
                            }
                        }

                        //entities
                        IEnumerable<XElement> xEntities = from el in appConfig.Elements("Entities").Elements("Entity") select el;
                        appModel.Entities = new List<Entity>();
                        if (xEntities.ToList().Count > 0)
                        {
                            Entity entity;
                            Field field;
                            WorkflowPhase phase;
                            WireGenerator.Model.Action action;
                            WireGenerator.Model.Section section;
                            XElement xElement;
                            XAttribute xListFieldsHasSelector;
                            IEnumerable<XElement> xElements, xFields;
                            IEnumerable<XElement> xListFields;

                            foreach (XElement xEntity in xEntities.ToList())
                            {
                                entity = new Entity();
                                entity.Name = xEntity.Attribute("name").Value;
                                entity.Title = xEntity.Attribute("title").Value;
                                entity.HasSearch = (xEntity.Attribute("hasSearch").Value == "true") ? true : false;

                                //load list screen fields
                                xListFieldsHasSelector = (from a in xEntity.Elements("ListFields").Attributes("WithSelector") select a).FirstOrDefault();
                                if (xListFieldsHasSelector != null)
                                    entity.IsListScreenWithSelector = Convert.ToBoolean(xListFieldsHasSelector.Value);

                                entity.ListScreenFields = new List<Field>();
                                if (xEntity.Elements("ListFields").Count() > 0)
                                {
                                    xListFields = from a in xEntity.Elements("ListFields").Descendants() select a;
                                    foreach (XElement xListField in xListFields.ToList())
                                    {
                                        field = new Field();
                                        field.FieldName = xListField.Value;
                                        field.Index = Convert.ToInt32(xListField.Attribute("index").Value);
                                        field.DataType = Convert.ToInt32(xListField.Attribute("datatype").Value);
                                        entity.ListScreenFields.Add(field);
                                    }
                                }

                                //load list screen actions
                                entity.ListScreenActions = new List<WireGenerator.Model.Action>();
                                if (xEntity.Elements("ListActions").Count() > 0)
                                {
                                    xElements = from a in xEntity.Elements("ListActions").Descendants() select a;
                                    foreach (XElement xListAction in xElements)
                                    {
                                        action = new WireGenerator.Model.Action();
                                        action.ActionName = xListAction.Value;
                                        action.LinkTo = xListAction.Attribute("linkTo").Value;
                                        entity.ListScreenActions.Add(action);
                                    }
                                }

                                //load add screen sections and fields
                                if (xEntity.Elements("AddFields").Count() > 0)
                                {
                                    xElements = from a in xEntity.Elements("AddFields").Elements("Section") select a;
                                    entity.AddScreenSections = new List<WireGenerator.Model.Section>();
                                    foreach (XElement xSection in xElements)
                                    {
                                        section = new Model.Section();
                                        section.SectionId = Convert.ToInt32(xSection.Attribute("id").Value);
                                        section.SectionName = xSection.Attribute("name").Value;
                                        section.Zone = xSection.Attribute("zone").Value;

                                        xFields = from a in xSection.Descendants() select a;
                                        section.Fields = new List<Field>();
                                        foreach (XElement xfield in xFields)
                                        {
                                            field = new Model.Field();
                                            field.FieldName = xfield.Attribute("label").Value;
                                            field.Index = Convert.ToInt32(xfield.Attribute("index").Value);
                                            field.Type = Convert.ToInt32(xfield.Attribute("type").Value);
                                            section.Fields.Add(field);
                                        }
                                        entity.AddScreenSections.Add(section);
                                    }
                                }

                                //load workflow
                                entity.Workflow = new List<WorkflowPhase>();
                                if (xEntity.Elements("Workflow").Count() > 0)
                                {
                                    xElement = (from a in xEntity.Elements("Workflow") select a).FirstOrDefault();
                                    if (xElement != null)
                                    {
                                        entity.WorkflowAliasName = xElement.Attribute("name").Value;
                                        xElements = from a in xEntity.Elements("Workflow").Descendants() select a;
                                        foreach (XElement xPhase in xElements)
                                        {
                                            phase = new WorkflowPhase();
                                            phase.PhaseName = xPhase.Value;
                                            entity.Workflow.Add(phase);
                                        }
                                    }
                                }
                                appModel.Entities.Add(entity);
                                lstEntities.Items.Add(entity.Name);
                            }
                        }
                    }
                    #endregion
                }
            }
        }
        private void AddListField_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Field field = new Field();
            if (!string.IsNullOrEmpty(txtListFieldName.Text.Trim()))
            {
                field.FieldName = txtListFieldName.Text.Trim();
                field.DataType = cmbListFieldDataType.SelectedIndex;

                bool isValid = true;
                if (lstEntities.SelectedItem == null)
                {
                    MessageBox.Show("Select Entity to add field");
                    isValid = false;
                }

                if (lstListFields.Items.Contains(field.FieldName.Trim()))
                    isValid = false;

                if (isValid)
                {
                    var selectedEntityName = lstEntities.SelectedItem;
                    Entity entity = appModel.Entities.Where(a => a.Name.Equals(selectedEntityName)).First();
                    if (entity.ListScreenFields == null)
                        entity.ListScreenFields = new List<Field>();

                    entity.ListScreenFields.Add(field);
                    lstListFields.ItemsSource = entity.ListScreenFields.Select(a => a.FieldName).ToList();
                    txtListFieldName.Clear();
                }
            }
        }