Update available policies when on configuration file update
[aquarium] / src / main / scala / gr / grnet / aquarium / store / mongodb / MongoDBStore.scala
index 56438dc..16721ed 100644 (file)
@@ -48,10 +48,10 @@ import gr.grnet.aquarium.logic.events.UserEvent.{JsonNames => UserEventJsonNames
 import gr.grnet.aquarium.logic.events.WalletEntry.{JsonNames => WalletJsonNames}
 import java.util.Date
 import com.ckkloverdos.maybe.Maybe
-import gr.grnet.aquarium.logic.events.{UserEvent, WalletEntry, ResourceEvent, AquariumEvent}
-import com.mongodb._
 import gr.grnet.aquarium.logic.accounting.Policy
-import gr.grnet.aquarium.logic.accounting.dsl.DSLComplexResource
+import gr.grnet.aquarium.logic.accounting.dsl.{Timeslot, DSLPolicy, DSLComplexResource}
+import gr.grnet.aquarium.logic.events._
+import com.mongodb._
 
 /**
  * Mongodb implementation of the various aquarium stores.
@@ -68,12 +68,14 @@ class MongoDBStore(
   with UserStateStore
   with WalletEntryStore
   with UserEventStore
+  with PolicyStore
   with Loggable {
 
-  private[store] lazy val rcEvents      = getCollection(MongoDBStore.RESOURCE_EVENTS_COLLECTION)
-  private[store] lazy val userStates    = getCollection(MongoDBStore.USER_STATES_COLLECTION)
-  private[store] lazy val userEvents    = getCollection(MongoDBStore.USER_EVENTS_COLLECTION)
-  private[store] lazy val walletEntries = getCollection(MongoDBStore.WALLET_ENTRIES_COLLECTION)
+  private[store] lazy val resourceEvents = getCollection(MongoDBStore.RESOURCE_EVENTS_COLLECTION)
+  private[store] lazy val userStates     = getCollection(MongoDBStore.USER_STATES_COLLECTION)
+  private[store] lazy val userEvents     = getCollection(MongoDBStore.USER_EVENTS_COLLECTION)
+  private[store] lazy val walletEntries  = getCollection(MongoDBStore.WALLET_ENTRIES_COLLECTION)
+  private[store] lazy val policies      = getCollection(MongoDBStore.POLICIES_COLLECTION)
 
   private[this] def getCollection(name: String): DBCollection = {
     val db = mongo.getDB(database)
@@ -98,16 +100,16 @@ class MongoDBStore(
 
   //+ResourceEventStore
   def storeResourceEvent(event: ResourceEvent): Maybe[RecordID] =
-    MongoDBStore.storeAquariumEvent(event, rcEvents)
+    MongoDBStore.storeAquariumEvent(event, resourceEvents)
 
   def findResourceEventById(id: String): Maybe[ResourceEvent] =
-    MongoDBStore.findById(id, rcEvents, MongoDBStore.dbObjectToResourceEvent)
+    MongoDBStore.findById(id, resourceEvents, MongoDBStore.dbObjectToResourceEvent)
 
   def findResourceEventsByUserId(userId: String)
                                 (sortWith: Option[(ResourceEvent, ResourceEvent) => Boolean]): List[ResourceEvent] = {
     val query = new BasicDBObject(ResourceJsonNames.userId, userId)
 
-    MongoDBStore.runQuery(query, rcEvents)(MongoDBStore.dbObjectToResourceEvent)(sortWith)
+    MongoDBStore.runQuery(query, resourceEvents)(MongoDBStore.dbObjectToResourceEvent)(sortWith)
   }
 
   def findResourceEventsByUserIdAfterTimestamp(userId: String, timestamp: Long): List[ResourceEvent] = {
@@ -117,7 +119,7 @@ class MongoDBStore(
     
     val sort = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
 
-    val cursor = rcEvents.find(query).sort(sort)
+    val cursor = resourceEvents.find(query).sort(sort)
 
     try {
       val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
@@ -148,7 +150,7 @@ class MongoDBStore(
     }
 
     val sort = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
-    val cursor = rcEvents.find(query).sort(sort)
+    val cursor = resourceEvents.find(query).sort(sort)
 
     try {
       val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
@@ -161,16 +163,25 @@ class MongoDBStore(
     }
   }
 
-  def findResourceEventsForPeriod(userId: String, startTime: Long, stopTime: Long): List[ResourceEvent] = {
+  def findResourceEventsForReceivedPeriod(userId: String, startTimeMillis: Long, stopTimeMillis: Long): List[ResourceEvent] = {
     val query = new BasicDBObject()
     query.put(ResourceJsonNames.userId, userId)
-    query.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$gte", startTime))
-    query.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$lte", stopTime))
+    query.put(ResourceJsonNames.receivedMillis, new BasicDBObject("$gte", startTimeMillis))
+    query.put(ResourceJsonNames.receivedMillis, new BasicDBObject("$lte", stopTimeMillis))
 
+    // Sort them by increasing order for occurred time
     val orderBy = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
 
-    MongoDBStore.runQuery[ResourceEvent](query, rcEvents, orderBy)(MongoDBStore.dbObjectToResourceEvent)(None)
+    MongoDBStore.runQuery[ResourceEvent](query, resourceEvents, orderBy)(MongoDBStore.dbObjectToResourceEvent)(None)
+  }
+  
+  def countOutOfSyncEventsForBillingMonth(userId: String, yearOfBillingMonth: Int, billingMonth: Int): Maybe[Long] = {
+    Maybe {
+      // FIXME: Implement
+      0L
+    }
   }
+
   //-ResourceEventStore
 
   //+UserStateStore
@@ -293,6 +304,24 @@ class MongoDBStore(
     MongoDBStore.runQuery(query, userEvents)(MongoDBStore.dbObjectToUserEvent)(Some(_sortByTimestampAsc))
   }
   //-UserEventStore
+
+  //+PolicyStore
+  def loadPolicies(after: Long): List[PolicyEntry] = {
+    val query = new BasicDBObject(PolicyEntry.JsonNames.validFrom,
+      new BasicDBObject("$gt", after))
+    MongoDBStore.runQuery(query, policies)(MongoDBStore.dbObjectToPolicyEvent)(Some(_sortByTimestampAsc))
+  }
+
+  def storePolicy(policy: PolicyEntry): Maybe[RecordID] = MongoDBStore.storeAquariumEvent(policy, policies)
+
+
+  def updatePolicy(policy: PolicyEntry) = {
+    //Find the entry
+    val query = new BasicDBObject(PolicyEntry.JsonNames.id, policy.id)
+    val policyObject = MongoDBStore.jsonSupportToDBObject(policy)
+    policies.update(query, policyObject, true, false)
+  }
+  //-PolicyStore
 }
 
 object MongoDBStore {
@@ -321,7 +350,6 @@ object MongoDBStore {
    */
   final val USER_EVENTS_COLLECTION = "userevents"
 
-
   /**
    * Collection holding [[gr.grnet.aquarium.logic.events.WalletEntry]].
    *
@@ -329,6 +357,11 @@ object MongoDBStore {
    */
   final val WALLET_ENTRIES_COLLECTION = "wallets"
 
+  /**
+   * Collection holding [[gr.grnet.aquarium.logic.accounting.dsl.DSLPolicy]].
+   */
+  final val POLICIES_COLLECTION = "policies"
+
   /* TODO: Some of the following methods rely on JSON (de-)serialization).
   * A method based on proper object serialization would be much faster.
   */
@@ -348,6 +381,10 @@ object MongoDBStore {
     UserEvent.fromJson(JSON.serialize(dbObj))
   }
 
+  def dbObjectToPolicyEvent(dbObj: DBObject): PolicyEntry = {
+    PolicyEntry.fromJson(JSON.serialize(dbObj))
+  }
+
   def findById[A >: Null <: AquariumEvent](id: String, collection: DBCollection, deserializer: (DBObject) => A) : Maybe[A] = Maybe {
     val query = new BasicDBObject(ResourceJsonNames.id, id)
     val cursor = collection find query