Click here to Skip to main content
15,886,055 members
Articles / Desktop Programming / WPF

DBTool for Oracle - Part 1

Rate me:
Please Sign up or sign in to vote.
4.92/5 (45 votes)
13 Apr 2014CPOL18 min read 136.1K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Harlinn.Oracle.DBTool.Example.Entities;

namespace Harlinn.Oracle.DBTool.Example.Client.Win
{
    public partial class MainForm : Form
    {
        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, MethodBase method)
        {
            Logger.LogException(sfLog, exc, method);
        }

        DataClient dataClient;
        EntityContext entityContext;

        public MainForm()
        {
            InitializeComponent();
            dataClient = new DataClient();
            entityContext = new EntityContext(dataClient);
            entityContext.SynchronizationControl = this;

            DefaultEntitySourceEntityContext.Context = entityContext;

        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            Utils.Logging.EventAppender.Instance.MessageLoggedEvent += Instance_MessageLoggedEvent;
            try
            {
                dataClient.Connect();
                RefreshNodes();
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
            }
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            try
            {
                dataClient.Disconnect();
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
            }
            Utils.Logging.EventAppender.Instance.MessageLoggedEvent -= Instance_MessageLoggedEvent;
        }


        delegate void Instance_MessageLoggedEventDelegate(object sender, Utils.Logging.MessageLoggedEventArgs e);

        void Instance_MessageLoggedEvent(object sender, Utils.Logging.MessageLoggedEventArgs e)
        {
            try
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Instance_MessageLoggedEventDelegate(Instance_MessageLoggedEvent), new object[] { sender, e });
                }
                else
                {
                    log4net.Core.LoggingEvent loggingEvent = e.LoggingEvent;

                    StringBuilder sb = new StringBuilder();
                    StringWriter sw = new StringWriter(sb);
                    using (sw)
                    {
                        loggingEvent.WriteRenderedMessage(sw);

                        sw.Flush();
                        sb.AppendLine();

                        string s = sb.ToString();
                        LogTextBox.AppendText(s);
                    }
                }
            }
            catch
            {
            }
        }


        // A bit of coding is required to get both nodes and
        // items into the treeview
        private void RefreshNodes()
        {
            try
            {
                nodesTreeView.Nodes.Clear();
                NodeEntityList nodes = entityContext.GetAllNodes();
                if (nodes.Count > 0)
                {
                    var rootNodes = from node in nodes
                                    where (node.Parent == null)
                                    orderby node.Name
                                    select node;


                    if (rootNodes != null)
                    {
                        foreach (NodeEntity rootNode in rootNodes)
                        {
                            TreeNode treeNode = nodesTreeView.Nodes.Add(rootNode.Id.ToString(), rootNode.Name);
                            treeNode.Tag = rootNode;
                            AddChildren(treeNode, rootNode);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
            }
        }

        private void AddChildren(TreeNode parentNode, NodeEntity entity)
        {
            TreeNode nodesNode = parentNode.Nodes.Add("Nodes");
            var childNodes = from node in entity.Nodes orderby node.Name select node;
            foreach (NodeEntity childNode in childNodes)
            {
                TreeNode treeNode = nodesNode.Nodes.Add(childNode.Id.ToString(), childNode.Name);
                treeNode.Tag = childNode;
                AddChildren(treeNode, childNode);
            }
            TreeNode itemsNode = parentNode.Nodes.Add("Items");
            var items = from item in entity.Items orderby item.Name select item;
            foreach (ItemEntity item in items)
            {
                TreeNode treeNode = itemsNode.Nodes.Add(item.Id.ToString(), item.Name);
                treeNode.Tag = item;
            }

        }

        private void nodesTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null && e.Node.Tag != null)
            {
                if (e.Node.Tag is ItemEntity)
                {
                    ItemEntity itemEntity = (ItemEntity)e.Node.Tag;
                    tagEntitySource.SetBindingList(itemEntity.Tags);
                }
            }

        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions