Click here to Skip to main content
15,891,372 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 137.6K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Threading;
using System.Reflection;

using Harlinn.Oracle.DBTool.Example.Common;
using Harlinn.Oracle.DBTool.Example.Types;

namespace Harlinn.Oracle.DBTool.Example.Entities
{
    [Serializable]
    public partial class TagTypeEntity : EntityBase
    {

        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, MethodBase method)
        {
            Harlinn.Oracle.DBTool.Example.Common.Logger.LogException(sfLog, exc, method);
        }

        public event OnTagTypeEntityChangedDelegate OnChangedEvent;
        public event OnTagTypeEntityDeletedDelegate OnDeletedEvent;

        public TagTypeEntity(EntityContext entityContext)
            : base(entityContext)
        {

        }
        public TagTypeEntity(EntityContext entityContext, TagTypeElementData data)
            : base(entityContext, data)
        {

        }

        protected override void Disposing(bool disposing)
        {
            try
            {
                EntityContext constext = Context;
                if ((constext != null) && (!IsDisposed))
                {
                    constext.Unregister(this);
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
            }
            base.Disposing(disposing);
        }


        private TagTypeElementData Data
        {
            get
            {
                try
                {
                    return (TagTypeElementData)_Data;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        protected override ElementBase CloneData(ElementBase element)
        {
            try
            {
                TagTypeElementData result = (TagTypeElementData)CreateData();
                if (element != null)
                {
                    TagTypeElementData elementData = (TagTypeElementData)element;
                    elementData.AssignTo(result);
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        protected override ElementBase CreateData()
        {
            try
            {
                TagTypeElementData elementData = new TagTypeElementData();
                return elementData;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }
        protected override ElementBase SaveData()
        {
            try
            {
                TagTypeElementData result = null;
                TagTypeElementData data = Data;
                if (data != null)
                {
                    if ( data.Id == 0 )
                    {
                        result = Context._InsertTagType(data);
                    }
                    else
                    {
                        result = Context._UpdateTagType(data);
                    }
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public long Id
        {
            get
            {
                try
                {
                    if (EnterReadLock())
                    {
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data != null)
                            {
                                return data.Id;
                            }
                            else
                            {
                                return 0;
                            }
                        }
                        finally
                        {
                            ExitReadLock();
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
            set
            {
                try
                {
                    if (EnterWriteLock())
                    {
                        bool changed = false;
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data.Id == value)
                                return;
                            data.Id = value;
                            changed = true;
                        }
                        finally
                        {
                            ExitWriteLock();
                        }
                        if (changed)
                        {
                            OnPropertyChanged("Id");
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public string Name
        {
            get
            {
                try
                {
                    if (EnterReadLock())
                    {
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data != null)
                            {
                                return data.Name;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        finally
                        {
                            ExitReadLock();
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
            set
            {
                try
                {
                    if (EnterWriteLock())
                    {
                        bool changed = false;
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data.Name == value)
                                return;
                            data.Name = value;
                            changed = true;
                        }
                        finally
                        {
                            ExitWriteLock();
                        }
                        if (changed)
                        {
                            OnPropertyChanged("Name");
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public string Description
        {
            get
            {
                try
                {
                    if (EnterReadLock())
                    {
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data != null)
                            {
                                return data.Description;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        finally
                        {
                            ExitReadLock();
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
            set
            {
                try
                {
                    if (EnterWriteLock())
                    {
                        bool changed = false;
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data.Description == value)
                                return;
                            data.Description = value;
                            changed = true;
                        }
                        finally
                        {
                            ExitWriteLock();
                        }
                        if (changed)
                        {
                            OnPropertyChanged("Description");
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public string Comments
        {
            get
            {
                try
                {
                    if (EnterReadLock())
                    {
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data != null)
                            {
                                return data.Comments;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        finally
                        {
                            ExitReadLock();
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
            set
            {
                try
                {
                    if (EnterWriteLock())
                    {
                        bool changed = false;
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data.Comments == value)
                                return;
                            data.Comments = value;
                            changed = true;
                        }
                        finally
                        {
                            ExitWriteLock();
                        }
                        if (changed)
                        {
                            OnPropertyChanged("Comments");
                        }
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }


        public TagEntityList Tags
        {
            get
            {
                try
                {
                    long id = 0;
                    if (EnterReadLock())
                    {
                        try
                        {
                            TagTypeElementData data = Data;
                            if (data != null)
                            {
                                id = data.Id;
                            }
                        }
                        finally
                        {
                            ExitReadLock();
                        }
                        if( id != 0 )
                        {
                            EntityContext context = Context;
                            if( context != null )
                            {
                                TagEntityList result = context.GetTagByType( id );
                                return result;
                            }
                        }
                        return null;
                    }
                    else
                    {
                        throw new TimeoutException();
                    }
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }



        public void HandleChanged(OnTagTypeChangedEventArgs eventArgs )
        {
            try
            {
                if (EnterWriteLock())
                {
                    try
                    {
                        SetDataOnChangeNotification( eventArgs.TagType);
                    }
                    finally
                    {
                        ExitWriteLock();
                    }
                }

                if (OnChangedEvent != null)
                {
                    OnTagTypeEntityChangedEventArgs ea = new OnTagTypeEntityChangedEventArgs(eventArgs.ClientId, this);
                    OnChangedEvent(this, ea);
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod() );
                throw;
            }
        }


        public void HandleDeleted(OnTagTypeDeletedEventArgs eventArgs )
        {
            try
            {
                if (OnDeletedEvent != null)
                {
                    OnTagTypeEntityDeletedEventArgs ea = new OnTagTypeEntityDeletedEventArgs(eventArgs.ClientId, eventArgs.Id);
                    OnDeletedEvent(this, ea);
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod() );
                throw;
            }
        }


    }



    public class TagTypeEntityCache
    {
        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);
        }
        private WeakReference contextReference;
        private Dictionary<long, WeakReference> elements;
        public TagTypeEntityCache(EntityContext entityContext)
        {
            if (entityContext == null)
            {
                throw new ArgumentNullException("entityContext");
            }
            contextReference = new WeakReference(entityContext);
            elements = new Dictionary<long, WeakReference>();
        }
        public EntityContext Context
        {
            get
            {
                EntityContext result = (EntityContext)contextReference.Target;
                return result;
            }
        }
        public TagTypeEntity GetEntityFromCache(long id)
        {
            try
            {
                TagTypeEntity result = null;
                WeakReference entityReference = null;
                if (elements.TryGetValue(id, out entityReference))
                {
                    result = (TagTypeEntity)entityReference.Target;
                    if (result == null)
                    {
                        elements.Remove(id);
                    }
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }
        public TagTypeEntity GetEntity( long id)
        {
            try
            {
                TagTypeEntity result = GetEntityFromCache(id);
                if (result == null)
                {
                    EntityContext context = Context;
                    if (context != null)
                    {
                        TagTypeElementData data = context._GetTagTypeById(id);
                        if (data != null)
                        {
                            TagTypeEntity entity = new TagTypeEntity(context, data);
                            elements.Add(id, new WeakReference(entity));
                            result = entity;
                        }
                    }
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }
        public TagTypeEntity GetEntity(TagTypeElementData data )
        {
            try
            {
                TagTypeEntity result = GetEntityFromCache(data.Id);
                if (result == null)
                {
                    EntityContext context = Context;
                    if (context != null)
                    {
                        TagTypeEntity entity = new TagTypeEntity(context, data);
                        elements.Add(data.Id, new WeakReference(entity));
                        result = entity;
                    }
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }
        public bool RemoveEntity(long id)
        {
            try
            {
                return elements.Remove(id);
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


    }

    [Serializable]
    public class TagTypeEntityList : BindingList<TagTypeEntity>, ITypedList
    {
        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, MethodBase method)
        {
            Harlinn.Oracle.DBTool.Example.Common.Logger.LogException(sfLog, exc, method);
        }

        public TagTypeEntityList( )
        {
        }

        public TagTypeEntityList( IList<TagTypeEntity> entities )
            : base( entities )
        {
        }


        [NonSerialized()]
        private PropertyDescriptorCollection propertyDescriptorCollection;
        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            if (listAccessors != null && listAccessors.Length > 0)
            {
                PropertyDescriptorCollection result = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
                return result;
            }
            else
            {
                if (propertyDescriptorCollection == null)
                {
                    propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(TagTypeEntity), new Attribute[] { new BrowsableAttribute(true) });
                }
                return propertyDescriptorCollection;
            }
        }

        public string GetListName(PropertyDescriptor[] listAccessors)
        {
            string result = typeof(TagTypeEntity).Name;
            return result;
        }


    }


}

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