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