]> git.mxchange.org Git - jshop-core.git/blob - src/org/mxchange/jshopcore/model/category/ProductCategory.java
Some cascade/fetch cleanups:
[jshop-core.git] / src / org / mxchange / jshopcore / model / category / ProductCategory.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.category;
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
31 /**
32  * A product category
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 @Entity (name = "category")
37 @Table (name = "category")
38 public class ProductCategory implements Category, Comparable<Category> {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 21_458_945_712_659L;
44
45         /**
46          * Id number of category
47          */
48         @Id
49         @GeneratedValue (strategy = GenerationType.IDENTITY)
50         @Column (name = "category_id", length = 20, nullable = false)
51         private Long categoryId;
52
53         /**
54          * Title of category
55          */
56         @Basic (optional = false)
57         @Column (name = "category_title", length = 100, nullable = false, unique = true)
58         private String categoryTitle;
59
60         /**
61          * Parent category
62          */
63         @JoinColumn (name = "parent_id")
64         @OneToOne (targetEntity = ProductCategory.class, cascade = CascadeType.REFRESH)
65         private Category parentCategory;
66
67         /**
68          * Constructor which accepts all database fields
69          * <p>
70          * @param categoryId Id number of database record
71          * @param categoryTitle Category categoryTitle
72          * @param parentCategory Parent category
73          */
74         public ProductCategory (final Long categoryId, final String categoryTitle, final Category parentCategory) {
75                 // Set all here
76                 this.categoryId = categoryId;
77                 this.categoryTitle = categoryTitle;
78                 this.parentCategory = parentCategory;
79         }
80
81         /**
82          * Default constructor
83          */
84         public ProductCategory () {
85         }
86
87         @Override
88         public int compareTo (final Category category) {
89                 // category should not be null
90                 if (null == category) {
91                         throw new NullPointerException("category is null"); //NOI18N
92                 }
93
94                 // Is the categoryId the same?
95                 if (Objects.equals(this.getCategoryId(), category.getCategoryId())) {
96                         // Same categoryId, means same category
97                         return 0;
98                 } else if (this.getCategoryId() > category.getCategoryId()) {
99                         // This categoryId is larger than compared to
100                         return -1;
101                 }
102
103                 // The other categoryId is larger
104                 return 1;
105         }
106
107         @Override
108         public void copyAll (final Category category) {
109                 // Copy all data
110                 this.setParentCategory(category.getParentCategory());
111                 this.setCategoryTitle(category.getCategoryTitle());
112         }
113
114         @Override
115         public Long getCategoryId () {
116                 return this.categoryId;
117         }
118
119         @Override
120         public void setCategoryId (final Long categoryId) {
121                 this.categoryId = categoryId;
122         }
123
124         @Override
125         public String getCategoryTitle () {
126                 return this.categoryTitle;
127         }
128
129         @Override
130         public void setCategoryTitle (final String categoryTitle) {
131                 this.categoryTitle = categoryTitle;
132         }
133
134         @Override
135         public Category getParentCategory () {
136                 return this.parentCategory;
137         }
138
139         @Override
140         public void setParentCategory (final Category parentCategory) {
141                 this.parentCategory = parentCategory;
142         }
143 }