]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/item/FinancialReceiptItem.java
Continued:
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / item / FinancialReceiptItem.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.receipt.item;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.Index;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32 import javax.persistence.Temporal;
33 import javax.persistence.TemporalType;
34 import javax.persistence.Transient;
35 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
36 import org.mxchange.jfinancials.model.receipt.FinancialReceipt;
37 import org.mxchange.jproduct.model.product.GenericProduct;
38 import org.mxchange.jproduct.model.product.Product;
39
40 /**
41  * A POJO for receipt items
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Entity (name = "receipt_items")
46 @Table (
47                 name = "receipt_items",
48                 indexes = {
49                         @Index (name = "item_receipt_product", columnList = "item_receipt_id,item_product_id", unique = true)
50                 }
51 )
52 @SuppressWarnings ("PersistenceUnitPresent")
53 public class FinancialReceiptItem implements BillableReceiptItem {
54
55         /**
56          * Serial number
57          */
58         @Transient
59         private static final long serialVersionUID = 126_498_698_378_571L;
60
61         /**
62          * When this item has been created in database
63          */
64         @Basic (optional = false)
65         @Temporal (TemporalType.TIMESTAMP)
66         @Column (name = "item_created", nullable = false)
67         private Date itemCreated;
68
69         /**
70          * Primary key
71          */
72         @Id
73         @GeneratedValue (strategy = GenerationType.IDENTITY)
74         @Column (name = "item_id", nullable = false, updatable = false)
75         private Long itemId;
76
77         /**
78          * Product being linked in this itemReceipt item
79          */
80         @JoinColumn (name = "item_product_id", referencedColumnName = "product_id", nullable = false, updatable = false)
81         @OneToOne (targetEntity = GenericProduct.class, cascade = CascadeType.REFRESH, optional = false)
82         private Product itemProduct;
83
84         /**
85          * Discount on item
86          */
87         @Column (name = "item_product_discount")
88         private Float itemProductDiscount;
89
90         /**
91          * Product quantity
92          */
93         @Basic (optional = false)
94         @Column (name = "item_product_quantity", nullable = false)
95         private Long itemProductQuantity;
96
97         /**
98          * Connected itemReceipt item
99          */
100         @JoinColumn (name = "item_receipt_id", referencedColumnName = "receipt_id", nullable = false, updatable = false)
101         @OneToOne (targetEntity = FinancialReceipt.class, cascade = CascadeType.REFRESH, optional = false)
102         private BillableReceipt itemReceipt;
103
104         /**
105          * Default constructor
106          */
107         public FinancialReceiptItem () {
108         }
109
110         /**
111          * Constructor with product, price, quantity and receipt instance
112          * <p>
113          * @param itemProduct         Product instance
114          * @param itemProductQuantity Product quantity
115          * @param itemReceipt         FinancialReceipt instance
116          */
117         public FinancialReceiptItem (final Product itemProduct, final Long itemProductQuantity, final BillableReceipt itemReceipt) {
118                 // Call other constructor
119                 this();
120
121                 // Set all values
122                 this.itemProduct = itemProduct;
123                 this.itemProductQuantity = itemProductQuantity;
124                 this.itemReceipt = itemReceipt;
125         }
126
127         @Override
128         public boolean equals (final Object object) {
129                 if (this == object) {
130                         return true;
131                 } else if (null == object) {
132                         return false;
133                 } else if (this.getClass() != object.getClass()) {
134                         return false;
135                 }
136
137                 final BillableReceiptItem receiptItem = (BillableReceiptItem) object;
138
139                 if (!Objects.equals(this.getItemId(), receiptItem.getItemId())) {
140                         return false;
141                 } else if (!Objects.equals(this.getItemProduct(), receiptItem.getItemProduct())) {
142                         return false;
143                 } else if (!Objects.equals(this.getItemProductQuantity(), receiptItem.getItemProductQuantity())) {
144                         return false;
145                 } else if (!Objects.equals(this.getItemReceipt(), receiptItem.getItemReceipt())) {
146                         return false;
147                 }
148
149                 return true;
150         }
151
152         @Override
153         @SuppressWarnings ("ReturnOfDateField")
154         public Date getItemCreated () {
155                 return this.itemCreated;
156         }
157
158         @Override
159         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
160         public void setItemCreated (final Date itemCreated) {
161                 this.itemCreated = itemCreated;
162         }
163
164         @Override
165         public Long getItemId () {
166                 return this.itemId;
167         }
168
169         @Override
170         public void setItemId (final Long itemId) {
171                 this.itemId = itemId;
172         }
173
174         @Override
175         public Product getItemProduct () {
176                 return this.itemProduct;
177         }
178
179         @Override
180         public void setItemProduct (final Product itemProduct) {
181                 this.itemProduct = itemProduct;
182         }
183
184         @Override
185         public Float getItemProductDiscount () {
186                 return this.itemProductDiscount;
187         }
188
189         @Override
190         public void setItemProductDiscount (final Float itemProductDiscount) {
191                 this.itemProductDiscount = itemProductDiscount;
192         }
193
194         @Override
195         public Long getItemProductQuantity () {
196                 return this.itemProductQuantity;
197         }
198
199         @Override
200         public void setItemProductQuantity (final Long itemProductQuantity) {
201                 this.itemProductQuantity = itemProductQuantity;
202         }
203
204         @Override
205         public BillableReceipt getItemReceipt () {
206                 return this.itemReceipt;
207         }
208
209         @Override
210         public void setItemReceipt (final BillableReceipt itemReceipt) {
211                 this.itemReceipt = itemReceipt;
212         }
213
214         @Override
215         public int hashCode () {
216                 int hash = 5;
217
218                 hash = 53 * hash + Objects.hashCode(this.getItemId());
219                 hash = 53 * hash + Objects.hashCode(this.getItemProduct());
220                 hash = 53 * hash + Objects.hashCode(this.getItemProductQuantity());
221                 hash = 53 * hash + Objects.hashCode(this.getItemReceipt());
222
223                 return hash;
224         }
225
226 }