<%
'=======================================================================================================================
' Jurisdiction Model
'=======================================================================================================================

Class JurisdictionModel_Class
	Public Validator
	Public Class_Get_Properties

	Public JCode '106
	Public Name '106
	Public Mailing_Address '106
	Public CSZ '106
	Public IMB '106
	Public IMB_Digits '106

	Private Sub Class_Initialize
		'ValidateExitsts Me, "",""
		Class_Get_Properties = Array("JCode, Name, Mailing_Address, CSZ, IMB, IMB_Digits")
	End Sub

End CLass

'=======================================================================================================================
' Jurisdiction Repository
'=======================================================================================================================

Class JurisdictionRepository_Class

	Public Function FindByJCode(JCode)
		dim sql : sql = "Select [JCode], [Name], [Mailing_Address], [CSZ], [IMB], [IMB_Digits] FROM [Jurisdiction] WHERE JCode = ?"
		dim rs : set rs = DAL.Query(sql,JCode)
    	If rs.EOF then
    		Err.Raise 1, "JurisdictionRepository_Class", JurisdictionNotFoundException("JCode", JCode)
		Else
			set FindByJCode = Automapper.AutoMap(rs,"JurisdictionModel_Class")
        End If
	End Function
	
	Public Function GetAll(orderBy)
		set GetAll = Find(empty,orderBy)
   	End Function

    Public Function Find(where_kvarray, order_string_or_array)
		dim sql : sql = "Select [JCode], [Name], [Mailing_Address], [CSZ], [IMB], [IMB_Digits] FROM [Jurisdiction]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " AND "
				sql = sql & " " & where_keys(i) & " "
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & "ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim rs : set rs = DAL.Query(sql, where_values)
		set Find = JurisdictionList(rs)
		Destroy rs
	End Function

	Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
		dim sql : sql = "Select [JCode], [Name], [Mailing_Address], [CSZ], [IMB], [IMB_Digits] FROM [Jurisdiction]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " AND "
				sql = sql & " " & where_keys(i) & " "
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & "ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, where_values, per_page, page_num)

		If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If

		set FindPaged = PagedJurisdictionList(rs, per_page)
		Destroy rs
	End Function

	Public Function SearchTablePaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
		dim sql : sql = "Select [JCode], [Name], [Mailing_Address], [CSZ], [IMB], [IMB_Digits] FROM [Jurisdiction]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " OR"
				sql = sql & " " & where_keys(i) & " LIKE ?"
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & " ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, where_values, per_page, page_num)

		If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If

		set SearchTablePaged = PagedJurisdictionList(rs, per_page)
		Destroy rs
	End Function

	Private Function PagedJurisdictionList(rs, per_page)
    	dim list : set list = new LinkedList_Class

		dim x : x =0
		Do While x < per_page and Not rs.EOF
			list.Push Automapper.AutoMap(rs, new JurisdictionModel_Class)
			x = x +1
			rs.MoveNext
		Loop
		set PagedJurisdictionList = list
	End Function

	Private Function JurisdictionNotFoundException(ByVal field_name, ByVal field_val)
			 JurisdictionNotFoundException = "Jurisdiction was not found with " & field_name & " of '" & field_val & "'."
	End Function

	Private Function JurisdictionList(rs)
		dim list : set list = new LinkedList_Class
		dim model

		Do until rs.EOF
			set model = new JurisdictionModel_Class
			list.Push Automapper.AutoMap(rs, model)
			rs.MoveNext
		Loop
		set JurisdictionList = list
	End Function

	Public Sub AddNew(ByRef model)
		dim sql : sql = "INSERT INTO [Jurisdiction] (" &_
													"[Name]," &_
													"[Mailing_Address]," &_
													"[CSZ]," &_
													"[IMB]," &_
													"[IMB_Digits])" &_
						"VALUES (?,?,?,?,?)"
		DAL.Execute sql, Array(model.Name, _
							  model.Mailing_Address, _
							  model.CSZ, _
							  model.IMB, _
							  model.IMB_Digits)
			sql = "SELECT TOP 1 JCode FROM [Jurisdiction] ORDER BY JCode DESC"
			dim rs : set rs = DAL.Query(sql, empty)
			model.JCode = rs("JCode")
			Destroy rs
	End Sub

	Public Sub Update(model)
		dim sql : sql = "UPDATE [Jurisdiction] SET [Name] = ?," &_
						"[Mailing_Address] = ?," &_
						"[CSZ] = ?," &_
						"[IMB] = ?," &_
						"[IMB_Digits] = ?" &_
						" WHERE [JCode] = ?"

		DAL.Execute sql, Array(model.Name, _
							  model.Mailing_Address, _
							  model.CSZ, _
							  model.IMB, _
							  model.IMB_Digits, _
							  model.JCode)
	End Sub

	Public Sub Delete(id)
		dim sql : sql = "DELETE FROM [Jurisdiction] WHERE [JCode] = ?"
		DAL.Execute sql, id
	End Sub
End Class

dim JurisdictionRepository__Singleton
Function JurisdictionRepository()
	If IsEmpty(JurisdictionRepository__Singleton) then
		set JurisdictionRepository__Singleton = new JurisdictionRepository_Class
	End If
	set JurisdictionRepository = JurisdictionRepository__Singleton
End Function
%>