[back to
Anatomy of a Domino e-commerce Web site (Part 2)
]
The Availability Notifications agent
Here is the code for the Availability Notifications agent:
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim availabilityView, catalogView As NotesView
Dim availabilityQuery, catalogEntry, notification, tmpDoc, profiledoc As NotesDocument
Dim rtitem As NotesRichTextItem
Set db = session.CurrentDatabase
Set availabilityView = db.GetView("Notification Queries")
Set profiledoc = db.GetProfileDocument("ApplicationSettings")
If profiledoc Is Nothing Then
Exit Sub
End If
Set catalogView = db.GetView("(CatalogByID)")
Set availabilityQuery = availabilityView.GetFirstDocument
While Not (availabilityQuery Is Nothing)
' Search the catalog for the title the customer originally expressed interest in
Set catalogEntry = catalogView.GetDocumentByKey(availabilityQuery.ID(0))
' If we couldn't find the title, mark the notification query as 'failed' and move to the next one
If catalogEntry Is Nothing Then
availabilityQuery.Failed = "Failed"
Call availabilityQuery.Save(True, False)
Set availabilityQuery = availabilityView.GetNextDocument(availabilityQuery)
Else
' If the title was found in the catalog, only process notifications for those titles that are now available
If catalogEntry.Available(0) = "1" Then
Set notification = db.CreateDocument
Set rtitem = notification.CreateRichTextItem( "Body" )
Call rtitem.AppendText("The following title is now available from Liberty Fund:")
Call rtitem.AddNewLine(2)
Call rtitem.AppendText(availabilityQuery.Title(0))
Call rtitem.AddNewLine(1)
If (availabilityQuery.Subtitle(0) <> "") Then
Call rtitem.AppendText(availabilityQuery.Subtitle(0))
Call rtitem.AddNewLine(1)
End If
' To pass a multi-value author field from the catalog entry to the availability query, we had to concatenate all the authors together with '!!'. Now, we parse them apart and show them on separate lines of the notification email
auths = Evaluate(|@Explode(Authors; "!!")|, availabilityQuery)
Forall x In auths
Call rtitem.AppendText(x)
Call rtitem.AddNewLine(1)
End Forall
Call rtitem.AddNewLine(1)
Call rtitem.AppendText("For more information, or to order this item, please use the following link:")
Call rtitem.AddNewLine(2)
Call rtitem.AppendText(profiledoc.WebServerURL(0) + "/" + profiledoc.LibraryDBW(0) + "/CatalogByID/" & availabilityQuery.ID(0))
Call rtitem.AddNewLine(2)
Call rtitem.AppendText("Thank you,")
Call rtitem.AddNewLine(2)
Call rtitem.AppendText("Liberty Fund Customer Service")
notification.SendTo = availabilityQuery.Email(0)
notification.Subject = "The Title You Requested Now Available From Liberty Fund"
Call notification.Send(False)
' Grab the next document before we delete the one we're working on
Set tmpDoc = availabilityView.GetNextDocument(availabilityQuery)
' Remove the availability query after we've processed it
Call availabilityQuery.Remove(True)
Set availabilityQuery = tmpDoc
Else
' Item is not yet available, so grab the next one
Set availabilityQuery = availabilityView.GetNextDocument(availabilityQuery)
End If
End If
Wend
End Sub