'Class module
Sub Class_Globals
Private ws As WebSocket 'ignore
' will hold our page information
Public page As ABMPage
' page theme
Private theme As ABMTheme
' to access the constants
Private ABM As ABMaterial 'ignore
' name of the page, must be the same as the class name (case sensitive!)
Public Name As String = "Mothering" '<-------------------------------------------------------- IMPORTANT
' will hold the unique browsers window id
Private ABMPageId As String = ""
' your own variables
Dim calves As List
Dim mothers As List
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
' build the local structure IMPORTANT!
Main.currAction = uiUtils.getAction("Mothering")
Main.currMob.ID = 0
mothers.Initialize
calves.Initialize
BuildPage
End Sub
#Region ABM
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
Log("Connected")
ws = WebSocket1
ABMPageId = ABM.GetPageID(page, Name,ws)
Dim session As HttpSession = ABM.GetSession(ws, ABMShared.SessionMaxInactiveIntervalSeconds)
If session.IsNew Then
session.Invalidate
ABMShared.NavigateToPage(ws, "", "./")
Return
End If
If ABMShared.NeedsAuthorization Then
If session.GetAttribute2("IsAuthorized", "") = "" Then
ABMShared.NavigateToPage(ws, ABMPageId, "../")
Return
End If
End If
ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws)
If page.ComesFromPageCache Then
' when we have a page that is cached it doesn't matter if it comes or not from a new connection we serve the cached version.
Log("Comes from cache")
page.Refresh
page.FinishedLoading
Else
If page.WebsocketReconnected Then
Log("Websocket reconnected")
' when we have a client that doesn't have the page in cache and it's websocket reconnected and also it's session is new - basically when the client had internet problems and it's session (and also cache) expired before he reconnected so the user has content in the browser but we don't have any on the server. So we need to reload the page.
' when a client that doesn't have the page in cache and it's websocket reconnected but it's session is not new - when the client had internet problems and when he reconnected it's session was valid but he had no cache for this page we need to reload the page as the user browser has content, reconnected but we have no content in cache
ABMShared.NavigateToPage (ws, ABMPageId, "./" & page.PageHTMLName)
Else
' when the client did not reconnected it doesn't matter if the session was new or not because this is the websockets first connection so no dynamic content in the browser ... we are going to serve the dynamic content...
Log("Websocket first connection")
page.Prepare
ConnectPage
End If
End If
Log(ABMPageId)
End Sub
Private Sub WebSocket_Disconnected
Log("Disconnected")
End Sub
Sub Page_ParseEvent(Params As Map)
Dim eventName As String = Params.Get("eventname")
Dim eventParams() As String = Regex.Split(",",Params.Get("eventparams"))
Log($".........................>> ${eventName} "$)
For i = 0 To eventParams.Length-1
Log(eventParams(i))
Next
If eventName = "beforeunload" Then
Log("preparing for url refresh")
ABM.RemoveMeFromCache(ABMShared.CachedPages, ABMPageId)
Return
End If
Dim caller As Object = page.GetEventHandler(Me, eventName)
If caller = Me Then
If SubExists(Me, eventName) Then
Params.Remove("eventname")
Params.Remove("eventparams")
If eventName = "page_dropped" Then
page.ProcessDroppedEvent(Params)
End If
Select Case Params.Size
Case 0
CallSub(Me, eventName)
Case 1
CallSub2(Me, eventName, Params.Get(eventParams(0)))
Case 2
If Params.get(eventParams(0)) = "abmistable" Then
Dim PassedTables As List = ABM.ProcessTablesFromTargetName(Params.get(eventParams(1)))
CallSub2(Me, eventName, PassedTables)
Else
CallSub3(Me, eventName, Params.Get(eventParams(0)), Params.Get(eventParams(1)))
End If
Case Else
' cannot be called directly, to many param
CallSub2(Me, eventName, Params)
End Select
End If
Else
CallSubDelayed2(caller, "ParseEvent", Params) 'ignore
End If
End Sub
public Sub BuildTheme()
' start with the base theme defined in ABMShared
theme.Initialize("pagetheme")
theme.AddABMTheme(ABMShared.MyTheme)
' add your specific page themes
End Sub
public Sub BuildPage()
' initialize the theme
BuildTheme
' initialize this page using our theme
page.InitializeWithTheme(Name, "/ws/" & ABMShared.AppName & "/" & Name, False, ABMShared.SessionMaxInactiveIntervalSeconds, theme)
page.ShowLoader=True
page.PageHTMLName = "index.html"
page.PageTitle = "Mothering"
page.PageDescription = "Match calves with mothers"
page.PageKeywords = ""
page.PageSiteMapPriority = ""
page.PageSiteMapFrequency = ABM.SITEMAP_FREQ_YEARLY
page.ShowConnectedIndicator = True
' adding a navigation bar
ABMShared.BuildNavigationBar(page, uiUtils.CompanyName("Cattle mothering"),"", "", "", "")
#Region NOT SAVED: 2019-02-27T09:19:13
page.AddRows(1,True,"").AddCellsOS(1,0,0,0,4,4,4,"")
page.AddRows(1,True,"").AddCellsOS(1,0,0,0,10,10,10,"")
page.AddRows(1,True,"").AddCellsOS(1,8,8,8,2,2,2,"")
page.BuildGrid ' IMPORTANT!
#End Region
End Sub
public Sub ConnectPage()
' connecting the navigation bar
ABMShared.ConnectNavigationBar(page)
Dim cbmMobs As ABMCombo
cbmMobs.Initialize(page,"cbmmobs","Select mob",500,"")
If Main.mobs.Size <= 0 Then
DBUtils.loadMobs()
End If
uiUtils.fillMobsCombo(page,cbmMobs)
Dim MthrTab As ABMTable
MthrTab.Initialize(page,"mthrtab",True,False,True,"")
MthrTab.SetHeaders(Array As String("Calf", "Mother"))
MthrTab.SetHeaderThemes(Array As String("headerfooter", "headerfooter"))
Dim but As ABMButton
but.InitializeRaised(page,"butsave","","","SAVE","")
page.Cell(1,1).AddComponent(cbmMobs)
page.Cell(2,1).AddComponent(MthrTab)
page.Cell(3,1).AddComponent(but)
' refresh the page
page.Refresh
' Tell the browser we finished loading
page.FinishedLoading
buildMotheringList
' restoring the navigation bar position
page.RestoreNavigationBarPosition
End Sub
#end region
Sub mthrtab_Clicked(PassedRowsAndColumns As List)
Dim but As ABMButton = page.Component("butsave")
but.Enabled = True
but.Refresh
End Sub
Sub butsave_clicked(Target As String )
Dim Tbl As ABMTable = page.Component("mthrtab")
For i = 0 To calves.Size-1
Dim chk As ABMCheckbox = Tbl.GetComponent(i,"chkbx"&i)
Dim cmb As ABMCombo = Tbl.GetComponent(i,"cmb"&i)
'Log($"Found calf ${i} ${chk.State} Mother code = ${cmb.GetActiveItemId}"$)
If chk.State And cmb.GetActiveItemId <> "-1" Then
Log($"GetActiveItemId = ${cmb.GetActiveItemId}"$)
Dim mNo As Int = cmb.GetActiveItemId.SubString(1)
Dim M As beast = mothers.Get(mNo)
Dim C As beast = calves.Get(i)
Log($"Calf ${C.EarTag} has mother ${M.EarTag}"$)
C.MothersREFRID=M.REFRID
Dim note As String = $"Mothering : Calf ${C.EarTag} has mother ${M.EarTag}"$
If M.OffspringDOB.length > 1 Then
C.DOB = M.OffspringDOB
note = note & $", ${M.EarTag}'s offspring DOB used to reset ${C.EarTag}'s DOB"$
End If
'M.OffspringDOB=0 NOTE this can not be done as can not be reused if twins.
DBUtils.updateBeast(C)
DBUtils.updateBeast(M)
Dim P As procedure
P.Initialize
P.REFRID = C.REFRID
P.Date=uiUtils.Ticks2Date( DateTime.Now)
P.ActivityID = Main.currAction.ID
P.Value = "Mothering"
P.Notes = note
DBUtils.saveNewProc(P)
End If
Next
Tbl.Clear
Dim but As ABMButton = page.Component("butsave")
but.Enabled = False
but.Refresh
End Sub
Sub fillTable
Dim T As ABMTable = page.Component("mthrtab")
T.Clear
If calves.Size > 0 Then
For i = 0 To calves.Size-1
Dim C As beast = calves.Get(i)
Dim eachCalf As List
eachCalf.Initialize
Dim chk As ABMCheckbox
chk.Initialize(page,"chkbx"&i,C.EarTag,False,"")
chk.Tag = C
eachCalf.Add(chk)
Dim cmb As ABMCombo
cmb.Initialize(page,"cmb"&i,$"Select mother (${i})"$,300,"")
'cmb.SetRelativePosition(ABM.POSITION_TOPRIGHT,0,-20)
For j = 0 To mothers.Size-1
Dim lab As ABMLabel
Dim M As beast = mothers.Get(j)
lab.Initialize(page,"lab"&j,M.EarTag,ABM.SIZE_H6,False,"")
lab.Tag=M
cmb.AddItem("m"&j,m.eartag,lab)
Next
cmb.SetActiveItemId(-1)
eachCalf.Add(cmb)
T.AddRow("t"&i,eachCalf)
Next
Else
Dim lbl As ABMLabel
lbl.Initialize(page,"lbl",$"No calves found!"$,ABM.SIZE_H5,False,"")
T.AddRow("warn",lbl)
End If
T.Refresh
End Sub
Sub cbmMobs_clicked(itemId As String)
Log($"cmbMobs click event itemID = ${itemId}"$)
Dim i As Int = itemId
Main.currMob = Main.mobs.Get(i)
buildMotheringList
Dim but As ABMButton = page.Component("butsave")
but.Enabled = True
but.Refresh
End Sub
Sub buildMotheringList()
Dim qu As String = $"SELECT * FROM Beasts WHERE OffspringDOB > 0 AND MobID = ${Main.currMob.ID} AND (sex = 'C' OR Sex = 'H') GROUP BY EarTag"$
mothers.Clear
DBUtils.getBeastGrp(qu,mothers)
Log($"${qu}${CRLF} ${mothers.Size} mothers found"$)
calves.Clear
Dim qu As String = $"SELECT * FROM Beasts WHERE MothersNLIS = 0 AND MobID = ${Main.currMob.ID} AND (sex = 'H' OR Sex = 'S') GROUP BY EarTag"$
DBUtils.getBeastGrp(qu,calves)
Log($"${qu}${CRLF} ${calves.Size} calves found"$)
If mothers.Size > 0 And calves.Size > 0 Then
fillTable
Else
Dim T As ABMTable = page.Component("MthrTab")
T.Clear
End If
End Sub
#Region ABMPage
' clicked on the navigation bar
Sub Page_NavigationbarClicked(Action As String, Value As String)
' saving the navigation bar position
page.SaveNavigationBarPosition
If Action = "LogOff" Then
ABMShared.LogOff(page)
Return
End If
ABMShared.NavigateToPage(ws, ABMPageId, Value)
End Sub
Sub Page_DebugConsole(message As String)
Log("---> " & message)
End Sub
#end region