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 AddSection_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            bool isValid = true;
            if (lstEntities.SelectedItem == null)
            {
                MessageBox.Show("Select Entity to add field");
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtSectionName.Text.Trim()))
                isValid = false;

            if (isValid)
            {
                var entity = appModel.Entities.Where(a => a.Name.Equals(lstEntities.SelectedItem.ToString())).First();
                if (entity.AddScreenSections != null)
                {
                    if (entity.AddScreenSections.Any(s => s.SectionName.Equals(txtSectionName.Text.Trim())))
                        isValid = false;
                }

                if (isValid)
                {
                    TreeViewItem treeItem = new TreeViewItem();
                    treeItem.Header = this.txtSectionName.Text.Trim();

                    WireGenerator.Model.Section section = new WireGenerator.Model.Section();
                    section.SectionId = entity.AddScreenSections.Max(m => m.SectionId) + 1;
                    section.SectionName = txtSectionName.Text.Trim();
                    section.Zone = (cmbZone.SelectedIndex + 1).ToString();

                    if (entity.AddScreenSections == null)
                    {
                        entity.AddScreenSections = new List<WireGenerator.Model.Section>();
                        entity.AddScreenSections.Add(section);
                    }
                    else
                    {
                        var sectionExists = entity.AddScreenSections.Any(s => s.SectionName.Equals(txtSectionName.Text.Trim()));
                        if (!sectionExists)
                        {
                            entity.AddScreenSections.Add(section);
                        }
                    }

                    var treeNode = new TreeViewItemModel();
                    treeNode.Value = txtSectionName.Text.Trim();
                    treeNode.ParentId = section.SectionId;
                    rootNode.Items.Add(treeNode);

                    AddScreenSectionFieldsTreeView.ItemsSource = rootNode.Items;
                    txtSectionName.Clear();
                }
            }
        }
        private void Entity_Select(object sender, SelectionChangedEventArgs e)
        {
            var entity = appModel.Entities.Where(a => a.Name == lstEntities.SelectedItem.ToString()).First();
            txtEntityName.Text = entity.Name;
            txtEntityTitle.Text = entity.Title;
            chkEntitySearch.IsChecked = false;
            chkListWithSelector.IsChecked = false;

            if (entity.HasSearch == true)
                chkEntitySearch.IsChecked = true;

            if (entity.ListScreenFields != null)
                if (entity.IsListScreenWithSelector == true)
                    chkListWithSelector.IsChecked = true;
            lstListFields.ItemsSource = entity.ListScreenFields.Select(f => f.FieldName).ToList();
            lstListActions.ItemsSource = entity.ListScreenActions.Select(a => a.ActionName).ToList();

            if (entity.AddScreenSections != null)
            {
                if (entity.AddScreenSections.Count > 0)
                {
                    // reset root node for treeview
                    rootNode.Items.Clear();
                    AddScreenSectionFieldsTreeView.ItemsSource = null;
                    AddScreenSectionFieldsTreeView.Items.Clear();
                    AddScreenSectionFieldsTreeView.ItemsSource = rootNode.Items;

                    foreach (var section in entity.AddScreenSections)
                    {
                        TreeViewItemModel innerNode = new TreeViewItemModel();
                        innerNode.Value = section.SectionName;
                        innerNode.ParentId = section.SectionId;

                        foreach (var item in section.Fields.OrderBy(ob => ob.Index))
                        {
                            TreeViewItemModel childNode = new TreeViewItemModel(item.FieldName);
                            childNode.ParentId = section.SectionId;
                            innerNode.Items.Add(childNode);
                        }

                        rootNode.Items.Add(innerNode);
                    }
                }
            }

            lstPhases.ItemsSource = entity.Workflow.Select(w => w.PhaseName).ToList();
            if (!string.IsNullOrEmpty(entity.WorkflowAliasName))
                txtPhaseAliasName.Text = entity.WorkflowAliasName;
        }