]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/department/BusinessDepartment.java
Updated jar(s)
[jcontacts-business-core.git] / src / org / mxchange / jcontactsbusiness / model / department / BusinessDepartment.java
1 /*
2  * Copyright (C) 2016 - 2018 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.jcontactsbusiness.model.department;
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.ManyToOne;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
38 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
39 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
40 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
41 import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice;
42 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
43 import org.mxchange.jcontactsbusiness.model.employee.Employable;
44 import org.mxchange.jcontactsbusiness.model.headquarter.BusinessHeadquarter;
45 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
46 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarters;
47 import org.mxchange.jcoreutils.Comparables;
48 import org.mxchange.jusercore.model.user.LoginUser;
49 import org.mxchange.jusercore.model.user.User;
50
51 /**
52  * A POJO for company departments
53  * <p>
54  * @author Roland Häder<roland@mxchange.org>
55  */
56 @Entity (name = "company_departments")
57 @Table (name = "company_departments")
58 @NamedQueries (
59                 {
60                         @NamedQuery (name = "AllDepartments", query = "SELECT d FROM company_departments AS d ORDER BY d.departmentId ASC")
61                 }
62 )
63 @SuppressWarnings ("PersistenceUnitPresent")
64 public class BusinessDepartment implements Department {
65
66         /**
67          * Serial number
68          */
69         @Transient
70         private static final long serialVersionUID = 94_835_918_958_717_660L;
71
72         /**
73          * Where this department is located
74          */
75         @JoinColumn (name = "department_branch_id")
76         @ManyToOne (targetEntity = BusinessBranchOffice.class, cascade = CascadeType.REFRESH)
77         private BranchOffice departmentBranchOffice;
78
79         /**
80          * Connection to business contact
81          */
82         @JoinColumn (name = "department_company_id", nullable = false, updatable = false)
83         @ManyToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH, optional = false)
84         private BasicData departmentCompany;
85
86         /**
87          * Timestamp when this entry has been created
88          */
89         @Basic (optional = false)
90         @Temporal (TemporalType.TIMESTAMP)
91         @Column (name = "department_created", nullable = false, updatable = false)
92         private Date departmentCreated;
93
94         /**
95          * Where this department is located
96          */
97         @JoinColumn (name = "department_headquarter_id")
98         @OneToOne (targetEntity = BusinessHeadquarter.class, cascade = CascadeType.REFRESH)
99         private Headquarter departmentHeadquarter;
100
101         /**
102          * Department i18n key
103          */
104         @Basic (optional = false)
105         @Column (name = "department_i18n_key", length = 100, nullable = false)
106         private String departmentI18nKey;
107
108         /**
109          * Id number
110          */
111         @Id
112         @GeneratedValue (strategy = GenerationType.IDENTITY)
113         @Column (name = "department_id", nullable = false, updatable = false)
114         private Long departmentId;
115
116         /**
117          * Department lead employee
118          */
119         @JoinColumn (name = "department_lead_id")
120         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
121         private Employable departmentLead;
122
123         /**
124          * User owner instance
125          */
126         @JoinColumn (name = "department_user_id")
127         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
128         private User departmentUserOwner;
129
130         /**
131          * Default constructor
132          */
133         public BusinessDepartment () {
134         }
135
136         /**
137          * Constructor with all required fields
138          * <p>
139          * @param departmentCompany Basic data instance
140          * @param departmentName    Department name
141          */
142         public BusinessDepartment (final BasicData departmentCompany, final String departmentName) {
143                 // Call other constructor
144                 this();
145
146                 // Validate parameter
147                 if (null == departmentCompany) {
148                         // Throw NPE
149                         throw new NullPointerException("departmentCompany is null"); //NOI18N
150                 } else if (null == departmentName) {
151                         // Throw it again
152                         throw new NullPointerException("departmentName is null"); //NOI18N
153                 } else if (departmentName.isEmpty()) {
154                         // Throw IAE
155                         throw new IllegalArgumentException("departmentName is empty"); //NOI18N
156                 }
157
158                 // Set all fields
159                 this.departmentCompany = departmentCompany;
160                 this.departmentI18nKey = departmentName;
161         }
162
163         @Override
164         public int compareTo (final Department department) {
165                 // Check parameter on null-reference and equality to this
166                 if (null == department) {
167                         // Should not happen
168                         throw new NullPointerException("department is null"); //NOI18N
169                 } else if (department.equals(this)) {
170                         // Same object
171                         return 0;
172                 }
173
174                 // Init comparisons
175                 final int[] comparators = {
176                         // First department's company (BasicData) ...
177                         this.getDepartmentCompany().compareTo(department.getDepartmentCompany()),
178                         // ... then headquarters
179                         Headquarters.compare(this.getDepartmentHeadquarter(), department.getDepartmentHeadquarter()),
180                         // ... branch office
181                         BranchOffices.compare(this.getDepartmentBranchOffice(), department.getDepartmentBranchOffice()),
182                         // ... finally department's i18n key
183                         this.getDepartmentI18nKey().compareTo(department.getDepartmentI18nKey())
184                 };
185
186                 // Check all values
187                 final int comparison = Comparables.checkAll(comparators);
188
189                 // Return value
190                 return comparison;
191         }
192
193         @Override
194         public boolean equals (final Object object) {
195                 if (null == object) {
196                         return false;
197                 } else if (this.getClass() != object.getClass()) {
198                         return false;
199                 }
200
201                 final Department other = (Department) object;
202
203                 if (!Objects.equals(this.getDepartmentId(), other.getDepartmentId())) {
204                         return false;
205                 } else if (!Objects.equals(this.getDepartmentCompany(), other.getDepartmentCompany())) {
206                         return false;
207                 } else if (!Objects.equals(this.getDepartmentI18nKey(), other.getDepartmentI18nKey())) {
208                         return false;
209                 }
210
211                 return true;
212         }
213
214         @Override
215         public BranchOffice getDepartmentBranchOffice () {
216                 return this.departmentBranchOffice;
217         }
218
219         @Override
220         public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
221                 this.departmentBranchOffice = departmentBranchOffice;
222         }
223
224         @Override
225         public BasicData getDepartmentCompany () {
226                 return this.departmentCompany;
227         }
228
229         @Override
230         public void setDepartmentCompany (final BasicData departmentCompany) {
231                 this.departmentCompany = departmentCompany;
232         }
233
234         @Override
235         @SuppressWarnings ("ReturnOfDateField")
236         public Date getDepartmentCreated () {
237                 return this.departmentCreated;
238         }
239
240         @Override
241         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
242         public void setDepartmentCreated (final Date departmentCreated) {
243                 this.departmentCreated = departmentCreated;
244         }
245
246         @Override
247         public Headquarter getDepartmentHeadquarter () {
248                 return this.departmentHeadquarter;
249         }
250
251         @Override
252         public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
253                 this.departmentHeadquarter = departmentHeadquarter;
254         }
255
256         @Override
257         public String getDepartmentI18nKey () {
258                 return this.departmentI18nKey;
259         }
260
261         @Override
262         public void setDepartmentI18nKey (final String departmentI18nKey) {
263                 this.departmentI18nKey = departmentI18nKey;
264         }
265
266         @Override
267         public Long getDepartmentId () {
268                 return this.departmentId;
269         }
270
271         @Override
272         public void setDepartmentId (final Long departmentId) {
273                 this.departmentId = departmentId;
274         }
275
276         @Override
277         public Employable getDepartmentLead () {
278                 return this.departmentLead;
279         }
280
281         @Override
282         public void setDepartmentLead (final Employable departmentLead) {
283                 this.departmentLead = departmentLead;
284         }
285
286         @Override
287         public User getDepartmentUserOwner () {
288                 return this.departmentUserOwner;
289         }
290
291         @Override
292         public void setDepartmentUserOwner (final User departmentUserOwner) {
293                 this.departmentUserOwner = departmentUserOwner;
294         }
295
296         @Override
297         public int hashCode () {
298                 int hash = 5;
299
300                 hash = 53 * hash + Objects.hashCode(this.getDepartmentId());
301                 hash = 53 * hash + Objects.hashCode(this.getDepartmentCompany());
302                 hash = 53 * hash + Objects.hashCode(this.getDepartmentI18nKey());
303
304                 return hash;
305         }
306
307 }