]> 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.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
38 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
39 import org.mxchange.jproduct.model.category.Category;
40 import org.mxchange.jproduct.model.category.ProductCategory;
41
42 /**
43  * Generic product class
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  * TODO: Find a better name
47  */
48 @Entity (name = "generic_products")
49 @Table (name = "generic_products")
50 @NamedQueries (
51                 {
52                         @NamedQuery (name = "AllProducts", query = "SELECT p FROM generic_products AS p ORDER BY p.productId ASC"),
53                         @NamedQuery (name = "AllAvailableProducts", query = "SELECT p FROM generic_products AS p WHERE p.productAvailability=TRUE ORDER BY p.productId ASC")
54                 }
55 )
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class GenericProduct implements Product {
58
59         /**
60          * Serial number
61          */
62         @Transient
63         private static final long serialVersionUID = 54_578_571_769_283L;
64
65         /**
66          * Availability of product
67          */
68         @Basic (optional = false)
69         @Column (name = "product_availability", nullable = false)
70         private Boolean productAvailability;
71
72         /**
73          * Product productCategory
74          */
75         @JoinColumn (name = "product_category_id", referencedColumnName = "category_id", nullable = false, updatable = false)
76         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH, optional = false)
77         private Category productCategory;
78
79         /**
80          * When this product has been created
81          */
82         @Basic (optional = false)
83         @Column (name = "product_created", nullable = false, updatable = false)
84         @Temporal (TemporalType.TIMESTAMP)
85         private Date productCreated;
86
87         /**
88          * Currency code for both prices, like EUR or USD
89          */
90         @Basic (optional = false)
91         @Column (name = "product_currency_code", nullable = false, length = 3)
92         private String productCurrencyCode;
93
94         /**
95          * Gross price of product
96          */
97         @Basic (optional = false)
98         @Column (name = "product_gross_price", nullable = false)
99         private Float productGrossPrice;
100
101         /**
102          * I18n key of product
103          */
104         @Basic (optional = false)
105         @Column (name = "product_i18n_key", length = 100, nullable = false, unique = true)
106         private String productI18nKey;
107
108         /**
109          * Id number of product
110          */
111         @Id
112         @GeneratedValue (strategy = GenerationType.IDENTITY)
113         @Column (name = "product_id", nullable = false, updatable = false)
114         private Long productId;
115
116         /**
117          * The company that has manufactured/produced this product
118          */
119         @JoinColumn (name = "product_manufacturer_id", referencedColumnName = "company_data_id")
120         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
121         private BasicData productManufacturer;
122
123         /**
124          * Net price of product
125          */
126         @Column (name = "product_net_price")
127         private Float productNetPrice;
128
129         /**
130          * Number of product
131          */
132         @Column (name = "product_number")
133         private Long productNumber;
134
135         /**
136          * Tax rate (0-1, by 1=100%)
137          */
138         @Column (name = "product_tax_rate")
139         private Float productTaxRate;
140
141         /**
142          * Amount of this product (for example 1 for 1 liter)
143          */
144         @Basic (optional = false)
145         @Column (name = "product_unit_amount", nullable = false)
146         private Float productUnitAmount;
147
148         /**
149          * Unit type (for example liter)
150          */
151         @Basic (optional = false)
152         @Column (name = "product_unit_i18n_key", nullable = false)
153         private String productUnitI18nKey;
154
155         /**
156          * Default constructor
157          */
158         public GenericProduct () {
159         }
160
161         /**
162          * Constructor will all required data
163          * <p>
164          * @param productI18nKey      I18n key of product
165          * @param productGrossPrice   Product's gross price
166          * @param productCurrencyCode Currency code for both prices
167          * @param productCategory     Category instance
168          * @param productAvailability Availability (selectable by customer)
169          * @param productUnitAmount   Unit amount
170          * @param productUnitI18nKey  Unit's i18n key
171          * <p>
172          * @throws NullPointerException If a parameter is null
173          * @throws IllegalArgumentException If a parameter is empty (string) or out
174          * of bounds
175          */
176         public GenericProduct (final String productI18nKey, final Float productGrossPrice, final String productCurrencyCode, final Category productCategory, final Boolean productAvailability, final Float productUnitAmount, final String productUnitI18nKey) {
177                 // Call other constructor first
178                 this();
179
180                 // Validate all parameters
181                 if (null == productAvailability) {
182                         // Throw NPE
183                         throw new NullPointerException("productAvailability is null"); //NOI18N
184                 } else if (null == productCategory) {
185                         // Throw it again
186                         throw new NullPointerException("productCategory is null"); //NOI18N
187                 } else if (productCategory.getCategoryId() == null) {
188                         // Throw it again
189                         throw new NullPointerException("productCategory.categoryId is null"); //NOI18N
190                 } else if (productCategory.getCategoryId() < 1) {
191                         // Throw IAE
192                         throw new IllegalArgumentException(MessageFormat.format("productCategory.categoryId={0} is invalid", productCategory.getCategoryId())); //NOI18N
193                 } else if (null == productCurrencyCode) {
194                         // Throw it again
195                         throw new NullPointerException("productCurrencyCode is null"); //NOI18N
196                 } else if (productCurrencyCode.isEmpty()) {
197                         // Throw IAE
198                         throw new IllegalArgumentException("productCurrencyCode is empty"); //NOI18N
199                 } else if (null == productGrossPrice) {
200                         // Throw it again
201                         throw new NullPointerException("productGrossPrice is null"); //NOI18N
202                 } else if (productGrossPrice < 0) {
203                         // Throw IAE
204                         throw new IllegalArgumentException(MessageFormat.format("productGrossPrice={0} is invalid. Do not enter discounts as products.", productGrossPrice)); //NOI18N
205                 } else if (null == productI18nKey) {
206                         // Throw NPE
207                         throw new NullPointerException("productI18nKey is null"); //NOI18N
208                 } else if (productI18nKey.isEmpty()) {
209                         // Throw IAE
210                         throw new IllegalArgumentException("productI18nKey is empty"); //NOI18N
211                 } else if (null == productUnitAmount) {
212                         // Throw it again
213                         throw new NullPointerException("productUnitAmount is null"); //NOI18N
214                 } else if (productUnitAmount < 0) {
215                         // Throw IAE
216                         throw new IllegalArgumentException(MessageFormat.format("productUnitAmount={0} is invalid. Do not enter discounts as products.", productUnitAmount)); //NOI18N
217                 } else if (null == productUnitI18nKey) {
218                         // Throw NPE
219                         throw new NullPointerException("productUnitI18nKey is null"); //NOI18N
220                 } else if (productUnitI18nKey.isEmpty()) {
221                         // Throw IAE
222                         throw new IllegalArgumentException("productUnitI18nKey is empty"); //NOI18N
223                 }
224
225                 // Set all here
226                 this.productI18nKey = productI18nKey;
227                 this.productGrossPrice = productGrossPrice;
228                 this.productCurrencyCode = productCurrencyCode.toUpperCase();
229                 this.productCategory = productCategory;
230                 this.productAvailability = productAvailability;
231                 this.productUnitAmount = productUnitAmount;
232                 this.productUnitI18nKey = productUnitI18nKey;
233         }
234
235         @Override
236         public boolean equals (final Object object) {
237                 if (this == object) {
238                         return true;
239                 } else if (null == object) {
240                         return false;
241                 } else if (this.getClass() != object.getClass()) {
242                         return false;
243                 }
244
245                 final Product product = (Product) object;
246
247                 if (!Objects.equals(this.getProductId(), product.getProductId())) {
248                         return false;
249                 } else if (!Objects.equals(this.getProductI18nKey(), product.getProductI18nKey())) {
250                         return false;
251                 }
252
253                 return true;
254         }
255
256         @Override
257         public Boolean getProductAvailability () {
258                 return this.productAvailability;
259         }
260
261         @Override
262         public void setProductAvailability (final Boolean productAvailability) {
263                 this.productAvailability = productAvailability;
264         }
265
266         @Override
267         public Category getProductCategory () {
268                 return this.productCategory;
269         }
270
271         @Override
272         public void setProductCategory (final Category productCategory) {
273                 this.productCategory = productCategory;
274         }
275
276         @Override
277         @SuppressWarnings ("ReturnOfDateField")
278         public Date getProductCreated () {
279                 return this.productCreated;
280         }
281
282         @Override
283         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
284         public void setProductCreated (final Date productCreated) {
285                 this.productCreated = productCreated;
286         }
287
288         @Override
289         public String getProductCurrencyCode () {
290                 return this.productCurrencyCode;
291         }
292
293         @Override
294         public void setProductCurrencyCode (final String productCurrencyCode) {
295                 this.productCurrencyCode = productCurrencyCode;
296         }
297
298         @Override
299         public Float getProductGrossPrice () {
300                 return this.productGrossPrice;
301         }
302
303         @Override
304         public void setProductGrossPrice (final Float productGrossPrice) {
305                 this.productGrossPrice = productGrossPrice;
306         }
307
308         @Override
309         public String getProductI18nKey () {
310                 return this.productI18nKey;
311         }
312
313         @Override
314         public void setProductI18nKey (final String productI18nKey) {
315                 this.productI18nKey = productI18nKey;
316         }
317
318         @Override
319         public Long getProductId () {
320                 return this.productId;
321         }
322
323         @Override
324         public void setProductId (final Long productId) {
325                 this.productId = productId;
326         }
327
328         @Override
329         public BasicData getProductManufacturer () {
330                 return this.productManufacturer;
331         }
332
333         @Override
334         public void setProductManufacturer (final BasicData productManufacturer) {
335                 this.productManufacturer = productManufacturer;
336         }
337
338         @Override
339         public Float getProductNetPrice () {
340                 return this.productNetPrice;
341         }
342
343         @Override
344         public void setProductNetPrice (final Float productNetPrice) {
345                 this.productNetPrice = productNetPrice;
346         }
347
348         @Override
349         public Long getProductNumber () {
350                 return this.productNumber;
351         }
352
353         @Override
354         public void setProductNumber (final Long productNumber) {
355                 this.productNumber = productNumber;
356         }
357
358         @Override
359         public Float getProductTaxRate () {
360                 return this.productTaxRate;
361         }
362
363         @Override
364         public void setProductTaxRate (final Float productTaxRate) {
365                 this.productTaxRate = productTaxRate;
366         }
367
368         @Override
369         public Float getProductUnitAmount () {
370                 return this.productUnitAmount;
371         }
372
373         @Override
374         public void setProductUnitAmount (final Float productUnitAmount) {
375                 this.productUnitAmount = productUnitAmount;
376         }
377
378         @Override
379         public String getProductUnitI18nKey () {
380                 return this.productUnitI18nKey;
381         }
382
383         @Override
384         public void setProductUnitI18nKey (final String productUnitI18nKey) {
385                 this.productUnitI18nKey = productUnitI18nKey;
386         }
387
388         @Override
389         public int hashCode () {
390                 int hash = 7;
391
392                 hash = 23 * hash + Objects.hashCode(this.getProductId());
393                 hash = 23 * hash + Objects.hashCode(this.getProductI18nKey());
394
395                 return hash;
396         }
397
398 }