added implementation for MemStoreProvider (PolicyStore methods)
authorProdromos Gerakios <pgerakios@grnet.gr>
Thu, 19 Jul 2012 12:54:15 +0000 (15:54 +0300)
committerProdromos Gerakios <pgerakios@grnet.gr>
Thu, 19 Jul 2012 12:54:15 +0000 (15:54 +0300)
src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Timeslot.scala
src/main/scala/gr/grnet/aquarium/store/memory/MemStoreProvider.scala
src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala

index 40109f8..1948d54 100644 (file)
@@ -73,7 +73,7 @@ final case class Timeslot(from: Date, to: Date) extends Ordered[Timeslot] {
    */
   def contains(t: Timeslot) : Boolean = this.start <= t.start && this.end >= t.end
 
-  private[dsl] def weakIncludes(t: Date) : Boolean = start < t.getTime &&  t.getTime < end
+  def weakIncludes(t: Date) : Boolean = start < t.getTime &&  t.getTime < end
   def weakOverlaps(t: Timeslot) : Boolean =
     contains(t) || t.contains(this) || this.weakIncludes(t.from) || this.weakIncludes(t.to)
 
@@ -84,7 +84,7 @@ final case class Timeslot(from: Date, to: Date) extends Ordered[Timeslot] {
   /**
    * Check whether this timeslot contains the provided time instant.
    */
-  private[dsl] def includes(t: Date) : Boolean = start <= t.getTime &&  t.getTime <= end
+  def includes(t: Date) : Boolean = start <= t.getTime &&  t.getTime <= end
 
 
   /**
index 8120df5..1b500c9 100644 (file)
@@ -51,6 +51,8 @@ import gr.grnet.aquarium.computation.BillingMonthInfo
 import gr.grnet.aquarium.policy.{PolicyModel, StdPolicy}
 import collection.immutable.SortedMap
 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
+import collection.immutable
+import java.util.Date
 
 /**
  * An implementation of various stores that persists parts in memory.
@@ -283,11 +285,23 @@ extends StoreProvider
   }
 
   def loadValidPolicyAt(atMillis: Long): Option[Policy] = {
-    throw new UnsupportedOperationException
+    var d = new Date(atMillis)
+    /* sort in reverse order  and return the first that includes this date*/
+    _policies.sortWith({(x,y)=> y.validFrom < x.validFrom}).collectFirst({
+      case t if(t.validityTimespan.toTimeslot.includes(d)) => t
+    })
   }
 
+  private def emptyMap = immutable.SortedMap[Timeslot,Policy]()
+
   def loadAndSortPoliciesWithin(fromMillis: Long, toMillis: Long): SortedMap[Timeslot, Policy] = {
-    throw new UnsupportedOperationException
+    val range = Timeslot(fromMillis,toMillis)
+    _policies.foldLeft (emptyMap) { (map,p) =>
+      if(range.overlaps(p.validityTimespan.toTimeslot))
+        map + ((p.validityTimespan.toTimeslot,p))
+      else
+        map
+    }
   }
 }
 
index 48b5a2d..fe1b55d 100644 (file)
@@ -55,6 +55,8 @@ import gr.grnet.aquarium.policy.PolicyModel
 import gr.grnet.aquarium.{Aquarium, AquariumException}
 import collection.immutable.SortedMap
 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
+import collection.immutable
+import java.util.Date
 
 /**
  * Mongodb implementation of the various aquarium stores.
@@ -263,17 +265,6 @@ class MongoDBStore(
 
 
   //+PolicyStore
-  def loadPoliciesAfter(after: Long): List[Policy] = {
-    // FIXME implement
-    throw new UnsupportedOperationException
-  }
-
-
-  def findPolicyByID(id: String) = {
-    // FIXME implement
-    throw new UnsupportedOperationException
-  }
-
   /**
    * Store an accounting policy.
    */
@@ -282,11 +273,19 @@ class MongoDBStore(
     MongoDBStore.insertObject(dbPolicy, policies, MongoDBStore.jsonSupportToDBObject)
   }
 
+  private def emptyMap = immutable.SortedMap[Timeslot,Policy]()
+
   def loadValidPolicyAt(atMillis: Long): Option[Policy] = {
+    /*var d = new Date(atMillis)
+    /* sort in reverse order  and return the first that includes this date*/
+    policies.sortWith({(x,y)=> y.validFrom < x.validFrom}).collectFirst({
+      case t if(t.validityTimespan.toTimeslot.includes(d)) => t
+    })*/
     throw new UnsupportedOperationException
   }
 
   def loadAndSortPoliciesWithin(fromMillis: Long, toMillis: Long): SortedMap[Timeslot, Policy] = {
+
     throw new UnsupportedOperationException
   }
   //-PolicyStore