Imports System.Reflection
Imports System.Xml
Imports System.Runtime.InteropServices
<Serializable()> Public Class HISNetwork
  Private Const _LoadXMLNode As String = "LoadXMLNode"   '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 _ToXMLNode As String = "ToXMLNode"   'Child Objects need to have LoadXMLNode method:LoadXMLNode(System.XmlNode)
  Private mLayerName As String
  Private mFilter As String      'filter would appears as: keyfield in (mKeyValues).
  Private mLayerIndex As Integer
  Private mKeyField As String
  Private mKeyValues As String   'value ready for filter, int appears as 1,2,3,4, string apears as 'a','b','c'.
  Private mExtraParams As String   'params may need
  Private mLayers As HISNetworks
  Private mTagName As String = "TagName"
  Private mName As String = "Name"
  Private mRecordsetXML As String = ""
  Private mDisplayFields As ApWebFields
  Private mParameterFields As ApWebFields
  Private mHISNetworks As HISNetworks
  Private mScript As String
  Private mWebServiceURL As String = ""
  Private mSiteCodeField As String = ""
  Private mDBConnection As String = ""
  Public Property DBConnection() As String
    Get
      Return mDBConnection
    End Get
    Set(ByVal value As String)
      mDBConnection = value
    End Set
  End Property

  Public Property SiteCodeField() As String
    Get
      Return mSiteCodeField
    End Get
    Set(ByVal value As String)
      mSiteCodeField = value
    End Set
  End Property
  Public Property WebServiceURL() As String
    Get
      Return mWebServiceURL
    End Get
    Set(ByVal value As String)
      mWebServiceURL = value
    End Set
  End Property
  Public Property Script() As String
    Get
      Return mScript
    End Get
    Set(ByVal value As String)
      mScript = value
    End Set
  End Property
  Public Property HISNetworks() As HISNetworks
    Get
      Return mHISNetworks
    End Get
    Set(ByVal Value As HISNetworks)
      mHISNetworks = Value
    End Set
  End Property
  Public Property ParameterFields() As ApWebFields
    Get
      Return mParameterFields
    End Get
    Set(ByVal Value As ApWebFields)
      mParameterFields = Value
    End Set
  End Property

  Public Property DisplayFields() As ApWebFields
    Get
      Return mDisplayFields
    End Get
    Set(ByVal Value As ApWebFields)
      mDisplayFields = Value
    End Set
  End Property
  Public Property RecordSetXML() As String
    Get
      Return mRecordsetXML
    End Get
    Set(ByVal Value As String)
      mRecordsetXML = Value
    End Set
  End Property
  Public Property TagName() As String
    Get
      If mTagName = "" Then
        mTagName = mLayerName
      End If
      Return mTagName
    End Get
    Set(ByVal Value As String)
      mTagName = Value
    End Set
  End Property
  Public Property Name() As String
    Get
      If mName = "" Then
        mName = mLayerName
      End If
      Return mName
    End Get
    Set(ByVal Value As String)
      mName = Value
    End Set
  End Property
  Public Property ExtraParams() As String
    Get
      Return mExtraParams
    End Get
    Set(ByVal Value As String)
      mExtraParams = Value
    End Set
  End Property
  Public Property LayerName() As String
    Get
      Return mLayerName
    End Get
    Set(ByVal Value As String)
      mLayerName = Value
    End Set
  End Property
  Public Property Filter() As String
    Get
      Return mFilter
    End Get
    Set(ByVal Value As String)
      mFilter = Value
    End Set
  End Property
  Public Property LayerIndex() As Integer
    Get
      Return mLayerIndex
    End Get
    Set(ByVal Value As Integer)
      mLayerIndex = Value
    End Set
  End Property
  Public Property KeyField() As String
    Get
      Return mKeyField
    End Get
    Set(ByVal Value As String)
      mKeyField = Value
    End Set
  End Property
  Public Property Layers() As HISNetworks
    Get
      Return mLayers
    End Get
    Set(ByVal Value As HISNetworks)
      mLayers = Value
    End Set
  End Property
  Public Property KeyValues() As String
    Get
      Return mKeyValues
    End Get
    Set(ByVal Value As String)
      mKeyValues = Value
    End Set
  End Property
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''  Write the layer to an xmlnode.
  ''' </summary>
  ''' <param name="pXMLDoc">xmlDocument</param>
  ''' <param name="pNodeParent">xmlnode - the node this layer node will append under</param>
  ''' <returns>xmlnode</returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/21/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function ToXMLNode(ByRef pXMLDoc As XmlDocument, ByVal pNodeParent As XmlNode) As XmlNode
    Dim pNode As XmlNode
    Dim pElem As XmlElement
    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 sName As String
    Dim nType As System.Reflection.MemberTypes
    Dim s As String = ""
    Dim bOk As Boolean
    Dim o As Object = Nothing
    Try
      pElem = pXMLDoc.CreateElement([GetType].Name)
      Try
        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 = pXMLDoc.CreateAttribute(sName)
                pAttribute.Value = s
                pElem.Attributes.Append(pAttribute)
              Else
                If Not o Is Nothing Then
                  Dim pMethodInfo As System.Reflection.MethodInfo
                  Dim oVals(1) As Object
                  pNode = pElem
                  pMethodInfo = o.GetType.GetMethod(_ToXMLNode)
                  If Not pMethodInfo Is Nothing Then
                    oVals(0) = pXMLDoc
                    oVals(1) = pNode
                    pMethodInfo.Invoke(o, BindingFlags.Instance, Nothing, oVals, Nothing)
                  End If

                  'If TypeOf o Is HISNetworks Then
                  '  cApLayers = CType(o, HISNetworks)
                  '  pNode = pElem
                  '  'cApLayers.ToXML(pXMLDoc, pNode)   '..Both call would have the same results because return values are not used.
                  '  cApLayers.ToXMLNode(pXMLDoc, pNode)
                  'End If
                  'If TypeOf o Is ApWebFields Then
                  '  cApWebFields = CType(o, ApWebFields)
                  '  pNode = pElem
                  '  'cApWebFields.ToXML(pXMLDoc, pNode)   '..Both call would have the same results because return values are not used.
                  '  cApWebFields.ToXMLNode(pXMLDoc, pNode)
                  'End If
                End If
              End If
            Catch ex As Exception
              'MsgBox("1. " & ex.ToString)
            End Try

          End If
        Next
      Catch ex As Exception
        'MsgBox("2. " & ex.ToString)
      End Try
      '..Get all the properties that is not object and add them as attributes for the 
      '..Get all the object properties of the layer and add it as child node.
      pNode = pElem
      If Not pNodeParent Is Nothing Then
        pNodeParent.AppendChild(pNode)
      Else
        pXMLDoc.AppendChild(pNode)
      End If
    Catch ex As Exception
      pNode = Nothing
    End Try
    Return pNode
  End Function
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''   Load properties from an xml node.
  ''' </summary>
  ''' <param name="pNode"></param>
  ''' <returns>true - if ok, false if not</returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/12/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function LoadXMLNode(ByVal pNode As XmlNode) As Boolean
    Dim pAttribute As XmlAttribute
    Dim pPropInfo As System.Reflection.PropertyInfo
    Dim pType As Type
    Dim oVals(0) As Object
    Dim intVals(0) As Integer
    Dim pChildNode As XmlNode
    Dim bOk As Boolean
    Dim pFieldInfo As System.Reflection.FieldInfo
    Dim pMethodInfo As System.Reflection.MethodInfo
    Try
      pType = Me.GetType
      For Each pAttribute In pNode.Attributes
        Try
          pPropInfo = Me.GetType.GetProperty(pAttribute.Name)
          oVals(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, oVals, 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, oVals, 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
      If pNode.HasChildNodes = True Then
        For Each pChildNode In pNode.ChildNodes
          Try
            oChild = Activator.CreateInstance(System.Type.GetType(pChildNode.Name))
          Catch ex As Exception
            oChild = Activator.CreateInstance(System.Type.GetType(ApSConfigAHS.getAppNameBase() & "." & pChildNode.Name))
          End 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(_Name)   '"Name")
              If Not pPropInfo Is Nothing Then
                sName = pPropInfo.GetValue(oChild, Nothing)
                pPropInfo = Me.GetType.GetProperty(sName)
                If Not pPropInfo Is Nothing Then
                  oVals(0) = oChild
                  pType.InvokeMember(sName, BindingFlags.Default Or BindingFlags.SetProperty, Nothing, Me, oVals, Nothing)
                End If
              End If
            End If
          End If
        Next
      End If
      bOk = True
    Catch ex As Exception
      bOk = False
    End Try
    Return bOk
  End Function
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''   given a key, return param value.
  ''' </summary>
  ''' <param name="sKey"></param>
  ''' <returns>sValue of the key</returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/21/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function GetExtraParam(ByVal sKey As String) As String
    Dim sReturn As String
    Dim htParams As Hashtable
    Try
      htParams = GetExtraParams()
      sReturn = htParams.Item(sKey)
    Catch ex As Exception
      sReturn = ""
    End Try
    Return sReturn
  End Function
  Public Function GetExcludedFields() As String()
    '*****************************************************************
    'Copyright © 2006 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description: GetExcludedFields in a WebLayer stored as ExtraParams
    '  ExcludedFields=Fld1,Fld2,Fld3 etc.
    'History: z.ye @4/21/2006 12:15:21 PM
    '*****************************************************************
    Dim ds As Long = Date.Now.Ticks
    Dim sFields() As String = Nothing
    Dim htParams As Hashtable
    Dim s As String
    Try
      htParams = GetExtraParams(";")
      If htParams.Count > 0 Then
        s = htParams.Item("ExcludedFields")
        If s <> "" Then
          sFields = s.Split(",")
        End If
      End If
    Catch ex As Exception

    End Try
    Return sFields
  End Function
  ''' -----------------------------------------------------------------------------
  ''' <summary>
  '''   Get extra params,
  ''' </summary>
  ''' <param name="ParamSep">Seperator, default ";"</param>
  ''' <param name="KeyValSep">KeyVal Seperator, default "="</param>
  ''' <returns>hashtable</returns>
  ''' <remarks>
  ''' </remarks>
  ''' <history>
  ''' 	[ye]	4/21/2006	Created
  ''' </history>
  ''' -----------------------------------------------------------------------------
  Public Function GetExtraParams(Optional ByVal ParamSep As String = ";", Optional ByVal KeyValSep As String = "=") As Hashtable
    '*****************************************************************
    'Copyright © 2006 ESRI. All Rights Reserved.
    '*****************************************************************
    'Description:
    'History: z.ye @4/21/2006 9:00:14 PM
    '*****************************************************************
    Dim ds As Long = Date.Now.Ticks
    Dim sValues As String
    Dim sKey As String
    Dim sVal As String
    Dim sVals() As String
    Dim htReturn As New Hashtable
    Dim sArr() As String
    Try
      If ParamSep.Trim = "" Then
        ParamSep = ";"
      End If
      If KeyValSep.Trim = "" Then
        KeyValSep = "="
      End If
      sArr = GetExtraParamsVals(ParamSep)
      If Not sArr Is Nothing Then
        For Each sValues In sArr
          sVals = sValues.Split(KeyValSep)
          If sVals.Length = 2 Then
            sKey = sVals(0)
            sVal = sVals(1)
            Try
              htReturn.Add(sKey, sVal)
            Catch ex As Exception

            End Try
          End If
        Next
      End If
    Catch ex As Exception

    End Try
    Return htReturn
  End Function
  Public Function GetExtraParamsVals(Optional ByVal sSep As String = ";") As String()
    Dim sArr() As String = Nothing
    Dim sExt As String
    Try
      sExt = ExtraParams
      If Trim(sExt) <> "" Then
        If sSep = "" Then
          sSep = ";"
        End If
        sArr = sExt.Split(sSep)
      End If
    Catch ex As Exception
      sArr = Nothing
    End Try
    Return sArr
  End Function

End Class

