<%
'=======================================================================================================================
' Contacts Model
'=======================================================================================================================

Class ContactsModel_Class
	Public Validator
	Public Class_Get_Properties

	Public ID '90
	Public JURISCODE '106
	Public TownshipName '106
	Public CONTACT_NAME '106
	Public TownshipNum '106
	Public BUSINESS_ADDRESS '106
	Public TITLE '106
	Public BUSINESS_ADDRESS2 '106
	Public BUSINESS_ADDRESS3 '106
	Public MAILING_ADDRESS '106
	Public MAILING_ADDRESS2 '106
	Public MAILING_ADDRESS3 '106
	Public RESIDENTIAL_ADDRESS '106
	Public RESIDENTIAL_ADDRESS2 '106
	Public PHONENUM1 '106
	Public RESIDENTIAL_ADDRESS3 '106
	Public FAXNUM '122
	Public PHONENUM2 '106
	Public EMAIL '106

	Private Sub Class_Initialize
		'ValidateExitsts Me, "",""
		Class_Get_Properties = Array("ID, JURISCODE, TownshipName, CONTACT_NAME, TownshipNum, BUSINESS_ADDRESS, TITLE, BUSINESS_ADDRESS2, BUSINESS_ADDRESS3, MAILING_ADDRESS, MAILING_ADDRESS2, MAILING_ADDRESS3, RESIDENTIAL_ADDRESS, RESIDENTIAL_ADDRESS2, PHONENUM1, RESIDENTIAL_ADDRESS3, FAXNUM, PHONENUM2, EMAIL")
	End Sub

End CLass

'=======================================================================================================================
' Contacts Repository
'=======================================================================================================================

