Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / domain / AccountingInfo.java @ 1206:292dec4eae08

History | View | Annotate | Download (3.9 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package org.gss_project.gss.server.domain;
20

    
21
import java.io.Serializable;
22
import java.util.Date;
23

    
24
import javax.persistence.Column;
25
import javax.persistence.Entity;
26
import javax.persistence.GeneratedValue;
27
import javax.persistence.Id;
28
import javax.persistence.JoinColumn;
29
import javax.persistence.ManyToOne;
30
import javax.persistence.Table;
31
import javax.persistence.Temporal;
32
import javax.persistence.TemporalType;
33
import javax.persistence.UniqueConstraint;
34
import javax.persistence.Version;
35

    
36
import org.hibernate.annotations.Cache;
37
import org.hibernate.annotations.CacheConcurrencyStrategy;
38

    
39
/**
40
 * This class holds information about bandwidth usage by users.
41
 * This information is broken down in time periods.
42
 */
43
@Entity
44
@Table(name="accountinginfo", uniqueConstraints=@UniqueConstraint(columnNames={"user_id", "dateFrom", "dateTo"}))
45
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
46
public class AccountingInfo  implements Serializable{
47

    
48
        /**
49
         * The persistence ID of the object.
50
         */
51
        @SuppressWarnings("unused")
52
        @Id
53
        @GeneratedValue
54
        private Long id;
55

    
56
        /**
57
         * Version field for optimistic locking.
58
         * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
59
         */
60
        @SuppressWarnings("unused")
61
        @Version
62
        @Column(columnDefinition=" integer DEFAULT 0")
63
        private int version;
64

    
65
        /**
66
         * The user whose usage we are noting. We can never change it after
67
         * creation.
68
         */
69
        @ManyToOne
70
        @JoinColumn(name="user_id", updatable = false, nullable = false)
71
        private User user;
72

    
73
        /**
74
         * The start of the time period. We can never change it after
75
         * creation.
76
         */
77
        @Temporal(TemporalType.TIMESTAMP)
78
        @Column(name="dateFrom", updatable = false, nullable = false)
79
        private Date dateFrom;
80

    
81

    
82
        /**
83
         * The end of the time period. We can never change it after
84
         * creation.
85
         */
86
        @Temporal(TemporalType.TIMESTAMP)
87
        @Column(name="dateTo", updatable = false, nullable = false)
88
        private Date dateTo;
89

    
90

    
91
        /**
92
         * The usage itself, in bytes.
93
         */
94
        @Column(nullable = false)
95
        private long bandwidthUsed = 0;
96

    
97

    
98
        /**
99
         * Default constructor. Required by Hibernate,
100
         * but shouldn't be called.
101
         */
102
        @SuppressWarnings("unused")
103
        private AccountingInfo() {
104
        }
105

    
106

    
107
        /**
108
         * Constructor
109
         */
110
        public AccountingInfo(User _user, Date _dateFrom, Date _dateTo) {
111
                user = _user;
112
                dateFrom = _dateFrom;
113
                dateTo = _dateTo;
114
        }
115

    
116

    
117
        /**
118
         * Retrieve the user
119
         * @return the user
120
         */
121
        public User getUser() {
122
                return user;
123
        }
124

    
125

    
126
        /**
127
         * Retrieve the start of the time period.
128
         * @return the start of the time period
129
         */
130
        public Date getDateFrom() {
131
                return dateFrom;
132
        }
133

    
134

    
135
        /**
136
         * Retrieve the end of the time period.
137
         * @return the end of the time period
138
         */
139
        public Date getDateTo() {
140
                return dateTo;
141
        }
142

    
143

    
144
        /**
145
         * Retrieve the bandwidth used.
146
         * @return the bandwidth used
147
         */
148
        public long getBandwidthUsed() {
149
                return bandwidthUsed;
150
        }
151

    
152

    
153
        /**
154
         * Update bandwidth used upwards or downwards with new amount.
155
         * @param bandwidthDiff The amount by which to update;
156
         * positive for increase, negative for decrease.
157
         */
158
        public void updateBandwidth(long bandwidthDiff) {
159
                bandwidthUsed += bandwidthDiff;
160
        }
161

    
162
}