Public Class Example
  Implements EarthSoft.EDGE.ICustomHandlerSysSampleCode


#Region "Data Members"

  Private Const COLUMN_SYS_LOC_CODE As String = "sys_loc_code"
  Private Const COLUMN_SAMPLE_MATRIX_CODE As String = "sample_matrix_code"
  Private _Efd As EarthSoft.EDP.EddFormatDefinition
  Private _Ch As EarthSoft.EDP.EddCustomHandler

#End Region


#Region "Methods needed for plugin handler object"


  Public Sub BeforeAddDataHandlers(ByRef Efd As EarthSoft.EDP.EddFormatDefinition)

    ' save refs to required objects
    Me._Efd = Efd
    Me._Ch = Efd.CustomHandler

    ' add event handlers here
    AddHandler Efd.Tables("location_v1").ColumnChanged, AddressOf Handler_Locationv1_ColumnChanged

  End Sub


  Public Sub AfterAddDataHandlers(ByRef Efd As EarthSoft.EDP.EddFormatDefinition)

    ' add event handlers here

  End Sub


#End Region


#Region "Handlers"


  ''' <summary>
  ''' Adds error if sys_loc_code contains x
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Private Sub Handler_Locationv1_ColumnChanged(sender As Object, e As System.Data.DataColumnChangeEventArgs)

    Select Case e.Column.ColumnName.ToLower

      Case "sys_loc_code"

        If (e.Row.IsNull(e.Column.ColumnName) OrElse String.IsNullOrEmpty(e.Row(e.Column))) Then
          Me._Ch.RemoveError(e.Row, EDP.EddErrors.CustomError1)
        Else
          Dim val As String = e.Row.Item(e.Column)
          If val.ToLower.Contains("x") Then
            Me._Ch.AddError(e.Row, EDP.EddErrors.CustomError1)
          Else
            Me._Ch.RemoveError(e.Row, EDP.EddErrors.CustomError1)
          End If
        End If

    End Select

  End Sub


#End Region

  ''' <summary>
  ''' Loc_Date_Matrix_Depth_Type
  ''' </summary>
  ''' <param name="sampleRow"></param>
  ''' <param name="columnChanged"></param>
  ''' <param name="sysSampleCode"></param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Function GetCustomSysSampleCode(sampleRow As System.Data.DataRow, columnChanged As String, ByRef sysSampleCode As String) As Boolean Implements ICustomHandlerSysSampleCode.GetCustomSysSampleCode
    sysSampleCode = String.Empty
    With sampleRow

            'separator
            Dim sep As String = "_"

            ' If you want to use the loc_name in your sample id then uncomment this block.
            ''''''''''''''''''''''''''''''''
            'Dim locname As String = String.Empty
            'Dim location As String = String.Empty
            'If Not .IsNull("sys_loc_code") Then location = .Item("sys_loc_code").ToString
            'Dim rows1() As System.Data.DataRow = _Efd.Tables("Location_v1").Select(String.Format("sys_loc_code ='{0}' ", location))
            'If rows1.Length > 0 Then
            '    locname = rows1(0).Item("loc_name").ToString
            'End If
            ''''''''''''''''''''''''''''''''''

            'type
            Dim sampleType As String = String.Empty
      If Not .Item("sample_type_code").ToString.Length = 0 Then
        sampleType = .Item("sample_type_code").ToString
      End If

      'date
      Dim df As String = "yyyyMMdd"

      'depth
      Dim startDepth As String
      If Not .Item("start_depth").ToString.Length = 0 Then
        startDepth = .Item("start_depth").ToString()
      Else
        startDepth = "X"
      End If

      'matrix
      Dim sampleMatrix As String = String.Empty
      If Not .Item("sample_matrix_code").ToString.Length = 0 Then
        sampleMatrix = .Item("sample_matrix_code").ToString()
      Else
        Return False
      End If

      Dim sampleDate As Date
      If .Item("sys_loc_code").ToString.Length > 0 AndAlso .Item("sample_date").ToString.Length > 0 AndAlso Date.TryParse(.Item("sample_date").ToString, sampleDate) Then

        'count type for use with FD/FR, firstly checks to see if currently being used and keeps the current counter value
        Dim counter As Double = 0
        If sampleType.ToUpper = "FD" Or sampleType.ToUpper = "FR" Then
          Dim sysSampleCodeOrig As String = .Item("sys_sample_code").ToString()
          Dim currentCounterString As String = String.Empty
          If (sysSampleCodeOrig.LastIndexOf("F"c) + 3) < sysSampleCodeOrig.Length Then
            currentCounterString = sysSampleCodeOrig.Substring(sysSampleCodeOrig.LastIndexOf("F"c) + 3)
          End If
          Dim regExpression As String = "((FD|FR)(" + sep + ")\d*)$"
          If Not System.Text.RegularExpressions.Regex.IsMatch(sysSampleCodeOrig, regExpression) OrElse Not Double.TryParse(currentCounterString, counter) Then
            Dim tmpStr As String = String.Format("sample_type_code = '{0}'", .Item("sample_type_code"), String.Format("{0}", .Item("sample_type_code")))
            counter = sampleRow.Table.Select(tmpStr).Count()
          End If
        End If

        If startDepth = "X" Then
          If sampleType.ToUpper = "FD" Or sampleType.ToUpper = "FR" Or sampleType.ToUpper.Equals("FB") Or sampleType.ToUpper.Equals("TB") Then
            sysSampleCode = String.Format("{0}{1}{2}{3}{4}{5}{6}", sampleDate.ToString(df), sep, sampleMatrix, sep, sampleType, sep, counter)
          Else
            sysSampleCode = String.Format("{0}{1}{2}{3}{4}", .Item("sys_loc_code").ToString, sep, sampleDate.ToString(df), sep, sampleMatrix)
          End If

        Else
          If sampleType.ToUpper = "FD" Or sampleType.ToUpper = "FR" Or sampleType.ToUpper.Equals("FB") Or sampleType.ToUpper.Equals("TB") Then
            sysSampleCode = String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}", sampleDate.ToString(df), sep, sampleMatrix, sep, startDepth, sep, sampleType, sep, counter)
          Else
            sysSampleCode = String.Format("{0}{1}{2}{3}{4}{5}{6}", .Item("sys_loc_code").ToString, sep, sampleDate.ToString(df), sep, sampleMatrix, sep, startDepth)
          End If
        End If
      End If

      Return True
    End With
    Return False
  End Function

End Class
