]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jproduct/model/category/ProductCategory.java
Updated copyright year
[jproduct-core.git] / src / org / mxchange / jproduct / model / category / ProductCategory.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.category;
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.apache.commons.lang3.StringUtils;
37 import org.mxchange.jcoreutils.comparable.ComparableUtils;
38 import org.mxchange.jproduct.model.utils.CategoryUtils;
39
40 /**
41  * A product category
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Entity (name = "product_category")
46 @Table (name = "product_category")
47 @NamedQueries (
48                 {
49                         @NamedQuery (name = "AllProductCategories", query = "SELECT pc FROM product_category AS pc ORDER BY pc.categoryId ASC")
50                 }
51 )
52 @SuppressWarnings ("PersistenceUnitPresent")
53 public class ProductCategory implements Category {
54
55         /**
56          * Serial number
57          */
58         @Transient
59         private static final long serialVersionUID = 21_458_945_712_659L;
60
61         /**
62          * When this entry has been created
63          */
64         @Basic (optional = false)
65         @Column (name = "category_entry_created", updatable = false, nullable = false)
66         @Temporal (TemporalType.TIMESTAMP)
67         private Date categoryEntryCreated;
68
69         /**
70          * When this entry has been created
71          */
72         @Column (name = "category_entry_updated", insertable = false)
73         @Temporal (TemporalType.TIMESTAMP)
74         private Date categoryEntryUpdated;
75
76         /**
77          * I18n key of category
78          */
79         @Basic (optional = false)
80         @Column (name = "category_i18n_key", nullable = false, unique = true)
81         private String categoryI18nKey;
82
83         /**
84          * Id number of category
85          */
86         @Id
87         @GeneratedValue (strategy = GenerationType.IDENTITY)
88         @Column (name = "category_id", nullable = false)
89         private Long categoryId;
90
91         /**
92          * Whether this category is shown in any statistics
93          */
94         @Basic (optional = false)
95         @Column (name = "category_in_statistics", nullable = false)
96         private Boolean categoryShownInStatistics;
97
98         /**
99          * Parent category
100          */
101         @JoinColumn (name = "category_parent_id", referencedColumnName = "category_id")
102         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH)
103         private Category parentCategory;
104
105         /**
106          * Default constructor
107          */
108         public ProductCategory () {
109         }
110
111         /**
112          * Constructor with all required fields
113          * <p>
114          * @param categoryI18nKey           Category i18n key
115          * @param categoryShownInStatistics Whether this category is shown in any
116          *                                  statistics
117          */
118         public ProductCategory (final String categoryI18nKey, final Boolean categoryShownInStatistics) {
119                 // Call other constructor
120                 this();
121
122                 // Validate parameter
123                 if (null == categoryI18nKey) {
124                         // Throw NPE
125                         throw new NullPointerException("categoryI18nKey is null"); //NOI18N
126                 } else if (categoryI18nKey.isEmpty()) {
127                         // Throw IAE
128                         throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
129                 } else if (null == categoryShownInStatistics) {
130                         // Throw NPE
131                         throw new NullPointerException("categoryShownInStatistics is null"); //NOI18N
132                 }
133
134                 // Set all here
135                 this.categoryI18nKey = categoryI18nKey;
136                 this.categoryShownInStatistics = categoryShownInStatistics;
137         }
138
139         @Override
140         public int compareTo (final Category category) {
141                 // Check parameter on null-reference and equality to this
142                 if (null == category) {
143                         // Should not happen
144                         throw new NullPointerException("category is null"); //NOI18N
145                 } else if (category.equals(this)) {
146                         // Same object
147                         return 0;
148                 }
149
150                 // Init comparators
151                 final int comparators[] = {
152                         // First check parent category
153                         CategoryUtils.compare(this.getParentCategory(), category.getParentCategory()),
154                         // ... i18n key as it is unique
155                         StringUtils.compare(this.getCategoryI18nKey(), category.getCategoryI18nKey()),
156                         // ... primary key
157                         Long.compare(this.getCategoryId(), category.getCategoryId())
158                 };
159
160                 // Check all values
161                 final int comparison = ComparableUtils.checkAll(comparators);
162
163                 // Return value
164                 return comparison;
165         }
166
167         @Override
168         public boolean equals (final Object object) {
169                 if (this == object) {
170                         return true;
171                 } else if (null == object) {
172                         return false;
173                 } else if (this.getClass() != object.getClass()) {
174                         return false;
175                 }
176
177                 final Category category = (Category) object;
178
179                 if (!Objects.equals(this.getCategoryI18nKey(), category.getCategoryI18nKey())) {
180                         return false;
181                 } else if (!Objects.equals(this.getCategoryShownInStatistics(), category.getCategoryShownInStatistics())) {
182                         return false;
183                 } else if (!Objects.equals(this.getParentCategory(), category.getParentCategory())) {
184                         return false;
185                 } else if (!Objects.equals(this.getCategoryId(), category.getCategoryId())) {
186                         return false;
187                 }
188
189                 return true;
190         }
191
192         @Override
193         @SuppressWarnings ("ReturnOfDateField")
194         public Date getCategoryEntryCreated () {
195                 return this.categoryEntryCreated;
196         }
197
198         @Override
199         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
200         public void setCategoryEntryCreated (final Date categoryEntryCreated) {
201                 this.categoryEntryCreated = categoryEntryCreated;
202         }
203
204         @Override
205         @SuppressWarnings ("ReturnOfDateField")
206         public Date getCategoryEntryUpdated () {
207                 return this.categoryEntryUpdated;
208         }
209
210         @Override
211         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
212         public void setCategoryEntryUpdated (final Date categoryEntryUpdated) {
213                 this.categoryEntryUpdated = categoryEntryUpdated;
214         }
215
216         @Override
217         public String getCategoryI18nKey () {
218                 return this.categoryI18nKey;
219         }
220
221         @Override
222         public void setCategoryI18nKey (final String categoryI18nKey) {
223                 this.categoryI18nKey = categoryI18nKey;
224         }
225
226         @Override
227         public Long getCategoryId () {
228                 return this.categoryId;
229         }
230
231         @Override
232         public void setCategoryId (final Long categoryId) {
233                 this.categoryId = categoryId;
234         }
235
236         @Override
237         public Boolean getCategoryShownInStatistics () {
238                 return this.categoryShownInStatistics;
239         }
240
241         @Override
242         public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) {
243                 this.categoryShownInStatistics = categoryShownInStatistics;
244         }
245
246         @Override
247         public Category getParentCategory () {
248                 return this.parentCategory;
249         }
250
251         @Override
252         public void setParentCategory (final Category parentCategory) {
253                 this.parentCategory = parentCategory;
254         }
255
256         @Override
257         public int hashCode () {
258                 int hash = 7;
259
260                 hash = 13 * hash + Objects.hashCode(this.getCategoryI18nKey());
261                 hash = 13 * hash + Objects.hashCode(this.getCategoryShownInStatistics());
262                 hash = 13 * hash + Objects.hashCode(this.getParentCategory());
263                 hash = 13 * hash + Objects.hashCode(this.getCategoryId());
264
265                 return hash;
266         }
267
268 }