- Add constraint in order to avoid double entries due to concurrency. (Happened twice...
[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 /**\r
37  * This class holds information about bandwidth usage by users.\r
38  * This information is broken down in time periods.\r
39  */\r
40 @Entity\r
41 @Table(name="accountinginfo", uniqueConstraints=@UniqueConstraint(columnNames={"user_id", "dateFrom", "dateTo"}))\r
42 public class AccountingInfo  implements Serializable{\r
43 \r
44         /**\r
45          * The persistence ID of the object.\r
46          */\r
47         @SuppressWarnings("unused")\r
48         @Id\r
49         @GeneratedValue\r
50         private Long id;\r
51 \r
52         /**\r
53          * Version field for optimistic locking.\r
54          * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too\r
55          */\r
56         @SuppressWarnings("unused")\r
57         @Version\r
58         @Column(columnDefinition=" integer DEFAULT 0")\r
59         private int version;\r
60 \r
61         /**\r
62          * The user whose usage we are noting. We can never change it after\r
63          * creation.\r
64          */\r
65         @ManyToOne\r
66         @JoinColumn(name="user_id", updatable = false, nullable = false)\r
67         private User user;\r
68 \r
69         /**\r
70          * The start of the time period. We can never change it after\r
71          * creation.\r
72          */\r
73         @Temporal(TemporalType.TIMESTAMP)\r
74         @Column(name="dateFrom", updatable = false, nullable = false)\r
75         private Date dateFrom;\r
76 \r
77 \r
78         /**\r
79          * The end of the time period. We can never change it after\r
80          * creation.\r
81          */\r
82         @Temporal(TemporalType.TIMESTAMP)\r
83         @Column(name="dateTo", updatable = false, nullable = false)\r
84         private Date dateTo;\r
85 \r
86 \r
87         /**\r
88          * The usage itself, in bytes.\r
89          */\r
90         @Column(nullable = false)\r
91         private long bandwidthUsed = 0;\r
92 \r
93 \r
94         /**\r
95          * Default constructor. Required by Hibernate,\r
96          * but shouldn't be called.\r
97          */\r
98         @SuppressWarnings("unused")\r
99         private AccountingInfo() {\r
100         }\r
101 \r
102 \r
103         /**\r
104          * Constructor\r
105          */\r
106         public AccountingInfo(User _user, Date _dateFrom, Date _dateTo) {\r
107                 user = _user;\r
108                 dateFrom = _dateFrom;\r
109                 dateTo = _dateTo;\r
110         }\r
111 \r
112 \r
113         /**\r
114          * Retrieve the user\r
115          * @return the user\r
116          */\r
117         public User getUser() {\r
118                 return user;\r
119         }\r
120 \r
121 \r
122         /**\r
123          * Retrieve the start of the time period.\r
124          * @return the start of the time period\r
125          */\r
126         public Date getDateFrom() {\r
127                 return dateFrom;\r
128         }\r
129 \r
130 \r
131         /**\r
132          * Retrieve the end of the time period.\r
133          * @return the end of the time period\r
134          */\r
135         public Date getDateTo() {\r
136                 return dateTo;\r
137         }\r
138 \r
139 \r
140         /**\r
141          * Retrieve the bandwidth used.\r
142          * @return the bandwidth used\r
143          */\r
144         public long getBandwidthUsed() {\r
145                 return bandwidthUsed;\r
146         }\r
147 \r
148 \r
149         /**\r
150          * Update bandwidth used upwards or downwards with new amount.\r
151          * @param bandwidthDiff The amount by which to update;\r
152          * positive for increase, negative for decrease.\r
153          */\r
154         public void updateBandwidth(long bandwidthDiff) {\r
155                 bandwidthUsed += bandwidthDiff;\r
156         }\r
157 \r
158 }\r