1 package gr.grnet.aquarium.logic.events
3 import gr.grnet.aquarium.util.json.JsonHelpers
4 import net.liftweb.json.{Extraction, parse => parseJson}
5 import gr.grnet.aquarium.Configurator._
6 import com.ckkloverdos.maybe.{Failed, NoVal, Just}
9 * Represents an incoming user event.
11 * @author Georgios Gousios <gousiosg@gmail.com>
14 override val id: String, // The id at the client side (the sender) TODO: Rename to remoteId or something...
15 override val occurredMillis: Long, // When it occurred at client side (the sender)
16 override val receivedMillis: Long, // When it was received by Aquarium
19 eventType: Short, //1: create, 2: modify
20 state: String, //ACTIVE, SUSPENDED
24 extends AquariumEvent(id, occurredMillis, receivedMillis) {
26 assert(eventType == 1 || eventType == 2)
27 assert(state.equalsIgnoreCase("ACTIVE") ||
28 state.equalsIgnoreCase("SUSPENDED"))
31 if(!state.equalsIgnoreCase("ACTIVE"))
35 * Validate this event according to the following rules:
37 * Valid event states: `(eventType, state)`:
40 * - `c := 2, SUSPENDED`
43 * - `(non-existent) -> a`
47 def validate: Boolean = {
49 MasterConfigurator.userStateStore.findUserStateByUserId(userId) match {
52 logger.warn("User to create exists: IMEvent".format(this.toJson));
57 logger.warn("Inexistent user to modify. IMEvent:".format(this.toJson))
61 logger.warn("Error retrieving user state: %s".format(x))
67 def setRcvMillis(millis: Long) = copy(receivedMillis = millis)
69 def isCreateUser = eventType == 1
71 def isModifyUser = eventType == 2
73 def isStateActive = state equalsIgnoreCase "ACTIVE"
75 def isStateSuspended = state equalsIgnoreCase "SUSPENDED"
79 def fromJson(json: String): UserEvent = {
80 implicit val formats = JsonHelpers.DefaultJsonFormats
81 val jsonAST = parseJson(json)
82 Extraction.extract[UserEvent](jsonAST)
85 def fromBytes(bytes: Array[Byte]): UserEvent = {
86 JsonHelpers.jsonBytesToObject[UserEvent](bytes)