]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jproduct/model/product/GenericProduct.java
Continued:
[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          * Default constructor
120          */
121         public GenericProduct () {
122         }
123
124         /**
125          * Constructor will all required data
126          * <p>
127          * @param productTitle        Name of product
128          * @param productGrossPrice   Product's gross price
129          * @param productCategory     Category instance
130          * @param productAvailability Availability (selectable by customer)
131          */
132         public GenericProduct (final String productTitle, final Float productGrossPrice, final Category productCategory, final Boolean productAvailability) {
133                 // Set all here
134                 this.productTitle = productTitle;
135                 this.productGrossPrice = productGrossPrice;
136                 this.productCategory = productCategory;
137                 this.productAvailability = productAvailability;
138         }
139
140         @Override
141         public boolean equals (final Object object) {
142                 if (this == object) {
143                         return true;
144                 } else if (null == object) {
145                         return false;
146                 } else if (this.getClass() != object.getClass()) {
147                         return false;
148                 }
149
150                 final Product product = (Product) object;
151
152                 if (!Objects.equals(this.getProductId(), product.getProductId())) {
153                         return false;
154                 } else if (!Objects.equals(this.getProductTitle(), product.getProductTitle())) {
155                         return false;
156                 }
157
158                 return true;
159         }
160
161         @Override
162         public Boolean getProductAvailability () {
163                 return this.productAvailability;
164         }
165
166         @Override
167         public void setProductAvailability (final Boolean productAvailability) {
168                 this.productAvailability = productAvailability;
169         }
170
171         @Override
172         public Category getProductCategory () {
173                 return this.productCategory;
174         }
175
176         @Override
177         public void setProductCategory (final Category productCategory) {
178                 this.productCategory = productCategory;
179         }
180
181         @Override
182         @SuppressWarnings ("ReturnOfDateField")
183         public Date getProductCreated () {
184                 return this.productCreated;
185         }
186
187         @Override
188         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
189         public void setProductCreated (final Date productCreated) {
190                 this.productCreated = productCreated;
191         }
192
193         @Override
194         public Float getProductGrossPrice () {
195                 return this.productGrossPrice;
196         }
197
198         @Override
199         public void setProductGrossPrice (final Float productGrossPrice) {
200                 this.productGrossPrice = productGrossPrice;
201         }
202
203         @Override
204         public Long getProductId () {
205                 return this.productId;
206         }
207
208         @Override
209         public void setProductId (final Long productId) {
210                 this.productId = productId;
211         }
212
213         @Override
214         public Float getProductNetPrice () {
215                 return this.productNetPrice;
216         }
217
218         @Override
219         public void setProductNetPrice (final Float productNetPrice) {
220                 this.productNetPrice = productNetPrice;
221         }
222
223         @Override
224         public Float getProductTaxRate () {
225                 return this.productTaxRate;
226         }
227
228         @Override
229         public void setProductTaxRate (final Float productTaxRate) {
230                 this.productTaxRate = productTaxRate;
231         }
232
233         @Override
234         public String getProductTitle () {
235                 return this.productTitle;
236         }
237
238         @Override
239         public void setProductTitle (final String productTitle) {
240                 this.productTitle = productTitle;
241         }
242
243         @Override
244         public int hashCode () {
245                 int hash = 7;
246
247                 hash = 23 * hash + Objects.hashCode(this.getProductId());
248                 hash = 23 * hash + Objects.hashCode(this.getProductTitle());
249
250                 return hash;
251         }
252
253 }