UpdateListener-class {Proc4}R Documentation

Class "UpdateListener"

Description

This Listener updates an existing record (in a Mongo collection) for the student (uid), with the contents of the data (details) field of the message.

Details

The database is a mongo collection identified by dburi, dbname and colname (collection within the database). The mess field of the P4Message is checked against the applicable messages in messSet. If it is there, then the record in the database corresponding to the qfields (by default app(mess) and uid(mess)) is updated. Specifically, the field targetField is set to details(mess). The function jsonEncoder is called to encode the target field as a JSON object for injection into the database.

Extends

This class implements the Listener interface.

All reference classes extend and inherit methods from "envRefClass".

Methods

isListener

signature(x = "UpdateListener"): TRUE

receiveMessage

signature(x = "UpdateListener"): If the message is in the messSet, it updates the record corresponding to app(mess) and uid(mess) in the database with the contents of details(mess). (See details.)

Fields

dbname:

Object of class character giving the name of the Mongo database

dburi:

Object of class character giving the url of the Mongo database.

colname:

Object of class character giving the column of the Mongo database.

messSet:

A vector of class character giving the name of messages which are sent to the database. Only messages for which mess(mess) is an element of messSet will be inserted.

db:

Object of class MongoDB giving the database. Use messdb() to access this field to makes sure it has been set up.

qfields:

Object of class character giving the names of the fields which should be considered a key for the messages.

targetField:

Object of class character naming the field which is to be set.

jsonEncoder:

Object of class character naming a function which will be used to encode details(mess) as a JSON object. The default is unparseData.

Class-Based Methods

messdb():

Accessor for the database collection. Initializes the connection if it has not been set up.

receiveMessage(mess):

Does the work of updating the database. See Details.

initialize(sender, dbname, dburi, colname, messSet, ...):

Sets default values for fields.

Author(s)

Russell Almond

References

This is an example of the observer design pattern. https://en.wikipedia.org/wiki/Observer_pattern.

See Also

Listener, P4Message, UpdateListener, InjectionListener, CaptureListener, UpsertListener, TableListener, mongo

The function unparseData is the default encoder.

Examples

mess2 <- P4Message(app="default",uid="Phred",context="Down Hill",
                   sender="EIEvent",mess="Money Earned",
                   details=list(trophyHall=list(list("Down Hill"="gold"),
                                                list("Stairs"="silver")),
                                bankBalance=10))
data2json <- function(dat) {
  toJSON(sapply(dat,unboxer))
}
upwind <- UpdateListener(messSet=c("Money Earned","Money Spent"),
                         targetField="data",colname="Players",
                         jsonEncoder="data2json")

receiveMessage(upwind,mess2)


[Package Proc4 version 0.4-7 Index]