Use EHCache for second-level caching.
[pithos] / src / gr / ebs / gss / server / domain / AccountingInfo.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.server.domain;\r
20 \r
21 import java.io.Serializable;\r
22 import java.util.Date;\r
23 \r
24 import javax.persistence.Column;\r
25 import javax.persistence.Entity;\r
26 import javax.persistence.GeneratedValue;\r
27 import javax.persistence.Id;\r
28 import javax.persistence.JoinColumn;\r
29 import javax.persistence.ManyToOne;\r
30 import javax.persistence.Table;\r
31 import javax.persistence.Temporal;\r
32 import javax.persistence.TemporalType;\r
33 import javax.persistence.UniqueConstraint;\r
34 import javax.persistence.Version;\r
35 \r
36 import org.hibernate.annotations.Cache;\r
37 import org.hibernate.annotations.CacheConcurrencyStrategy;\r
38 \r
39 /**\r
40  * This class holds information about bandwidth usage by users.\r
41  * This information is broken down in time periods.\r
42  */\r
43 @Entity\r
44 @Table(name="accountinginfo", uniqueConstraints=@UniqueConstraint(columnNames={"user_id", "dateFrom", "dateTo"}))\r
45 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)\r
46 public class AccountingInfo  implements Serializable{\r
47 \r
48         /**\r
49          * The persistence ID of the object.\r
50          */\r
51         @SuppressWarnings("unused")\r
52         @Id\r
53         @GeneratedValue\r
54         private Long id;\r
55 \r
56         /**\r
57          * Version field for optimistic locking.\r
58          * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too\r
59          */\r
60         @SuppressWarnings("unused")\r
61         @Version\r
62         @Column(columnDefinition=" integer DEFAULT 0")\r
63         private int version;\r
64 \r
65         /**\r
66          * The user whose usage we are noting. We can never change it after\r
67          * creation.\r
68          */\r
69         @ManyToOne\r
70         @JoinColumn(name="user_id", updatable = false, nullable = false)\r
71         private User user;\r
72 \r
73         /**\r
74          * The start of the time period. We can never change it after\r
75          * creation.\r
76          */\r
77         @Temporal(TemporalType.TIMESTAMP)\r
78         @Column(name="dateFrom", updatable = false, nullable = false)\r
79         private Date dateFrom;\r
80 \r
81 \r
82         /**\r
83          * The end of the time period. We can never change it after\r
84          * creation.\r
85          */\r
86         @Temporal(TemporalType.TIMESTAMP)\r
87         @Column(name="dateTo", updatable = false, nullable = false)\r
88         private Date dateTo;\r
89 \r
90 \r
91         /**\r
92          * The usage itself, in bytes.\r
93          */\r
94         @Column(nullable = false)\r
95         private long bandwidthUsed = 0;\r
96 \r
97 \r
98         /**\r
99          * Default constructor. Required by Hibernate,\r
100          * but shouldn't be called.\r
101          */\r
102         @SuppressWarnings("unused")\r
103         private AccountingInfo() {\r
104         }\r
105 \r
106 \r
107         /**\r
108          * Constructor\r
109          */\r
110         public AccountingInfo(User _user, Date _dateFrom, Date _dateTo) {\r
111                 user = _user;\r
112                 dateFrom = _dateFrom;\r
113                 dateTo = _dateTo;\r
114         }\r
115 \r
116 \r
117         /**\r
118          * Retrieve the user\r
119          * @return the user\r
120          */\r
121         public User getUser() {\r
122                 return user;\r
123         }\r
124 \r
125 \r
126         /**\r
127          * Retrieve the start of the time period.\r
128          * @return the start of the time period\r
129          */\r
130         public Date getDateFrom() {\r
131                 return dateFrom;\r
132         }\r
133 \r
134 \r
135         /**\r
136          * Retrieve the end of the time period.\r
137          * @return the end of the time period\r
138          */\r
139         public Date getDateTo() {\r
140                 return dateTo;\r
141         }\r
142 \r
143 \r
144         /**\r
145          * Retrieve the bandwidth used.\r
146          * @return the bandwidth used\r
147          */\r
148         public long getBandwidthUsed() {\r
149                 return bandwidthUsed;\r
150         }\r
151 \r
152 \r
153         /**\r
154          * Update bandwidth used upwards or downwards with new amount.\r
155          * @param bandwidthDiff The amount by which to update;\r
156          * positive for increase, negative for decrease.\r
157          */\r
158         public void updateBandwidth(long bandwidthDiff) {\r
159                 bandwidthUsed += bandwidthDiff;\r
160         }\r
161 \r
162 }\r