Class ContactsRepository_Class

	Public Function FindByID(ID)
		dim sql : sql = "Select [ID], [JURISCODE], [TownshipName], [CONTACT_NAME], [TownshipNum], [BUSINESS_ADDRESS], [TITLE], [BUSINESS_ADDRESS2], [BUSINESS_ADDRESS3], [MAILING_ADDRESS], [MAILING_ADDRESS2], [MAILING_ADDRESS3], [RESIDENTIAL_ADDRESS], [RESIDENTIAL_ADDRESS2], [PHONENUM1], [RESIDENTIAL_ADDRESS3], [FAXNUM], [PHONENUM2], [EMAIL] FROM [Contacts] WHERE ID = ?"
		dim rs : set rs = DAL.Query(sql,ID)
    	If rs.EOF then
    		Err.Raise 1, "ContactsRepository_Class", ContactsNotFoundException("ID", ID)
		Else
			set FindByID = Automapper.AutoMap(rs,"ContactsModel_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 [ID], [JURISCODE], [TownshipName], [CONTACT_NAME], [TownshipNum], [BUSINESS_ADDRESS], [TITLE], [BUSINESS_ADDRESS2], [BUSINESS_ADDRESS3], [MAILING_ADDRESS], [MAILING_ADDRESS2], [MAILING_ADDRESS3], [RESIDENTIAL_ADDRESS], [RESIDENTIAL_ADDRESS2], [PHONENUM1], [RESIDENTIAL_ADDRESS3], [FAXNUM], [PHONENUM2], [EMAIL] FROM [Contacts]"

 		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 = ContactsList(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 [ID], [JURISCODE], [TownshipName], [CONTACT_NAME], [TownshipNum], [BUSINESS_ADDRESS], [TITLE], [BUSINESS_ADDRESS2], [BUSINESS_ADDRESS3], [MAILING_ADDRESS], [MAILING_ADDRESS2], [MAILING_ADDRESS3], [RESIDENTIAL_ADDRESS], [RESIDENTIAL_ADDRESS2], [PHONENUM1], [RESIDENTIAL_ADDRESS3], [FAXNUM], [PHONENUM2], [EMAIL] FROM [Contacts]"

 		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 = PagedContactsList(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 [ID], [JURISCODE], [TownshipName], [CONTACT_NAME], [TownshipNum], [BUSINESS_ADDRESS], [TITLE], [BUSINESS_ADDRESS2], [BUSINESS_ADDRESS3], [MAILING_ADDRESS], [MAILING_ADDRESS2], [MAILING_ADDRESS3], [RESIDENTIAL_ADDRESS], [RESIDENTIAL_ADDRESS2], [PHONENUM1], [RESIDENTIAL_ADDRESS3], [FAXNUM], [PHONENUM2], [EMAIL] FROM [Contacts]"

 		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 = PagedContactsList(rs, per_page)
		Destroy rs
	End Function

	Private Function PagedContactsList(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 ContactsModel_Class)
			x = x +1
			rs.MoveNext
		Loop
		set PagedContactsList = list
	End Function

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

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

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

	Public Sub AddNew(ByRef model)
		dim sql : sql = "INSERT INTO [Contacts] (" &_
													"[JURISCODE]," &_
													"[TownshipName]," &_
													"[CONTACT_NAME]," &_
													"[TownshipNum]," &_
													"[BUSINESS_ADDRESS]," &_
													"[TITLE]," &_
													"[BUSINESS_ADDRESS2]," &_
													"[BUSINESS_ADDRESS3]," &_
													"[MAILING_ADDRESS]," &_
													"[MAILING_ADDRESS2]," &_
													"[MAILING_ADDRESS3]," &_
													"[RESIDENTIAL_ADDRESS]," &_
													"[RESIDENTIAL_ADDRESS2]," &_
													"[PHONENUM1]," &_
													"[RESIDENTIAL_ADDRESS3]," &_
													"[FAXNUM]," &_
													"[PHONENUM2]," &_
													"[EMAIL])" &_
						"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
		DAL.Execute sql, Array(model.JURISCODE, _
							  model.TownshipName, _
							  model.CONTACT_NAME, _
							  model.TownshipNum, _
							  model.BUSINESS_ADDRESS, _
							  model.TITLE, _
							  model.BUSINESS_ADDRESS2, _
							  model.BUSINESS_ADDRESS3, _
							  model.MAILING_ADDRESS, _
							  model.MAILING_ADDRESS2, _
							  model.MAILING_ADDRESS3, _
							  model.RESIDENTIAL_ADDRESS, _
							  model.RESIDENTIAL_ADDRESS2, _
							  model.PHONENUM1, _
							  model.RESIDENTIAL_ADDRESS3, _
							  model.FAXNUM, _
							  model.PHONENUM2, _
							  model.EMAIL)
			sql = "SELECT TOP 1 ID FROM [Contacts] ORDER BY ID DESC"
			dim rs : set rs = DAL.Query(sql, empty)
			model.ID = rs("ID")
			Destroy rs
	End Sub

	Public Sub Update(model)
		dim sql : sql = "UPDATE [Contacts] SET [JURISCODE] = ?," &_
						"[TownshipName] = ?," &_
						"[CONTACT_NAME] = ?," &_
						"[TownshipNum] = ?," &_
						"[BUSINESS_ADDRESS] = ?," &_
						"[TITLE] = ?," &_
						"[BUSINESS_ADDRESS2] = ?," &_
						"[BUSINESS_ADDRESS3] = ?," &_
						"[MAILING_ADDRESS] = ?," &_
						"[MAILING_ADDRESS2] = ?," &_
						"[MAILING_ADDRESS3] = ?," &_
						"[RESIDENTIAL_ADDRESS] = ?," &_
						"[RESIDENTIAL_ADDRESS2] = ?," &_
						"[PHONENUM1] = ?," &_
						"[RESIDENTIAL_ADDRESS3] = ?," &_
						"[FAXNUM] = ?," &_
						"[PHONENUM2] = ?," &_
						"[EMAIL] = ?" &_
						" WHERE [ID] = ?"

		DAL.Execute sql, Array(model.JURISCODE, _
							  model.TownshipName, _
							  model.CONTACT_NAME, _
							  model.TownshipNum, _
							  model.BUSINESS_ADDRESS, _
							  model.TITLE, _
							  model.BUSINESS_ADDRESS2, _
							  model.BUSINESS_ADDRESS3, _
							  model.MAILING_ADDRESS, _
							  model.MAILING_ADDRESS2, _
							  model.MAILING_ADDRESS3, _
							  model.RESIDENTIAL_ADDRESS, _
							  model.RESIDENTIAL_ADDRESS2, _
							  model.PHONENUM1, _
							  model.RESIDENTIAL_ADDRESS3, _
							  model.FAXNUM, _
							  model.PHONENUM2, _
							  model.EMAIL, _
							  model.ID)
	End Sub

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

dim ContactsRepository__Singleton
Function ContactsRepository()
	If IsEmpty(ContactsRepository__Singleton) then
		set ContactsRepository__Singleton = new ContactsRepository_Class
	End If
	set ContactsRepository = ContactsRepository__Singleton
End Function
%>
