2 * Copyright (C) 2016 Roland Haeder
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.jshopcore.model.product;
19 import java.util.Objects;
20 import javax.persistence.Basic;
21 import javax.persistence.CascadeType;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.GeneratedValue;
25 import javax.persistence.GenerationType;
26 import javax.persistence.Id;
27 import javax.persistence.JoinColumn;
28 import javax.persistence.OneToOne;
29 import javax.persistence.Table;
30 import org.mxchange.jshopcore.model.category.Category;
31 import org.mxchange.jshopcore.model.category.ProductCategory;
34 * Generic product class
36 * @author Roland Haeder<roland@mxchange.org>
37 * TODO: Find a better name
39 @Entity (name = "products")
40 @Table (name = "products")
41 public class GenericProduct implements Product, Comparable<Product> {
46 private static final long serialVersionUID = 54_578_571_769_283L;
49 * Availability of product
51 @Column (name = "product_availability", nullable = false)
52 private Boolean productAvailability;
55 * Product productCategory
57 @JoinColumn (name = "category_id", nullable = false, updatable = false)
58 @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.MERGE, optional = false)
59 private Category productCategory;
62 * Id number of product
65 @GeneratedValue (strategy = GenerationType.IDENTITY)
66 @Column (name = "product_id", length = 20, nullable = false, updatable = false)
67 private Long productId;
72 @Basic (optional = false)
73 @Column (name = "product_price", nullable = false)
74 private Float productPrice;
79 @Basic (optional = false)
80 @Column (name = "product_title", length = 100, nullable = false)
81 private String productTitle;
86 public GenericProduct () {
90 * Constructor will all required data
92 * @param productId Id number of product
93 * @param productTitle Name of product
94 * @param productPrice Price
95 * @param productCategory Category instance
96 * @param productAvailability Availability (selectable by customer)
98 public GenericProduct (final Long productId, final String productTitle, final Float productPrice, final Category productCategory, final Boolean productAvailability) {
100 this.productId = productId;
101 this.productTitle = productTitle;
102 this.productPrice = productPrice;
103 this.productCategory = productCategory;
104 this.productAvailability = productAvailability;
108 public int compareTo (final Product product) {
109 // productCategory should not be null
110 if (null == product) {
111 throw new NullPointerException("product is null"); //NOI18N
114 // Is the productId the same?
115 if (Objects.equals(this.getProductId(), product.getProductId())) {
116 // Same productId, means same productCategory
118 } else if (this.getProductId() > product.getProductId()) {
119 // This productId is larger than compared to
123 // The other productId is larger
128 public void copyAll (final Product product) {
130 this.setProductAvailability(product.getProductAvailability());
131 this.setProductCategory(product.getProductCategory());
132 this.setProductPrice(product.getProductPrice());
133 this.setProductTitle(product.getProductTitle());
137 public Boolean getProductAvailability () {
138 return this.productAvailability;
142 public void setProductAvailability (final Boolean productAvailability) {
143 this.productAvailability = productAvailability;
147 public Category getProductCategory () {
148 return this.productCategory;
152 public void setProductCategory (final Category productCategory) {
153 this.productCategory = productCategory;
157 public Long getProductId () {
158 return this.productId;
162 public void setProductId (final Long productId) {
163 this.productId = productId;
167 public Float getProductPrice () {
168 return this.productPrice;
172 public void setProductPrice (final Float productPrice) {
173 this.productPrice = productPrice;
177 public String getProductTitle () {
178 return this.productTitle;
182 public void setProductTitle (final String productTitle) {
183 this.productTitle = productTitle;