root / src / main / scala / gr / grnet / aquarium / charging / state / UserAgreementHistoryModel.scala @ 4b8ff3e0
History | View | Annotate | Download (4.9 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.logic.accounting.dsl.Timeslot |
39 |
import gr.grnet.aquarium.policy.UserAgreementModel |
40 |
import gr.grnet.aquarium.util.json.JsonSupport |
41 |
import scala.collection.immutable |
42 |
import gr.grnet.aquarium.message.avro.gen.{UserAgreementMsg, IMEventMsg, UserAgreementHistoryMsg} |
43 |
import gr.grnet.aquarium.message.avro.{MessageFactory, MessageHelpers, ModelFactory} |
44 |
|
45 |
/** |
46 |
* |
47 |
* @author Christos KK Loverdos <loverdos@gmail.com> |
48 |
*/ |
49 |
|
50 |
final class UserAgreementHistoryModel(val userAgreementHistoryMsg: UserAgreementHistoryMsg) { |
51 |
|
52 |
private[this] var _latestIMEventOccurredMillis = 0L |
53 |
private[this] var _userCreationIMEventMsgOpt: Option[IMEventMsg] = None |
54 |
|
55 |
private[this] var _userAgreementModels: immutable.SortedSet[UserAgreementModel] = { |
56 |
var userAgreementModels = immutable.SortedSet[UserAgreementModel]() |
57 |
val userAgreements = userAgreementHistoryMsg.getAgreements.iterator() |
58 |
while(userAgreements.hasNext) { |
59 |
val userAgreement = userAgreements.next() |
60 |
val userAgreementModel = ModelFactory.newUserAgreementModel(userAgreement) |
61 |
userAgreementModels += userAgreementModel |
62 |
|
63 |
checkUserCreationIMEvent(userAgreement.getRelatedIMEventMsg) |
64 |
checkLatestIMEventOccurredMillis(userAgreement.getRelatedIMEventMsg) |
65 |
} |
66 |
|
67 |
userAgreementModels |
68 |
} |
69 |
|
70 |
private[this] def checkUserCreationIMEvent(imEvent: IMEventMsg) { |
71 |
if(MessageHelpers.isUserCreationIMEvent(imEvent)) { |
72 |
this._userCreationIMEventMsgOpt = Some(imEvent) |
73 |
} |
74 |
} |
75 |
private[this] def checkLatestIMEventOccurredMillis(imEvent: IMEventMsg) { |
76 |
if(imEvent ne null) { |
77 |
if(this._latestIMEventOccurredMillis < imEvent.getOccurredMillis) { |
78 |
this._latestIMEventOccurredMillis = imEvent.getOccurredMillis |
79 |
} |
80 |
} |
81 |
} |
82 |
|
83 |
private[this] def updateOtherVars(imEvent: IMEventMsg) { |
84 |
checkUserCreationIMEvent(imEvent) |
85 |
checkLatestIMEventOccurredMillis(imEvent) |
86 |
} |
87 |
|
88 |
def userID = this.userAgreementHistoryMsg.getUserID |
89 |
|
90 |
def latestIMEventOccurredMillis = this._latestIMEventOccurredMillis |
91 |
|
92 |
def hasUserCreationEvent = this._userCreationIMEventMsgOpt.isDefined |
93 |
|
94 |
def userCreationIMEventOpt = this._userCreationIMEventMsgOpt |
95 |
|
96 |
def unsafeUserCreationIMEvent = this._userCreationIMEventMsgOpt.get |
97 |
|
98 |
def unsafeUserCreationMillis = unsafeUserCreationIMEvent.getOccurredMillis |
99 |
|
100 |
def size: Int = userAgreementHistoryMsg.getAgreements.size() |
101 |
|
102 |
def agreementByTimeslot: immutable.SortedMap[Timeslot, UserAgreementModel] = { |
103 |
immutable.TreeMap(_userAgreementModels.map(ag ⇒ (ag.timeslot, ag)).toSeq: _*) |
104 |
} |
105 |
|
106 |
def insertUserAgreementModel(userAgreement: UserAgreementModel) { |
107 |
MessageHelpers.insertUserAgreement(this.userAgreementHistoryMsg, userAgreement.msg) |
108 |
|
109 |
this._userAgreementModels += userAgreement |
110 |
|
111 |
updateOtherVars(userAgreement.msg.getRelatedIMEventMsg) |
112 |
} |
113 |
|
114 |
def insertUserAgreementMsg(userAgreementMsg: UserAgreementMsg) { |
115 |
insertUserAgreementModel(ModelFactory.newUserAgreementModel(userAgreementMsg)) |
116 |
} |
117 |
|
118 |
def insertUserAgreementMsgFromIMEvent(imEvent: IMEventMsg) { |
119 |
val userAgreementMsg = MessageFactory.newUserAgreementFromIMEventMsg(imEvent) |
120 |
insertUserAgreementMsg(userAgreementMsg) |
121 |
} |
122 |
|
123 |
def oldestAgreementModel: Option[UserAgreementModel] = { |
124 |
_userAgreementModels.headOption |
125 |
} |
126 |
|
127 |
def newestAgreementModel: Option[UserAgreementModel] = { |
128 |
_userAgreementModels.lastOption |
129 |
} |
130 |
|
131 |
override def toString = userAgreementHistoryMsg.toString |
132 |
} |
133 |
|