2 * Copyright (C) 2016, 2017 Roland Häder
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.jproduct.model.product;
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;
43 * Generic product class
45 * @author Roland Häder<roland@mxchange.org>
46 * TODO: Find a better name
48 @Entity (name = "generic_products")
49 @Table (name = "generic_products")
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")
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class GenericProduct implements Product {
63 private static final long serialVersionUID = 54_578_571_769_283L;
66 * Availability of product
68 @Basic (optional = false)
69 @Column (name = "product_availability", nullable = false)
70 private Boolean productAvailability;
73 * Product productCategory
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;
80 * When this product has been created
82 @Basic (optional = false)
83 @Column (name = "product_created", nullable = false, updatable = false)
84 @Temporal (TemporalType.TIMESTAMP)
85 private Date productCreated;
88 * Currency code for both prices, like EUR or USD
90 @Basic (optional = false)
91 @Column (name = "product_currency_code", nullable = false, length = 3)
92 private String productCurrencyCode;
95 * Gross price of product
97 @Basic (optional = false)
98 @Column (name = "product_gross_price", nullable = false)
99 private Float productGrossPrice;
102 * I18n key of product
104 @Basic (optional = false)
105 @Column (name = "product_i18n_key", length = 100, nullable = false, unique = true)
106 private String productI18nKey;
109 * Id number of product
112 @GeneratedValue (strategy = GenerationType.IDENTITY)
113 @Column (name = "product_id", nullable = false, updatable = false)
114 private Long productId;
117 * The company that has manufactured/produced this product
119 @JoinColumn (name = "product_manufacturer_id", referencedColumnName = "company_data_id")
120 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
121 private BasicData productManufacturer;
124 * Net price of product
126 @Column (name = "product_net_price")
127 private Float productNetPrice;
132 @Column (name = "product_number")
133 private Long productNumber;
136 * Tax rate (0-1, by 1=100%)
138 @Column (name = "product_tax_rate")
139 private Float productTaxRate;
142 * Amount of this product (for example 1 for 1 liter)
144 @Basic (optional = false)
145 @Column (name = "product_unit_amount", nullable = false)
146 private Float productUnitAmount;
149 * Unit type (for example liter)
151 @Basic (optional = false)
152 @Column (name = "product_unit_i18n_key", nullable = false)
153 private String productUnitI18nKey;
156 * Default constructor
158 public GenericProduct () {
162 * Constructor will all required data
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
172 * @throws NullPointerException If a parameter is null
173 * @throws IllegalArgumentException If a parameter is empty (string) or out
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
180 // Validate all parameters
181 if (null == productAvailability) {
183 throw new NullPointerException("productAvailability is null"); //NOI18N
184 } else if (null == productCategory) {
186 throw new NullPointerException("productCategory is null"); //NOI18N
187 } else if (productCategory.getCategoryId() == null) {
189 throw new NullPointerException("productCategory.categoryId is null"); //NOI18N
190 } else if (productCategory.getCategoryId() < 1) {
192 throw new IllegalArgumentException(MessageFormat.format("productCategory.categoryId={0} is invalid", productCategory.getCategoryId())); //NOI18N
193 } else if (null == productCurrencyCode) {
195 throw new NullPointerException("productCurrencyCode is null"); //NOI18N
196 } else if (productCurrencyCode.isEmpty()) {
198 throw new IllegalArgumentException("productCurrencyCode is empty"); //NOI18N
199 } else if (null == productGrossPrice) {
201 throw new NullPointerException("productGrossPrice is null"); //NOI18N
202 } else if (productGrossPrice < 0) {
204 throw new IllegalArgumentException(MessageFormat.format("productGrossPrice={0} is invalid. Do not enter discounts as products.", productGrossPrice)); //NOI18N
205 } else if (null == productI18nKey) {
207 throw new NullPointerException("productI18nKey is null"); //NOI18N
208 } else if (productI18nKey.isEmpty()) {
210 throw new IllegalArgumentException("productI18nKey is empty"); //NOI18N
211 } else if (null == productUnitAmount) {
213 throw new NullPointerException("productUnitAmount is null"); //NOI18N
214 } else if (productUnitAmount < 0) {
216 throw new IllegalArgumentException(MessageFormat.format("productUnitAmount={0} is invalid. Do not enter discounts as products.", productUnitAmount)); //NOI18N
217 } else if (null == productUnitI18nKey) {
219 throw new NullPointerException("productUnitI18nKey is null"); //NOI18N
220 } else if (productUnitI18nKey.isEmpty()) {
222 throw new IllegalArgumentException("productUnitI18nKey is empty"); //NOI18N
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;
236 public boolean equals (final Object object) {
237 if (this == object) {
239 } else if (null == object) {
241 } else if (this.getClass() != object.getClass()) {
245 final Product product = (Product) object;
247 if (!Objects.equals(this.getProductId(), product.getProductId())) {
249 } else if (!Objects.equals(this.getProductI18nKey(), product.getProductI18nKey())) {
257 public Boolean getProductAvailability () {
258 return this.productAvailability;
262 public void setProductAvailability (final Boolean productAvailability) {
263 this.productAvailability = productAvailability;
267 public Category getProductCategory () {
268 return this.productCategory;
272 public void setProductCategory (final Category productCategory) {
273 this.productCategory = productCategory;
277 @SuppressWarnings ("ReturnOfDateField")
278 public Date getProductCreated () {
279 return this.productCreated;
283 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
284 public void setProductCreated (final Date productCreated) {
285 this.productCreated = productCreated;
289 public String getProductCurrencyCode () {
290 return this.productCurrencyCode;
294 public void setProductCurrencyCode (final String productCurrencyCode) {
295 this.productCurrencyCode = productCurrencyCode;
299 public Float getProductGrossPrice () {
300 return this.productGrossPrice;
304 public void setProductGrossPrice (final Float productGrossPrice) {
305 this.productGrossPrice = productGrossPrice;
309 public String getProductI18nKey () {
310 return this.productI18nKey;
314 public void setProductI18nKey (final String productI18nKey) {
315 this.productI18nKey = productI18nKey;
319 public Long getProductId () {
320 return this.productId;
324 public void setProductId (final Long productId) {
325 this.productId = productId;
329 public BasicData getProductManufacturer () {
330 return this.productManufacturer;
334 public void setProductManufacturer (final BasicData productManufacturer) {
335 this.productManufacturer = productManufacturer;
339 public Float getProductNetPrice () {
340 return this.productNetPrice;
344 public void setProductNetPrice (final Float productNetPrice) {
345 this.productNetPrice = productNetPrice;
349 public Long getProductNumber () {
350 return this.productNumber;
354 public void setProductNumber (final Long productNumber) {
355 this.productNumber = productNumber;
359 public Float getProductTaxRate () {
360 return this.productTaxRate;
364 public void setProductTaxRate (final Float productTaxRate) {
365 this.productTaxRate = productTaxRate;
369 public Float getProductUnitAmount () {
370 return this.productUnitAmount;
374 public void setProductUnitAmount (final Float productUnitAmount) {
375 this.productUnitAmount = productUnitAmount;
379 public String getProductUnitI18nKey () {
380 return this.productUnitI18nKey;
384 public void setProductUnitI18nKey (final String productUnitI18nKey) {
385 this.productUnitI18nKey = productUnitI18nKey;
389 public int hashCode () {
392 hash = 23 * hash + Objects.hashCode(this.getProductId());
393 hash = 23 * hash + Objects.hashCode(this.getProductI18nKey());