]> git.mxchange.org Git - jbonuscard-core.git/blob - src/org/mxchange/jfinancials/model/income/FinancialIncome.java
08e6225a5f57ef373c32154f600cb7a516fa9d51
[jbonuscard-core.git] / src / org / mxchange / jfinancials / model / income / FinancialIncome.java
1 /*
2  * Copyright (C) 2017 - 2020 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.model.income;
18
19 import java.math.BigDecimal;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.mxchange.jcoreutils.Comparables;
38 import org.mxchange.jfinancials.model.income.interval.FinancialInterval;
39 import org.mxchange.jusercore.model.user.LoginUser;
40 import org.mxchange.jusercore.model.user.User;
41
42 /**
43  * An entity for income data
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Entity (name = "income")
48 @Table (name = "income")
49 @SuppressWarnings ("PersistenceUnitPresent")
50 public class FinancialIncome implements BillableIncome {
51
52         /**
53          * Serial number
54          */
55         @Transient
56         private static final long serialVersionUID = 173_587_690_625_524L;
57
58         /**
59          * Income created timestamp
60          */
61         @Basic (optional = false)
62         @Temporal (TemporalType.TIMESTAMP)
63         @Column (name = "income_created", nullable = false, updatable = false)
64         private Date incomeCreated;
65
66         /**
67          * Income enabled (default) or disabled (no longer receiving income but need
68          * to keep it for statistics).
69          */
70         @Basic (optional = false)
71         @Column (name = "income_enabled", nullable = false)
72         private Boolean incomeEnabled;
73
74         /**
75          * Income id (primary key)
76          */
77         @Id
78         @GeneratedValue (strategy = GenerationType.IDENTITY)
79         @Column (name = "income_id", nullable = false, updatable = false)
80         private Long incomeId;
81
82         /**
83          * Income interval
84          */
85         @Basic (optional = false)
86         @Column (name = "income_interval", nullable = false)
87         @Enumerated (EnumType.STRING)
88         private FinancialInterval incomeInterval;
89
90         /**
91          * Income single amount
92          */
93         @Basic (optional = false)
94         @Column (name = "income_single_amount", nullable = false)
95         private BigDecimal incomeSingleAmount;
96
97         /**
98          * Income title
99          */
100         @Basic (optional = false)
101         @Column (name = "income_title", nullable = false)
102         private String incomeTitle;
103
104         /**
105          * Income updated timestamp
106          */
107         @Temporal (TemporalType.TIMESTAMP)
108         @Column (name = "income_updated", insertable = false)
109         private Date incomeUpdated;
110
111         /**
112          * Connected user account
113          */
114         @JoinColumn (name = "income_user_id", referencedColumnName = "user_id", nullable = false, updatable = false)
115         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
116         private User incomeUser;
117
118         /**
119          * Default constructor
120          */
121         public FinancialIncome () {
122         }
123
124         /**
125          * Constructor with all fields, except timestamps. These have to be set at a
126          * proper place
127          * <p>
128          * @param incomeTitle        Income (type) title
129          * @param incomeSingleAmount Single amount
130          * @param incomeInterval     Interval
131          * @param incomeUser         Connected user
132          */
133         public FinancialIncome (final String incomeTitle, final BigDecimal incomeSingleAmount, final FinancialInterval incomeInterval, final User incomeUser) {
134                 // Invoke default constructor
135                 this();
136
137                 // Set all fields
138                 this.incomeTitle = incomeTitle;
139                 this.incomeSingleAmount = incomeSingleAmount;
140                 this.incomeInterval = incomeInterval;
141                 this.incomeUser = incomeUser;
142
143                 // Enable this income by default
144                 this.incomeEnabled = Boolean.TRUE;
145         }
146
147         @Override
148         public int compareTo (final BillableIncome billableIncome) {
149                 // Check parameter on null-reference and equality to this
150                 if (null == billableIncome) {
151                         // Should not happen
152                         throw new NullPointerException("billableIncome is null"); //NOI18N
153                 } else if (billableIncome.equals(this)) {
154                         // Same object
155                         return 0;
156                 }
157
158                 // Init comparators
159                 final int comparators[] = {
160                         // First compare title
161                         this.getIncomeTitle().compareToIgnoreCase(billableIncome.getIncomeTitle()),
162                         // ... then compare interval
163                         this.getIncomeInterval().compareTo(billableIncome.getIncomeInterval()),
164                         // ... value
165                         this.getIncomeSingleAmount().compareTo(billableIncome.getIncomeSingleAmount()),
166                         // ... user instance
167                         this.getIncomeUser().compareTo(billableIncome.getIncomeUser())
168                 };
169
170                 // Check all values
171                 final int comparison = Comparables.checkAll(comparators);
172
173                 // Return value
174                 return comparison;
175         }
176
177         @Override
178         public boolean equals (final Object object) {
179                 if (this == object) {
180                         return true;
181                 } else if (null == object) {
182                         return false;
183                 } else if (this.getClass() != object.getClass()) {
184                         return false;
185                 }
186
187                 final BillableIncome other = (BillableIncome) object;
188
189                 if (!Objects.equals(this.getIncomeTitle(), other.getIncomeTitle())) {
190                         return false;
191                 } else if (!Objects.equals(this.getIncomeId(), other.getIncomeId())) {
192                         return false;
193                 } else if (!Objects.equals(this.getIncomeUser(), other.getIncomeUser())) {
194                         return false;
195                 }
196                 return true;
197         }
198
199         @Override
200         @SuppressWarnings ("ReturnOfDateField")
201         public Date getIncomeCreated () {
202                 return this.incomeCreated;
203         }
204
205         @Override
206         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
207         public void setIncomeCreated (Date incomeCreated) {
208                 this.incomeCreated = incomeCreated;
209         }
210
211         @Override
212         public Boolean getIncomeEnabled () {
213                 return this.incomeEnabled;
214         }
215
216         @Override
217         public void setIncomeEnabled (Boolean incomeEnabled) {
218                 this.incomeEnabled = incomeEnabled;
219         }
220
221         @Override
222         public Long getIncomeId () {
223                 return this.incomeId;
224         }
225
226         @Override
227         public void setIncomeId (final Long incomeId) {
228                 this.incomeId = incomeId;
229         }
230
231         @Override
232         public FinancialInterval getIncomeInterval () {
233                 return this.incomeInterval;
234         }
235
236         @Override
237         public void setIncomeInterval (final FinancialInterval incomeInterval) {
238                 this.incomeInterval = incomeInterval;
239         }
240
241         @Override
242         public BigDecimal getIncomeSingleAmount () {
243                 return this.incomeSingleAmount;
244         }
245
246         @Override
247         public void setIncomeSingleAmount (final BigDecimal incomeSingleAmount) {
248                 this.incomeSingleAmount = incomeSingleAmount;
249         }
250
251         @Override
252         public String getIncomeTitle () {
253                 return this.incomeTitle;
254         }
255
256         @Override
257         public void setIncomeTitle (String incomeTitle) {
258                 this.incomeTitle = incomeTitle;
259         }
260
261         @Override
262         @SuppressWarnings ("ReturnOfDateField")
263         public Date getIncomeUpdated () {
264                 return this.incomeUpdated;
265         }
266
267         @Override
268         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
269         public void setIncomeUpdated (Date incomeUpdated) {
270                 this.incomeUpdated = incomeUpdated;
271         }
272
273         @Override
274         public User getIncomeUser () {
275                 return this.incomeUser;
276         }
277
278         @Override
279         public void setIncomeUser (User incomeUser) {
280                 this.incomeUser = incomeUser;
281         }
282
283         @Override
284         public int hashCode () {
285                 int hash = 3;
286
287                 hash = 29 * hash + Objects.hashCode(this.getIncomeId());
288                 hash = 29 * hash + Objects.hashCode(this.getIncomeTitle());
289                 hash = 29 * hash + Objects.hashCode(this.getIncomeUser());
290
291                 return hash;
292         }
293
294 }