]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jproduct/model/product/GenericProduct.java
bac7e4c49bc28fd76ed05c733a5ab938d1f46343
[jproduct-core.git] / src / org / mxchange / jproduct / model / product / GenericProduct.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.jproduct.model.product;
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.JoinColumn;
29 import javax.persistence.NamedQueries;
30 import javax.persistence.NamedQuery;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33 import javax.persistence.Temporal;
34 import javax.persistence.TemporalType;
35 import javax.persistence.Transient;
36 import org.mxchange.jproduct.model.category.Category;
37 import org.mxchange.jproduct.model.category.ProductCategory;
38
39 /**
40  * Generic product class
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  * TODO: Find a better name
44  */
45 @Entity (name = "generic_products")
46 @Table (name = "generic_products")
47 @NamedQueries (
48                 {
49                         @NamedQuery (name = "AllProducts", query = "SELECT p FROM generic_products AS p ORDER BY p.productId ASC"),
50                         @NamedQuery (name = "AllAvailableProducts", query = "SELECT p FROM generic_products AS p WHERE p.productAvailability=TRUE ORDER BY p.productId ASC")
51                 }
52 )
53 @SuppressWarnings ("PersistenceUnitPresent")
54 public class GenericProduct implements Product {
55
56         /**
57          * Serial number
58          */
59         @Transient
60         private static final long serialVersionUID = 54_578_571_769_283L;
61
62         /**
63          * Availability of product
64          */
65         @Basic (optional = false)
66         @Column (name = "product_availability", nullable = false)
67         private Boolean productAvailability;
68
69         /**
70          * Product productCategory
71          */
72         @JoinColumn (name = "product_category_id", referencedColumnName = "category_id", nullable = false, updatable = false)
73         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH, optional = false)
74         private Category productCategory;
75
76         /**
77          * When this product has been created
78          */
79         @Basic (optional = false)
80         @Column (name = "product_created", nullable = false, updatable = false)
81         @Temporal (TemporalType.TIMESTAMP)
82         private Date productCreated;
83
84         /**
85          * Gross price of product
86          */
87         @Basic (optional = false)
88         @Column (name = "product_gross_price", nullable = false)
89         private Float productGrossPrice;
90
91         /**
92          * Id number of product
93          */
94         @Id
95         @GeneratedValue (strategy = GenerationType.IDENTITY)
96         @Column (name = "product_id", nullable = false, updatable = false)
97         private Long productId;
98
99         /**
100          * Net price of product
101          */
102         @Column (name = "product_net_price")
103         private Float productNetPrice;
104
105         /**
106          * Tax rate (0-1, by 1=100%)
107          */
108         @Column (name = "product_gross_price")
109         private Float productTaxRate;
110
111         /**
112          * Title of product
113          */
114         @Basic (optional = false)
115         @Column (name = "product_title", length = 100, nullable = false, unique = true)
116         private String productTitle;
117
118         /**
119          * Currency code for both prices
120          */
121         private String productCurrencyCode;
122
123         /**
124          * Default constructor
125          */
126         public GenericProduct () {
127         }
128
129         /**
130          * Constructor will all required data
131          * <p>
132          * @param productTitle        Name of product
133          * @param productGrossPrice   Product's gross price
134          * @param productCurrencyCode code for both prices
135          * @param productCategory     Category instance
136          * @param productAvailability Availability (selectable by customer)
137          */
138         public GenericProduct (final String productTitle, final Float productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability) {
139                 // Set all here
140                 this.productTitle = productTitle;
141                 this.productGrossPrice = productGrossPrice;
142                 this.productCurrencyCode = productCurrencyCode;
143                 this.productCategory = productCategory;
144                 this.productAvailability = productAvailability;
145         }
146
147         @Override
148         public boolean equals (final Object object) {
149                 if (this == object) {
150                         return true;
151                 } else if (null == object) {
152                         return false;
153                 } else if (this.getClass() != object.getClass()) {
154                         return false;
155                 }
156
157                 final Product product = (Product) object;
158
159                 if (!Objects.equals(this.getProductId(), product.getProductId())) {
160                         return false;
161                 } else if (!Objects.equals(this.getProductTitle(), product.getProductTitle())) {
162                         return false;
163                 }
164
165                 return true;
166         }
167
168         @Override
169         public Boolean getProductAvailability () {
170                 return this.productAvailability;
171         }
172
173         @Override
174         public void setProductAvailability (final Boolean productAvailability) {
175                 this.productAvailability = productAvailability;
176         }
177
178         @Override
179         public Category getProductCategory () {
180                 return this.productCategory;
181         }
182
183         @Override
184         public void setProductCategory (final Category productCategory) {
185                 this.productCategory = productCategory;
186         }
187
188         @Override
189         @SuppressWarnings ("ReturnOfDateField")
190         public Date getProductCreated () {
191                 return this.productCreated;
192         }
193
194         @Override
195         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
196         public void setProductCreated (final Date productCreated) {
197                 this.productCreated = productCreated;
198         }
199
200         @Override
201         public String getProductCurrencyCode () {
202                 return this.productCurrencyCode;
203         }
204
205         @Override
206         public void setProductCurrencyCode (final String productCurrencyCode) {
207                 this.productCurrencyCode = productCurrencyCode;
208         }
209
210         @Override
211         public Float getProductGrossPrice () {
212                 return this.productGrossPrice;
213         }
214
215         @Override
216         public void setProductGrossPrice (final Float productGrossPrice) {
217                 this.productGrossPrice = productGrossPrice;
218         }
219
220         @Override
221         public Long getProductId () {
222                 return this.productId;
223         }
224
225         @Override
226         public void setProductId (final Long productId) {
227                 this.productId = productId;
228         }
229
230         @Override
231         public Float getProductNetPrice () {
232                 return this.productNetPrice;
233         }
234
235         @Override
236         public void setProductNetPrice (final Float productNetPrice) {
237                 this.productNetPrice = productNetPrice;
238         }
239
240         @Override
241         public Float getProductTaxRate () {
242                 return this.productTaxRate;
243         }
244
245         @Override
246         public void setProductTaxRate (final Float productTaxRate) {
247                 this.productTaxRate = productTaxRate;
248         }
249
250         @Override
251         public String getProductTitle () {
252                 return this.productTitle;
253         }
254
255         @Override
256         public void setProductTitle (final String productTitle) {
257                 this.productTitle = productTitle;
258         }
259
260         @Override
261         public int hashCode () {
262                 int hash = 7;
263
264                 hash = 23 * hash + Objects.hashCode(this.getProductId());
265                 hash = 23 * hash + Objects.hashCode(this.getProductTitle());
266
267                 return hash;
268         }
269
270 }