]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jshopcore/model/category/BaseCategory.java
Continued with JPA:
[jproduct-core.git] / src / org / mxchange / jshopcore / model / category / BaseCategory.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.Id;
25 import javax.persistence.Table;
26
27 /**
28  * A general product category class
29  *
30  * @author Roland Haeder<roland@mxchange.org>
31  */
32 @Entity(name = "category")
33 @Table(name = "category")
34 public abstract class BaseCategory implements Category, Comparable<Category> {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 38_472_937_685_901L;
40
41         /**
42          * Id number of category
43          */
44         @Id
45         @GeneratedValue
46         @Column
47         private Long id;
48
49         /**
50          * Parent category id
51          */
52         @Column(name = "parent_id", length = 20)
53         private Long parentId;
54
55         /**
56          * Title of category
57          */
58         @Basic(optional = false)
59         @Column(length = 100, nullable = false, unique = true)
60         private String title;
61
62         /**
63          * Constructor which accepts all database fields
64          *
65          * @param id Id number of database record
66          * @param title Category title
67          * @param parentId Parent id
68          */
69         protected BaseCategory (final Long id, final String title, final Long parentId) {
70                 // Set all here
71                 this.id = id;
72                 this.title = title;
73                 this.parentId = parentId;
74         }
75
76         /**
77          * Default constructor
78          */
79         protected BaseCategory () {
80         }
81
82         /**
83          * Compares two categories with each other
84          *
85          * @param category Category comparator
86          * @return Comparison value
87          */
88         @Override
89         public int compareTo (final Category category) {
90                 // category should not be null
91                 if (null == category) {
92                         throw new NullPointerException("category is null"); //NOI18N
93                 }
94
95                 // Is the id the same?
96                 if (Objects.equals(this.getId(), category.getId())) {
97                         // Same id, means same category
98                         return 0;
99                 } else if (this.getId() > category.getId()) {
100                         // This id is larger than compared to
101                         return -1;
102                 }
103
104                 // The other id is larger
105                 return 1;
106         }
107
108         @Override
109         public void copyAll (final Category category) {
110                 // Copy all data
111                 this.setParentId(category.getParentId());
112                 this.setTitle(category.getTitle());
113         }
114
115         /**
116          * Id number of category
117          *
118          * @return the id
119          */
120         @Override
121         public Long getId () {
122                 return this.id;
123         }
124
125         /**
126          * Id number of category
127          *
128          * @param id the id to set
129          */
130         @Override
131         public void setId (final Long id) {
132                 this.id = id;
133         }
134
135         /**
136          * Parent category id
137          *
138          * @return the parentId
139          */
140         @Override
141         public Long getParentId () {
142                 return this.parentId;
143         }
144
145         /**
146          * Parent category id
147          *
148          * @param parentId the parentId to set
149          */
150         @Override
151         public void setParentId (final Long parentId) {
152                 this.parentId = parentId;
153         }
154
155         /**
156          * Title of category
157          *
158          * @return the title
159          */
160         @Override
161         public String getTitle () {
162                 return this.title;
163         }
164
165         /**
166          * Title of category
167          *
168          * @param title the title to set
169          */
170         @Override
171         public void setTitle (final String title) {
172                 this.title = title;
173         }
174 }