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.MasterConf._
8 * Represents an incoming user event.
10 * @author Georgios Gousios <gousiosg@gmail.com>
13 override val id: String,
14 override val timestamp: Long,
17 eventType: Short, //1: create, 2: modify
18 state: String, //ACTIVE, SUSPENDED
22 ) extends AquariumEvent(id, timestamp) {
24 assert(eventType == 1 || eventType == 2)
25 assert(state.equalsIgnoreCase("ACTIVE") ||
26 state.equalsIgnoreCase("SUSPENDED"))
29 * Validate this event according to the following rules:
31 * Valid event states: `(eventType, state)`:
34 * - `c := 2, SUSPENDED`
37 * - `(non-existent) -> a`
41 def validate: Boolean = {
44 if (MasterConf.IMStore.userExists(userId))
47 if (!state.equalsIgnoreCase("ACTIVE")) return false
51 // All user events are of type 2 (modify) from hereon
52 val oldEvent = MasterConf.IMStore.findLastUserEvent(this.userId)
57 case y if (y.equalsIgnoreCase("SUSPENDED")) => this.state match {
58 case z if (z.equalsIgnoreCase("ACTIVE")) => //OK
59 case _ => return false
61 case y if (y.equalsIgnoreCase("ACTIVE")) => this.state match {
62 case z if (z.equalsIgnoreCase("SUSPENDED")) =>
63 if (x.eventType == 2) return false
64 case _ => return false
67 case None => return false
75 def fromJson(json: String): ResourceEvent = {
76 implicit val formats = JsonHelpers.DefaultJsonFormats
77 val jsonAST = parseJson(json)
78 Extraction.extract[ResourceEvent](jsonAST)