Imports System.Xml
Imports System.Reflection
<Serializable()> Public Class HISNetworks
  Inherits Hashtable
  Private Const _LoadXMLNode As String = "LoadXMLNode"   'Child Objects need to have LoadXMLNode method:LoadXMLNode(System.XmlNode)
  Private Const _ToXMLNode As String = "ToXMLNode"   'Child Objects need to have LoadXMLNode method:LoadXMLNode(System.XmlNode)
  Private Const _Name As String = "Name"   'Child object need to have Name property = the Name of the parent property refrencing the child object
  Private Const _TagName As String = "TagName"
  Private mExtraParams As String   'params may need
  Public Property ExtraParams() As String
    Get
      Return mExtraParams
    End Get
    Set(ByVal Value As String)
      mExtraParams = Value
    End Set
  End Property
  Public Function ToXMLString() As String
    Dim pXMLDoc As XmlDocument
    pXMLDoc = New XmlDocument
    pXMLDoc = ToXMLDoc(pXMLDoc)
    Return pXMLDoc.OuterXml
  End Function
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''   Write the HISNetworks to an XMLDoc, if ParentNode is provided, append the HISNetworksNode to the parentnode
  ''' </summary>
  ''' <param name="pDoc"> XMLDocument</param>
  ''' <returns>XMLDOC</returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/12/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function ToXMLDoc(Optional ByVal pDoc As XmlDocument = Nothing) As XmlDocument
    '*****************************************************************
    'Copyright © 2006 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description: 
    'History: z.ye @4/12/2006 9:09:27 AM
    '*****************************************************************
    Dim ds As Long = Date.Now.Ticks
    Try
      If pDoc Is Nothing Then
        pDoc = New XmlDocument
      End If
      If Not pDoc Is Nothing Then
        ToXMLNode(pDoc, Nothing)
      End If
    Catch ex As Exception

    End Try
    Return pDoc
  End Function
  ''' <summary>
  ''' Write the HISNetworks to an XMLDoc, if ParentNode is provided, append the HISNetworksNode to the parentnode
  ''' </summary>
  ''' <param name="pDoc"></param>
  ''' <param name="pNodeParent"></param>
  ''' <returns>XMLNode</returns>
  ''' <remarks></remarks>
  ''' <history>
  ''' 	[ye]	4/12/2006	Created
  ''' </history>
  Public Function ToXMLNode(ByVal pDoc As XmlDocument, ByVal pNodeParent As XmlNode) As XmlNode
    '*****************************************************************
    'Copyright © 2006 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description: Write the HISNetworks to an XMLDoc, 
    '  if ParentNode is provided, append the HISNetworksNode to the parentnode
    '  Returns the XMLNode
    'History: z.ye @4/12/2006 9:10:03 AM
    '*****************************************************************
    Dim ds As Long = Date.Now.Ticks
    Dim pNode As XmlNode = Nothing
    Dim pElem As XmlElement
    Dim sName As String
    '..GetMemebers info
    Dim pAttribute As XmlAttribute
    Dim pMembers() As System.Reflection.MemberInfo
    Dim pMemberInfo As System.Reflection.MemberInfo
    Dim pFieldInfo As System.Reflection.FieldInfo
    Dim pPropInfo As System.Reflection.PropertyInfo
    Dim pType As Type
    Dim nType As System.Reflection.MemberTypes
    Dim s As String = ""
    Dim o As Object = Nothing
    Dim bOk As Boolean

    If pDoc Is Nothing Then
      pDoc = New XmlDocument
    End If
    If Not pDoc Is Nothing Then
      sName = [GetType].Name
      pElem = pDoc.CreateElement(sName)
      pNode = pElem
      '..Get the attributes.
      pMembers = Me.GetType().GetMembers
      pType = Me.GetType
      For Each pMemberInfo In pMembers
        sName = pMemberInfo.Name
        nType = pMemberInfo.MemberType
        If pMemberInfo.MemberType = Reflection.MemberTypes.Property Or pMemberInfo.MemberType = MemberTypes.Field Then
          Try
            If pMemberInfo.MemberType = Reflection.MemberTypes.Property Then
              pPropInfo = Me.GetType.GetProperty(sName)
              o = pPropInfo.GetValue(Me, Nothing)  ' pType.InvokeMember(pMemberInfo.Name, Reflection.BindingFlags.GetProperty, Nothing, Me, Nothing, Nothing)
            ElseIf pMemberInfo.MemberType = Reflection.MemberTypes.Field Then
              pFieldInfo = Me.GetType.GetField(sName)
              o = pFieldInfo.GetValue(Me)
            End If
            bOk = ApSConfigAHS.IsTypeObject(o, s)
            If bOk = False Then
              pAttribute = pDoc.CreateAttribute(sName)
              pAttribute.Value = s
              pElem.Attributes.Append(pAttribute)
            Else

            End If
          Catch ex As Exception
            'MsgBox("1. " & ex.ToString)
          End Try
        End If
      Next
      If Not pNodeParent Is Nothing Then
        pNodeParent.AppendChild(pNode)
      Else
        pDoc.AppendChild(pNode)
      End If

      Dim oChild As Object
      Dim pMethodInfo As System.Reflection.MethodInfo
      Dim oVals(1) As Object
      For Each oChild In Me.Values
        pMethodInfo = oChild.GetType.GetMethod(_ToXMLNode)
        If Not pMethodInfo Is Nothing Then
          oVals(0) = pDoc
          oVals(1) = pNode
          pMethodInfo.Invoke(oChild, BindingFlags.Instance, Nothing, oVals, Nothing)
        End If
      Next
      'For Each pApLayer In Me.Values
      '  pNodeChild = pApLayer.ToXMLNode(pDoc, pNode)
      'Next
    End If
    Return pNode
  End Function
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''   Load from XMLNode, if pNode is nothing, then try to get it from the pDoc.
  ''' </summary>
  ''' <param name="pNode"></param>
  ''' <returns></returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/12/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function LoadXMLNode(ByVal pNode As XmlNode) As Boolean
    Dim bOk As Boolean = True
    Dim pAttribute As XmlAttribute
    Dim pPropInfo As System.Reflection.PropertyInfo
    Dim pFieldInfo As System.Reflection.FieldInfo
    Dim pType As Type
    Dim strVals(0) As String
    Dim pChildNode As XmlNode
    Try
      pType = Me.GetType
      For Each pAttribute In pNode.Attributes
        Try
          pPropInfo = Me.GetType.GetProperty(pAttribute.Name)
          strVals(0) = pAttribute.Value
          If Not pPropInfo Is Nothing Then
            If pPropInfo.CanWrite = True Then
              Try
                Select Case pPropInfo.PropertyType.Name
                  Case "Object"
                    pType.InvokeMember(pAttribute.Name, BindingFlags.Default Or BindingFlags.SetProperty, Nothing, Me, strVals, Nothing)
                  Case "String"
                    pPropInfo.SetValue(Me, CType(pAttribute.Value, String), Nothing)
                  Case "Int32", "Int16", "Integer", "Int64"
                    pPropInfo.SetValue(Me, CInt(pAttribute.Value), Nothing)
                  Case "Single"
                    pPropInfo.SetValue(Me, CType(pAttribute.Value, Single), Nothing)
                  Case "Double", "Decimal"
                    pPropInfo.SetValue(Me, CType(pAttribute.Value, Double), Nothing)
                  Case "String"
                    pPropInfo.SetValue(Me, pAttribute.Value, Nothing)
                  Case "DateTime"
                    pPropInfo.SetValue(Me, CType(pAttribute.Value, DateTime), Nothing)
                  Case "Boolean"
                    pPropInfo.SetValue(Me, CType(pAttribute.Value, Boolean), Nothing)
                End Select
                'System.Diagnostics.Debug.WriteLine(pPropInfo.Name & ", " & pPropInfo.GetSetMethod.Name & ", " & pPropInfo.GetHashCode & ", " & (pPropInfo.GetType.Equals(GetType(Integer))) & ", " & pPropInfo.GetType.Name)
              Catch ex As Exception
                'pPropInfo.SetValue(Me, CInt(pAttribute.Value), Nothing)
              End Try
            End If
          Else
            pFieldInfo = Me.GetType.GetField(pAttribute.Name)
            If Not pFieldInfo Is Nothing Then
              Try
                Select Case pFieldInfo.FieldType.Name
                  Case "Object"
                    pType.InvokeMember(pAttribute.Name, BindingFlags.Default Or BindingFlags.SetField, Nothing, Me, strVals, Nothing)
                  Case "String"
                    pFieldInfo.SetValue(Me, CType(pAttribute.Value, String))
                  Case "Int32", "Int16", "Integer", "Int64"
                    pFieldInfo.SetValue(Me, CInt(pAttribute.Value))
                  Case "Single"
                    pFieldInfo.SetValue(Me, CType(pAttribute.Value, Single))
                  Case "Double", "Decimal"
                    pFieldInfo.SetValue(Me, CType(pAttribute.Value, Double))
                  Case "String"
                    pFieldInfo.SetValue(Me, pAttribute.Value)
                  Case "DateTime"
                    pFieldInfo.SetValue(Me, CType(pAttribute.Value, DateTime))
                  Case "Boolean"
                    pFieldInfo.SetValue(Me, CType(pAttribute.Value, Boolean))
                End Select
              Catch ex As Exception

              End Try
            End If
          End If
        Catch ex As Exception
          System.Diagnostics.Debug.WriteLine(ex.ToString)
        End Try
      Next
      Dim oChild As Object
      Dim sName As String
      Dim pMethodInfo As System.Reflection.MethodInfo
      Dim oVals(0) As Object

      For Each pChildNode In pNode.ChildNodes
        Try
          oChild = Activator.CreateInstance(Type.GetType(pChildNode.Name))
        Catch ex As Exception
          oChild = Activator.CreateInstance(Type.GetType(getAppNameBase() & "." & pChildNode.Name))
        End Try
        Try
          If Not oChild Is Nothing Then
            pMethodInfo = oChild.GetType.GetMethod(_LoadXMLNode)   '  "LoadXMLNode")
            If Not pMethodInfo Is Nothing Then
              oVals(0) = pChildNode
              pMethodInfo.Invoke(oChild, BindingFlags.Instance, Nothing, oVals, Nothing)
              pPropInfo = oChild.GetType.GetProperty(_TagName)
              If pPropInfo Is Nothing Then
                pPropInfo = oChild.GetType.GetProperty(_Name)
              End If
              sName = ""
              If Not pPropInfo Is Nothing Then
                sName = pPropInfo.GetValue(oChild, Nothing)
              End If
              If sName = "" Then
                sName = oChild.GetType.Name
              End If
              Me.Add(sName, oChild)
            End If
          End If
        Catch ex As Exception

        End Try
        'pHISNetwork = New HISNetwork
        'pHISNetwork.LoadXMLNode(pChildNode)
        'Me.Add(pHISNetwork.TagName, pHISNetwork)
      Next
    Catch ex As Exception

    End Try
    Return bOk
  End Function
  Public Function LoadXMLDoc(ByVal pDoc As XmlDocument) As Boolean
    Dim pNode As XmlNode
    Dim s As String
    s = Me.GetType.Name
    pNode = pDoc.SelectSingleNode(s)  '"HISNetworks")
    Return LoadXMLNode(pNode)
  End Function
  Public Function LoadXMLFile(ByVal FileName As String) As Boolean
    Dim pDoc As XmlDocument
    pDoc = New XmlDocument
    pDoc.Load(FileName)
    Return LoadXMLDoc(pDoc)
  End Function
  Public Function LoadXMLString(ByVal strXML As String) As Boolean
    Dim pDoc As XmlDocument
    Dim bOk As Boolean
    Try
      pDoc = New XmlDocument
      pDoc.LoadXml(strXML)
      bOk = LoadXMLDoc(pDoc)
    Catch ex As Exception
      bOk = False
    End Try
    Return bOk
  End Function

  Public Sub New()
    MyBase.New()
  End Sub
  Public Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
    MyBase.New(info, context)
  End Sub
  Public Function getAppName() As String
    '*****************************************************************
    'Copyright © 2005 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description: get Application Name
    'History: ye @4/8/2005 10:31:44 AM
    '*****************************************************************
    Dim sLocation As String
    sLocation = System.Reflection.Assembly.GetExecutingAssembly.Location()
    sLocation = Right(sLocation, Len(sLocation) - InStrRev(sLocation, "\"))
    Return sLocation
  End Function
  Public Function getAppNameBase() As String
    Dim sName As String
    sName = getAppName()
    Try
      If sName.IndexOf(".") > 0 Then
        sName = sName.Substring(0, (sName.IndexOf(".")))
      End If
    Catch ex As Exception
    End Try
    Return sName
  End Function
  Public Function GetNetworkByName(ByVal sName As String) As HISNetwork
    '..Provide method to get HisNetwork by Name.   zye.
    Dim sKey As String
    Dim pHisNetwork As HISNetwork
    Dim pHisNetworkR As HISNetwork = Nothing
    Try
      sName = sName.Trim.ToUpper
      For Each sKey In Me.Keys
        pHisNetwork = Me.Item(sKey)
        If UCase(pHisNetwork.Name) = sName Then
          pHisNetworkR = pHisNetwork
          Exit For
        End If
      Next
    Catch ex As Exception
      pHisNetworkR = Nothing
    End Try
    Return pHisNetworkR
  End Function

  Public Function GetNetworkByLayerName(ByVal sLayerName As String) As HISNetwork
    '..Provide method to get HisNetwork by LayerName.   zye.
    Dim sKey As String
    Dim pHisNetwork As HISNetwork
    Dim pHisNetworkR As HISNetwork = Nothing
    Try
      sLayerName = sLayerName.Trim.ToUpper
      For Each sKey In Me.Keys
        pHisNetwork = Me.Item(sKey)
        If UCase(pHisNetwork.LayerName) = sLayerName Then
          pHisNetworkR = pHisNetwork
          Exit For
        End If
      Next
    Catch ex As Exception
      pHisNetworkR = Nothing
    End Try
    Return pHisNetworkR
  End Function

End Class

