root / src / main / scala / gr / grnet / aquarium / charging / state / UserStateModel.scala @ 4b8ff3e0
History | View | Annotate | Download (6 kB)
1 |
/* |
---|---|
2 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or |
5 |
* without modification, are permitted provided that the following |
6 |
* conditions are met: |
7 |
* |
8 |
* 1. Redistributions of source code must retain the above |
9 |
* copyright notice, this list of conditions and the following |
10 |
* disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above |
13 |
* copyright notice, this list of conditions and the following |
14 |
* disclaimer in the documentation and/or other materials |
15 |
* provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
23 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
24 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
25 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
27 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 |
* POSSIBILITY OF SUCH DAMAGE. |
29 |
* |
30 |
* The views and conclusions contained in the software and |
31 |
* documentation are those of the authors and should not be |
32 |
* interpreted as representing official policies, either expressed |
33 |
* or implied, of GRNET S.A. |
34 |
*/ |
35 |
|
36 |
package gr.grnet.aquarium.charging.state |
37 |
|
38 |
import gr.grnet.aquarium.message.avro.gen.{UserAgreementMsg, IMEventMsg, UserAgreementHistoryMsg, UserStateMsg} |
39 |
import gr.grnet.aquarium.Real |
40 |
import scala.collection.immutable |
41 |
import gr.grnet.aquarium.policy.UserAgreementModel |
42 |
import gr.grnet.aquarium.message.avro.{MessageFactory, MessageHelpers, ModelFactory} |
43 |
import gr.grnet.aquarium.logic.accounting.dsl.Timeslot |
44 |
|
45 |
/** |
46 |
* |
47 |
* A wrapper around [[gr.grnet.aquarium.message.avro.gen.UserStateMsg]] and |
48 |
* [[]] with convenient (sorted) |
49 |
* user agreement history. |
50 |
* |
51 |
* @author Christos KK Loverdos <loverdos@gmail.com> |
52 |
*/ |
53 |
|
54 |
final class UserStateModel( |
55 |
private[this] var _userStateMsg: UserStateMsg, |
56 |
private[this] var _userAgreementHistoryMsg: UserAgreementHistoryMsg |
57 |
) { |
58 |
require(this._userStateMsg ne null, "this._userStateMsg ne null") |
59 |
require(this._userAgreementHistoryMsg ne null, "this._userAgreementHistoryMsg ne null") |
60 |
|
61 |
private[this] var _isInitial = true |
62 |
private[this] var _latestIMEventOccurredMillis = 0L |
63 |
private[this] var _userCreationIMEventMsgOpt: Option[IMEventMsg] = None |
64 |
|
65 |
private[this] var _userAgreementModels: immutable.SortedSet[UserAgreementModel] = { |
66 |
var userAgreementModels = immutable.SortedSet[UserAgreementModel]() |
67 |
val userAgreements = _userAgreementHistoryMsg.getAgreements.iterator() |
68 |
while(userAgreements.hasNext) { |
69 |
val userAgreement = userAgreements.next() |
70 |
val userAgreementModel = ModelFactory.newUserAgreementModel(userAgreement) |
71 |
userAgreementModels += userAgreementModel |
72 |
|
73 |
checkUserCreationIMEvent(userAgreement.getRelatedIMEventMsg) |
74 |
checkLatestIMEventOccurredMillis(userAgreement.getRelatedIMEventMsg) |
75 |
} |
76 |
|
77 |
userAgreementModels |
78 |
} |
79 |
|
80 |
private[this] def checkUserCreationIMEvent(imEvent: IMEventMsg) { |
81 |
if(MessageHelpers.isUserCreationIMEvent(imEvent)) { |
82 |
this._userCreationIMEventMsgOpt = Some(imEvent) |
83 |
} |
84 |
} |
85 |
private[this] def checkLatestIMEventOccurredMillis(imEvent: IMEventMsg) { |
86 |
if(imEvent ne null) { |
87 |
if(this._latestIMEventOccurredMillis < imEvent.getOccurredMillis) { |
88 |
this._latestIMEventOccurredMillis = imEvent.getOccurredMillis |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
private[this] def updateOtherVars(imEvent: IMEventMsg) { |
94 |
this._isInitial = false |
95 |
checkUserCreationIMEvent(imEvent) |
96 |
checkLatestIMEventOccurredMillis(imEvent) |
97 |
} |
98 |
|
99 |
def isInitial = this._isInitial |
100 |
|
101 |
def userID = this._userAgreementHistoryMsg.getUserID |
102 |
|
103 |
def latestIMEventOccurredMillis = this._latestIMEventOccurredMillis |
104 |
|
105 |
def hasUserCreationEvent = this._userCreationIMEventMsgOpt.isDefined |
106 |
|
107 |
def userCreationIMEventOpt = this._userCreationIMEventMsgOpt |
108 |
|
109 |
def unsafeUserCreationIMEvent = this._userCreationIMEventMsgOpt.get |
110 |
|
111 |
def unsafeUserCreationMillis = unsafeUserCreationIMEvent.getOccurredMillis |
112 |
|
113 |
def size: Int = _userAgreementHistoryMsg.getAgreements.size() |
114 |
|
115 |
def userStateMsg = this._userStateMsg |
116 |
|
117 |
def updateUserStateMsg(msg: UserStateMsg) { |
118 |
this._userStateMsg = msg |
119 |
this._isInitial = false |
120 |
} |
121 |
|
122 |
def userAgreementHistoryMsg = this._userAgreementHistoryMsg |
123 |
|
124 |
def updateUserAgreementHistoryMsg(msg: UserAgreementHistoryMsg) { |
125 |
this._userAgreementHistoryMsg = msg |
126 |
this._isInitial = false |
127 |
} |
128 |
|
129 |
def agreementByTimeslot: immutable.SortedMap[Timeslot, UserAgreementModel] = { |
130 |
immutable.TreeMap(_userAgreementModels.map(ag ⇒ (ag.timeslot, ag)).toSeq: _*) |
131 |
} |
132 |
|
133 |
def insertUserAgreementModel(userAgreement: UserAgreementModel) { |
134 |
MessageHelpers.insertUserAgreement(this._userAgreementHistoryMsg, userAgreement.msg) |
135 |
|
136 |
this._userAgreementModels += userAgreement |
137 |
|
138 |
updateOtherVars(userAgreement.msg.getRelatedIMEventMsg) |
139 |
} |
140 |
|
141 |
def insertUserAgreementMsg(userAgreementMsg: UserAgreementMsg) { |
142 |
insertUserAgreementModel(ModelFactory.newUserAgreementModel(userAgreementMsg)) |
143 |
} |
144 |
|
145 |
def insertUserAgreementMsgFromIMEvent(imEvent: IMEventMsg) { |
146 |
val userAgreementMsg = MessageFactory.newUserAgreementFromIMEventMsg(imEvent) |
147 |
insertUserAgreementMsg(userAgreementMsg) |
148 |
} |
149 |
|
150 |
def oldestAgreementModel: Option[UserAgreementModel] = { |
151 |
_userAgreementModels.headOption |
152 |
} |
153 |
|
154 |
def newestAgreementModel: Option[UserAgreementModel] = { |
155 |
_userAgreementModels.lastOption |
156 |
} |
157 |
|
158 |
def latestResourceEventOccurredMillis = this._userStateMsg.getLatestResourceEventOccurredMillis |
159 |
|
160 |
@inline final def totalCreditsAsReal: Real = Real(this._userStateMsg.getTotalCredits) |
161 |
|
162 |
@inline final def totalCredits: String = this._userStateMsg.getTotalCredits |
163 |
|
164 |
override def toString = _userStateMsg.toString |
165 |
|
166 |
} |