]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/income/FinancialIncome.java
Continued:
[jfinancials-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.text.MessageFormat;
21 import java.util.Date;
22 import java.util.Objects;
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.EnumType;
28 import javax.persistence.Enumerated;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
37 import javax.persistence.Transient;
38 import org.mxchange.jcoreutils.Comparables;
39 import org.mxchange.jfinancials.model.income.interval.FinancialInterval;
40 import org.mxchange.jusercore.model.user.LoginUser;
41 import org.mxchange.jusercore.model.user.User;
42
43 /**
44  * An entity for income data
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Entity (name = "income")
49 @Table (name = "income")
50 @SuppressWarnings ("PersistenceUnitPresent")
51 public class FinancialIncome implements BillableIncome {
52
53         /**
54          * Serial number
55          */
56         @Transient
57         private static final long serialVersionUID = 173_587_690_625_524L;
58
59         /**
60          * Income enabled (default) or disabled (no longer receiving income but need
61          * to keep it for statistics).
62          */
63         @Basic (optional = false)
64         @Column (name = "income_enabled", nullable = false)
65         private Boolean incomeEnabled;
66
67         /**
68          * Income created timestamp
69          */
70         @Basic (optional = false)
71         @Temporal (TemporalType.TIMESTAMP)
72         @Column (name = "income_entry_created", nullable = false, updatable = false)
73         private Date incomeEntryCreated;
74
75         /**
76          * Income updated timestamp
77          */
78         @Temporal (TemporalType.TIMESTAMP)
79         @Column (name = "income_entry_updated", insertable = false)
80         private Date incomeEntryUpdated;
81
82         /**
83          * Income id (primary key)
84          */
85         @Id
86         @GeneratedValue (strategy = GenerationType.IDENTITY)
87         @Column (name = "income_id", nullable = false, updatable = false)
88         private Long incomeId;
89
90         /**
91          * Income interval
92          */
93         @Basic (optional = false)
94         @Column (name = "income_interval", nullable = false)
95         @Enumerated (EnumType.STRING)
96         private FinancialInterval incomeInterval;
97
98         /**
99          * Income single amount
100          */
101         @Basic (optional = false)
102         @Column (name = "income_single_amount", nullable = false)
103         private BigDecimal incomeSingleAmount;
104
105         /**
106          * Income title
107          */
108         @Basic (optional = false)
109         @Column (name = "income_title", nullable = false)
110         private String incomeTitle;
111
112         /**
113          * Connected user account
114          */
115         @JoinColumn (name = "income_user_id", referencedColumnName = "user_id", nullable = false, updatable = false)
116         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
117         private User incomeUser;
118
119         /**
120          * Default constructor
121          */
122         public FinancialIncome () {
123         }
124
125         /**
126          * Constructor with all fields, except timestamps. These have to be set at a
127          * proper place
128          * <p>
129          * @param incomeTitle        Income (type) title
130          * @param incomeSingleAmount Single amount
131          * @param incomeInterval     Interval
132          * @param incomeUser         Connected user
133          * @param incomeEnabled      Whether this income is enabled
134          */
135         public FinancialIncome (final String incomeTitle, final BigDecimal incomeSingleAmount, final FinancialInterval incomeInterval, final User incomeUser, final Boolean incomeEnabled) {
136                 // Invoke default constructor
137                 this();
138
139                 // Validate all parameter
140                 if (null == incomeEnabled) {
141                         // Throw NPE
142                         throw new NullPointerException("incomeEnabled is null"); //NOI18N
143                 } else if (null == incomeInterval) {
144                         // Throw NPE again
145                         throw new NullPointerException("incomeInterval is null"); //NOI18N
146                 } else if (null == incomeSingleAmount) {
147                         // Throw NPE again
148                         throw new NullPointerException("incomeSingleAmount is null"); //NOI18N
149                 } else if (incomeSingleAmount.floatValue() < 0.0f) {
150                         // Throw IAE
151                         throw new IllegalArgumentException(MessageFormat.format("incomeSingleAmount={0} is not valid.", incomeSingleAmount)); //NOI18N
152                 } else if (null == incomeTitle) {
153                         // Throw NPE
154                         throw new NullPointerException("incomeTitle is null"); //NOI18N
155                 } else if (incomeTitle.isEmpty()) {
156                         // Throw IAE
157                         throw new IllegalArgumentException("incomeTitle is empty"); //NOI18N
158                 } else if (null == incomeUser) {
159                         // Throw NPE
160                         throw new NullPointerException("incomeUser is null"); //NOI18N
161                 } else if (incomeUser.getUserId() == null) {
162                         // Throw NPE again
163                         throw new NullPointerException("incomeUser.userId is null"); //NOI18N
164                 } else if (incomeUser.getUserId() < 1) {
165                         // Throw IAE
166                         throw new IllegalArgumentException(MessageFormat.format("incomeUser.userId={0} is not valid.", incomeUser.getUserId())); //NOI18N
167                 }
168
169                 // Set all fields
170                 this.incomeTitle = incomeTitle;
171                 this.incomeSingleAmount = incomeSingleAmount;
172                 this.incomeInterval = incomeInterval;
173                 this.incomeUser = incomeUser;
174                 this.incomeEnabled = incomeEnabled;
175         }
176
177         @Override
178         public int compareTo (final BillableIncome billableIncome) {
179                 // Check parameter on null-reference and equality to this
180                 if (null == billableIncome) {
181                         // Should not happen
182                         throw new NullPointerException("billableIncome is null"); //NOI18N
183                 } else if (billableIncome.equals(this)) {
184                         // Same object
185                         return 0;
186                 }
187
188                 // Init comparators
189                 final int comparators[] = {
190                         // First compare title
191                         this.getIncomeTitle().compareToIgnoreCase(billableIncome.getIncomeTitle()),
192                         // ... then compare interval
193                         this.getIncomeInterval().compareTo(billableIncome.getIncomeInterval()),
194                         // ... value
195                         this.getIncomeSingleAmount().compareTo(billableIncome.getIncomeSingleAmount()),
196                         // ... user instance
197                         this.getIncomeUser().compareTo(billableIncome.getIncomeUser())
198                 };
199
200                 // Check all values
201                 final int comparison = Comparables.checkAll(comparators);
202
203                 // Return value
204                 return comparison;
205         }
206
207         @Override
208         public boolean equals (final Object object) {
209                 if (this == object) {
210                         return true;
211                 } else if (null == object) {
212                         return false;
213                 } else if (this.getClass() != object.getClass()) {
214                         return false;
215                 }
216
217                 final BillableIncome other = (BillableIncome) object;
218
219                 if (!Objects.equals(this.getIncomeEnabled(), other.getIncomeEnabled())) {
220                         return false;
221                 } else if (!Objects.equals(this.getIncomeId(), other.getIncomeId())) {
222                         return false;
223                 } else if (!Objects.equals(this.getIncomeInterval(), other.getIncomeInterval())) {
224                         return false;
225                 } else if (!Objects.equals(this.getIncomeSingleAmount(), other.getIncomeSingleAmount())) {
226                         return false;
227                 } else if (!Objects.equals(this.getIncomeTitle(), other.getIncomeTitle())) {
228                         return false;
229                 } else if (!Objects.equals(this.getIncomeUser(), other.getIncomeUser())) {
230                         return false;
231                 }
232
233                 return true;
234         }
235
236         @Override
237         public Boolean getIncomeEnabled () {
238                 return this.incomeEnabled;
239         }
240
241         @Override
242         public void setIncomeEnabled (Boolean incomeEnabled) {
243                 this.incomeEnabled = incomeEnabled;
244         }
245
246         @Override
247         @SuppressWarnings ("ReturnOfDateField")
248         public Date getIncomeEntryCreated () {
249                 return this.incomeEntryCreated;
250         }
251
252         @Override
253         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
254         public void setIncomeEntryCreated (Date incomeEntryCreated) {
255                 this.incomeEntryCreated = incomeEntryCreated;
256         }
257
258         @Override
259         @SuppressWarnings ("ReturnOfDateField")
260         public Date getIncomeEntryUpdated () {
261                 return this.incomeEntryUpdated;
262         }
263
264         @Override
265         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
266         public void setIncomeEntryUpdated (Date incomeEntryUpdated) {
267                 this.incomeEntryUpdated = incomeEntryUpdated;
268         }
269
270         @Override
271         public Long getIncomeId () {
272                 return this.incomeId;
273         }
274
275         @Override
276         public void setIncomeId (final Long incomeId) {
277                 this.incomeId = incomeId;
278         }
279
280         @Override
281         public FinancialInterval getIncomeInterval () {
282                 return this.incomeInterval;
283         }
284
285         @Override
286         public void setIncomeInterval (final FinancialInterval incomeInterval) {
287                 this.incomeInterval = incomeInterval;
288         }
289
290         @Override
291         public BigDecimal getIncomeSingleAmount () {
292                 return this.incomeSingleAmount;
293         }
294
295         @Override
296         public void setIncomeSingleAmount (final BigDecimal incomeSingleAmount) {
297                 this.incomeSingleAmount = incomeSingleAmount;
298         }
299
300         @Override
301         public String getIncomeTitle () {
302                 return this.incomeTitle;
303         }
304
305         @Override
306         public void setIncomeTitle (String incomeTitle) {
307                 this.incomeTitle = incomeTitle;
308         }
309
310         @Override
311         public User getIncomeUser () {
312                 return this.incomeUser;
313         }
314
315         @Override
316         public void setIncomeUser (User incomeUser) {
317                 this.incomeUser = incomeUser;
318         }
319
320         @Override
321         public int hashCode () {
322                 int hash = 3;
323
324                 hash = 29 * hash + Objects.hashCode(this.getIncomeEnabled());
325                 hash = 29 * hash + Objects.hashCode(this.getIncomeId());
326                 hash = 29 * hash + Objects.hashCode(this.getIncomeInterval());
327                 hash = 29 * hash + Objects.hashCode(this.getIncomeSingleAmount());
328                 hash = 29 * hash + Objects.hashCode(this.getIncomeTitle());
329                 hash = 29 * hash + Objects.hashCode(this.getIncomeUser());
330
331                 return hash;
332         }
333
334 }