+ def onResourceEventMsg(rcEvent: ResourceEventMsg) {
+ if(checkInitial()) {
+ return
+ }
+
+ if(!haveUserCreationEvent) {
+ DEBUG("No agreements. Ignoring %s", rcEvent)
+
+ return
+ }
+
+ assert(haveUserState, "haveUserState")
+
+ val oldTotalCredits = this._userStateModel.totalCreditsAsReal
+
+ chargingService.processResourceEvent(
+ rcEvent.getReceivedMillis,
+ rcEvent,
+ this._userStateModel,
+ aquarium.currentResourceMapping,
+ true
+ )
+
+ val newTotalCredits = this._userStateModel.totalCreditsAsReal
+
+ if(oldTotalCredits.signum * newTotalCredits.signum < 0) {
+ aquarium.eventBus ! new BalanceEvent(userID, newTotalCredits >= 0)
+ }
+
+ DEBUG("Updated %s", this._userStateModel)
+ }
+
+ def onGetUserBillRequest(event: GetUserBillRequest): Unit = {
+ checkInitial()
+
+ try{
+ val timeslot = event.timeslot
+ val resourceTypes: Map[String, ResourceType] = aquarium.policyStore.
+ loadSortedPolicyModelsWithin(timeslot.from.getTime,
+ timeslot.to.getTime).
+ values.headOption match {
+ case None => Map[String,ResourceType]()
+ case Some(policy:PolicyModel) => policy.resourceTypesMap
+ }
+ val state= if(haveUserState) Some(this._userStateModel.userStateMsg) else None
+ val billEntryMsg = BillEntryMsg.fromWorkingUserState(timeslot,this.userID,state,resourceTypes)
+ //val billEntryMsg = MessageFactory.createBillEntryMsg(billEntry)
+ //logger.debug("BILL ENTRY MSG: " + billEntryMsg.toString)
+ val billData = GetUserBillResponseData(this.userID,billEntryMsg)
+ sender ! GetUserBillResponse(Right(billData))
+ } catch {
+ case e:Exception =>
+ e.printStackTrace()
+ sender ! GetUserBillResponse(Left("Internal Server Error [AQU-BILL-0001]"), 500)
+ }
+ }
+
+ def onGetUserBalanceRequest(event: GetUserBalanceRequest): Unit = {
+ checkInitial()
+
+ val userID = event.userID
+
+ (haveAgreements, haveUserState) match {
+ case (true, true) ⇒
+ // (User CREATEd, with balance state)
+ val realtimeMillis = TimeHelpers.nowMillis()
+ chargingService.calculateRealtimeUserState(
+ this._userStateModel,
+ aquarium.currentResourceMapping,
+ realtimeMillis
+ )
+
+ sender ! GetUserBalanceResponse(Right(GetUserBalanceResponseData(this.userID, this._userStateModel.totalCredits)))
+
+ case (true, false) ⇒
+ // (User CREATEd, no balance state)
+ // Return the default initial balance
+ sender ! GetUserBalanceResponse(
+ Right(
+ GetUserBalanceResponseData(
+ this.userID,
+ aquarium.initialUserBalance(this.unsafeUserCreationIMEventMsg.getRole, this.unsafeUserCreationIMEventMsg.getOccurredMillis).toString()
+ )))
+
+ case (false, true) ⇒
+ // (Not CREATEd, with balance state)
+ // Clearly this is internal error
+ sender ! GetUserBalanceResponse(Left("Internal Server Error [AQU-BAL-0001]"), 500)
+
+ case (false, false) ⇒
+ // (Not CREATEd, no balance state)
+ // The user is completely unknown
+ sender ! GetUserBalanceResponse(Left("Unknown user %s [AQU-BAL-0004]".format(userID)), 404/*Not found*/)
+ }