]> git.mxchange.org Git - jshop-core.git/blob - src/org/mxchange/jshopcore/model/product/GenericProduct.java
Updated copyright year
[jshop-core.git] / src / org / mxchange / jshopcore / model / product / GenericProduct.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jshopcore.model.product;
18
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;
32
33 /**
34  * Generic product class
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  * TODO: Find a better name
38  */
39 @Entity (name = "products")
40 @Table (name = "products")
41 public class GenericProduct implements Product, Comparable<Product> {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 54_578_571_769_283L;
47
48         /**
49          * Availability of product
50          */
51         @Column (name = "product_availability", nullable = false)
52         private Boolean productAvailability;
53
54         /**
55          * Product productCategory
56          */
57         @JoinColumn (name = "category_id", nullable = false, updatable = false)
58         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.MERGE, optional = false)
59         private Category productCategory;
60
61         /**
62          * Id number of product
63          */
64         @Id
65         @GeneratedValue (strategy = GenerationType.IDENTITY)
66         @Column (name = "product_id", length = 20, nullable = false, updatable = false)
67         private Long productId;
68
69         /**
70          * Price of product
71          */
72         @Basic (optional = false)
73         @Column (name = "product_price", nullable = false)
74         private Float productPrice;
75
76         /**
77          * Title of product
78          */
79         @Basic (optional = false)
80         @Column (name = "product_title", length = 100, nullable = false)
81         private String productTitle;
82
83         /**
84          * Default constructor
85          */
86         public GenericProduct () {
87         }
88
89         /**
90          * Constructor will all required data
91          * <p>
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)
97          */
98         public GenericProduct (final Long productId, final String productTitle, final Float productPrice, final Category productCategory, final Boolean productAvailability) {
99                 // Set all here
100                 this.productId = productId;
101                 this.productTitle = productTitle;
102                 this.productPrice = productPrice;
103                 this.productCategory = productCategory;
104                 this.productAvailability = productAvailability;
105         }
106
107         @Override
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
112                 }
113
114                 // Is the productId the same?
115                 if (Objects.equals(this.getProductId(), product.getProductId())) {
116                         // Same productId, means same productCategory
117                         return 0;
118                 } else if (this.getProductId() > product.getProductId()) {
119                         // This productId is larger than compared to
120                         return 1;
121                 }
122
123                 // The other productId is larger
124                 return -1;
125         }
126
127         @Override
128         public void copyAll (final Product product) {
129                 // Copy all
130                 this.setProductAvailability(product.getProductAvailability());
131                 this.setProductCategory(product.getProductCategory());
132                 this.setProductPrice(product.getProductPrice());
133                 this.setProductTitle(product.getProductTitle());
134         }
135
136         @Override
137         public Boolean getProductAvailability () {
138                 return this.productAvailability;
139         }
140
141         @Override
142         public void setProductAvailability (final Boolean productAvailability) {
143                 this.productAvailability = productAvailability;
144         }
145
146         @Override
147         public Category getProductCategory () {
148                 return this.productCategory;
149         }
150
151         @Override
152         public void setProductCategory (final Category productCategory) {
153                 this.productCategory = productCategory;
154         }
155
156         @Override
157         public Long getProductId () {
158                 return this.productId;
159         }
160
161         @Override
162         public void setProductId (final Long productId) {
163                 this.productId = productId;
164         }
165
166         @Override
167         public Float getProductPrice () {
168                 return this.productPrice;
169         }
170
171         @Override
172         public void setProductPrice (final Float productPrice) {
173                 this.productPrice = productPrice;
174         }
175
176         @Override
177         public String getProductTitle () {
178                 return this.productTitle;
179         }
180
181         @Override
182         public void setProductTitle (final String productTitle) {
183                 this.productTitle = productTitle;
184         }
185 }