2 * Copyright (C) 2016 - 2024 Free Software Foundation
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.category;
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;
43 * @author Roland Häder<roland@mxchange.org>
45 @Entity (name = "product_category")
46 @Table (name = "product_category")
49 @NamedQuery (name = "AllProductCategories", query = "SELECT pc FROM product_category AS pc ORDER BY pc.categoryId ASC")
52 @SuppressWarnings ("PersistenceUnitPresent")
53 public class ProductCategory implements Category {
59 private static final long serialVersionUID = 21_458_945_712_659L;
62 * When this entry has been created
64 @Basic (optional = false)
65 @Column (name = "category_entry_created", updatable = false, nullable = false)
66 @Temporal (TemporalType.TIMESTAMP)
67 private Date categoryEntryCreated;
70 * When this entry has been created
72 @Column (name = "category_entry_updated", insertable = false)
73 @Temporal (TemporalType.TIMESTAMP)
74 private Date categoryEntryUpdated;
77 * I18n key of category
79 @Basic (optional = false)
80 @Column (name = "category_i18n_key", nullable = false, unique = true)
81 private String categoryI18nKey;
84 * Id number of category
87 @GeneratedValue (strategy = GenerationType.IDENTITY)
88 @Column (name = "category_id", nullable = false)
89 private Long categoryId;
92 * Whether this category is shown in any statistics
94 @Basic (optional = false)
95 @Column (name = "category_in_statistics", nullable = false)
96 private Boolean categoryShownInStatistics;
101 @JoinColumn (name = "category_parent_id", referencedColumnName = "category_id")
102 @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH)
103 private Category parentCategory;
106 * Default constructor
108 public ProductCategory () {
112 * Constructor with all required fields
114 * @param categoryI18nKey Category i18n key
115 * @param categoryShownInStatistics Whether this category is shown in any
118 public ProductCategory (final String categoryI18nKey, final Boolean categoryShownInStatistics) {
119 // Call other constructor
122 // Validate parameter
123 if (null == categoryI18nKey) {
125 throw new NullPointerException("categoryI18nKey is null"); //NOI18N
126 } else if (categoryI18nKey.isEmpty()) {
128 throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N
129 } else if (null == categoryShownInStatistics) {
131 throw new NullPointerException("categoryShownInStatistics is null"); //NOI18N
135 this.categoryI18nKey = categoryI18nKey;
136 this.categoryShownInStatistics = categoryShownInStatistics;
140 public int compareTo (final Category category) {
141 // Check parameter on null-reference and equality to this
142 if (null == category) {
144 throw new NullPointerException("category is null"); //NOI18N
145 } else if (category.equals(this)) {
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()),
157 Long.compare(this.getCategoryId(), category.getCategoryId())
161 final int comparison = ComparableUtils.checkAll(comparators);
168 public boolean equals (final Object object) {
169 if (this == object) {
171 } else if (null == object) {
173 } else if (this.getClass() != object.getClass()) {
177 final Category category = (Category) object;
179 if (!Objects.equals(this.getCategoryI18nKey(), category.getCategoryI18nKey())) {
181 } else if (!Objects.equals(this.getCategoryShownInStatistics(), category.getCategoryShownInStatistics())) {
183 } else if (!Objects.equals(this.getParentCategory(), category.getParentCategory())) {
185 } else if (!Objects.equals(this.getCategoryId(), category.getCategoryId())) {
193 @SuppressWarnings ("ReturnOfDateField")
194 public Date getCategoryEntryCreated () {
195 return this.categoryEntryCreated;
199 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
200 public void setCategoryEntryCreated (final Date categoryEntryCreated) {
201 this.categoryEntryCreated = categoryEntryCreated;
205 @SuppressWarnings ("ReturnOfDateField")
206 public Date getCategoryEntryUpdated () {
207 return this.categoryEntryUpdated;
211 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
212 public void setCategoryEntryUpdated (final Date categoryEntryUpdated) {
213 this.categoryEntryUpdated = categoryEntryUpdated;
217 public String getCategoryI18nKey () {
218 return this.categoryI18nKey;
222 public void setCategoryI18nKey (final String categoryI18nKey) {
223 this.categoryI18nKey = categoryI18nKey;
227 public Long getCategoryId () {
228 return this.categoryId;
232 public void setCategoryId (final Long categoryId) {
233 this.categoryId = categoryId;
237 public Boolean getCategoryShownInStatistics () {
238 return this.categoryShownInStatistics;
242 public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) {
243 this.categoryShownInStatistics = categoryShownInStatistics;
247 public Category getParentCategory () {
248 return this.parentCategory;
252 public void setParentCategory (final Category parentCategory) {
253 this.parentCategory = parentCategory;
257 public int hashCode () {
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());