2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.model.income;
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.comparable.ComparableUtils;
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;
44 * An entity for income data
46 * @author Roland Häder<roland@mxchange.org>
48 @Entity (name = "income")
49 @Table (name = "income")
50 @SuppressWarnings ("PersistenceUnitPresent")
51 public class FinancialIncome implements BillableIncome {
57 private static final long serialVersionUID = 173_587_690_625_524L;
60 * Income enabled (default) or disabled (no longer receiving income but need
61 * to keep it for statistics).
63 @Basic (optional = false)
64 @Column (name = "income_enabled", nullable = false)
65 private Boolean incomeEnabled;
68 * Income created timestamp
70 @Basic (optional = false)
71 @Temporal (TemporalType.TIMESTAMP)
72 @Column (name = "income_entry_created", nullable = false, updatable = false)
73 private Date incomeEntryCreated;
76 * Income updated timestamp
78 @Temporal (TemporalType.TIMESTAMP)
79 @Column (name = "income_entry_updated", insertable = false)
80 private Date incomeEntryUpdated;
83 * Income id (primary key)
86 @GeneratedValue (strategy = GenerationType.IDENTITY)
87 @Column (name = "income_id", nullable = false, updatable = false)
88 private Long incomeId;
93 @Basic (optional = false)
94 @Column (name = "income_interval", nullable = false)
95 @Enumerated (EnumType.STRING)
96 private FinancialInterval incomeInterval;
99 * Income single amount
101 @Basic (optional = false)
102 @Column (name = "income_single_amount", nullable = false)
103 private BigDecimal incomeSingleAmount;
108 @Basic (optional = false)
109 @Column (name = "income_title", nullable = false)
110 private String incomeTitle;
113 * Connected user account
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;
120 * Default constructor
122 public FinancialIncome () {
126 * Constructor with all fields, except timestamps. These have to be set at a
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
135 public FinancialIncome (final String incomeTitle, final BigDecimal incomeSingleAmount, final FinancialInterval incomeInterval, final User incomeUser, final Boolean incomeEnabled) {
136 // Invoke default constructor
139 // Validate all parameter
140 if (null == incomeEnabled) {
142 throw new NullPointerException("incomeEnabled is null"); //NOI18N
143 } else if (null == incomeInterval) {
145 throw new NullPointerException("incomeInterval is null"); //NOI18N
146 } else if (null == incomeSingleAmount) {
148 throw new NullPointerException("incomeSingleAmount is null"); //NOI18N
149 } else if (incomeSingleAmount.floatValue() < 0.0f) {
151 throw new IllegalArgumentException(MessageFormat.format("incomeSingleAmount={0} is not valid.", incomeSingleAmount)); //NOI18N
152 } else if (null == incomeTitle) {
154 throw new NullPointerException("incomeTitle is null"); //NOI18N
155 } else if (incomeTitle.isEmpty()) {
157 throw new IllegalArgumentException("incomeTitle is empty"); //NOI18N
158 } else if (null == incomeUser) {
160 throw new NullPointerException("incomeUser is null"); //NOI18N
161 } else if (incomeUser.getUserId() == null) {
163 throw new NullPointerException("incomeUser.userId is null"); //NOI18N
164 } else if (incomeUser.getUserId() < 1) {
166 throw new IllegalArgumentException(MessageFormat.format("incomeUser.userId={0} is not valid.", incomeUser.getUserId())); //NOI18N
170 this.incomeTitle = incomeTitle;
171 this.incomeSingleAmount = incomeSingleAmount;
172 this.incomeInterval = incomeInterval;
173 this.incomeUser = incomeUser;
174 this.incomeEnabled = incomeEnabled;
178 public int compareTo (final BillableIncome billableIncome) {
179 // Check parameter on null-reference and equality to this
180 if (null == billableIncome) {
182 throw new NullPointerException("billableIncome is null"); //NOI18N
183 } else if (billableIncome.equals(this)) {
189 final int comparators[] = {
190 // First compare title
191 this.getIncomeTitle().compareToIgnoreCase(billableIncome.getIncomeTitle()),
192 // ... then compare interval
193 this.getIncomeInterval().compareTo(billableIncome.getIncomeInterval()),
195 this.getIncomeSingleAmount().compareTo(billableIncome.getIncomeSingleAmount()),
197 this.getIncomeUser().compareTo(billableIncome.getIncomeUser())
201 final int comparison = ComparableUtils.checkAll(comparators);
208 public boolean equals (final Object object) {
209 if (this == object) {
211 } else if (null == object) {
213 } else if (this.getClass() != object.getClass()) {
217 final BillableIncome income = (BillableIncome) object;
219 if (!Objects.equals(this.getIncomeEnabled(), income.getIncomeEnabled())) {
221 } else if (!Objects.equals(this.getIncomeId(), income.getIncomeId())) {
223 } else if (!Objects.equals(this.getIncomeInterval(), income.getIncomeInterval())) {
225 } else if (!Objects.equals(this.getIncomeSingleAmount(), income.getIncomeSingleAmount())) {
227 } else if (!Objects.equals(this.getIncomeTitle(), income.getIncomeTitle())) {
229 } else if (!Objects.equals(this.getIncomeUser(), income.getIncomeUser())) {
237 public Boolean getIncomeEnabled () {
238 return this.incomeEnabled;
242 public void setIncomeEnabled (Boolean incomeEnabled) {
243 this.incomeEnabled = incomeEnabled;
247 @SuppressWarnings ("ReturnOfDateField")
248 public Date getIncomeEntryCreated () {
249 return this.incomeEntryCreated;
253 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
254 public void setIncomeEntryCreated (Date incomeEntryCreated) {
255 this.incomeEntryCreated = incomeEntryCreated;
259 @SuppressWarnings ("ReturnOfDateField")
260 public Date getIncomeEntryUpdated () {
261 return this.incomeEntryUpdated;
265 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
266 public void setIncomeEntryUpdated (Date incomeEntryUpdated) {
267 this.incomeEntryUpdated = incomeEntryUpdated;
271 public Long getIncomeId () {
272 return this.incomeId;
276 public void setIncomeId (final Long incomeId) {
277 this.incomeId = incomeId;
281 public FinancialInterval getIncomeInterval () {
282 return this.incomeInterval;
286 public void setIncomeInterval (final FinancialInterval incomeInterval) {
287 this.incomeInterval = incomeInterval;
291 public BigDecimal getIncomeSingleAmount () {
292 return this.incomeSingleAmount;
296 public void setIncomeSingleAmount (final BigDecimal incomeSingleAmount) {
297 this.incomeSingleAmount = incomeSingleAmount;
301 public String getIncomeTitle () {
302 return this.incomeTitle;
306 public void setIncomeTitle (String incomeTitle) {
307 this.incomeTitle = incomeTitle;
311 public User getIncomeUser () {
312 return this.incomeUser;
316 public void setIncomeUser (User incomeUser) {
317 this.incomeUser = incomeUser;
321 public int hashCode () {
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());