2 * Copyright (C) 2016 - 2020 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.jcontactsbusiness.model.department;
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;
52 * A POJO for company departments
54 * @author Roland Häder<roland@mxchange.org>
56 @Entity (name = "company_departments")
57 @Table (name = "company_departments")
60 @NamedQuery (name = "AllDepartments", query = "SELECT d FROM company_departments AS d ORDER BY d.departmentId ASC")
63 @SuppressWarnings ("PersistenceUnitPresent")
64 public class BusinessDepartment implements Department {
70 private static final long serialVersionUID = 94_835_918_958_717_660L;
73 * Where this department is located
75 @JoinColumn (name = "department_branch_id")
76 @ManyToOne (targetEntity = BusinessBranchOffice.class, cascade = CascadeType.REFRESH)
77 private BranchOffice departmentBranchOffice;
80 * Connection to business contact
82 @JoinColumn (name = "department_company_id", nullable = false, updatable = false)
83 @ManyToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH, optional = false)
84 private BasicData departmentCompany;
87 * Timestamp when this entry has been created
89 @Basic (optional = false)
90 @Temporal (TemporalType.TIMESTAMP)
91 @Column (name = "department_created", nullable = false, updatable = false)
92 private Date departmentCreated;
95 * Where this department is located
97 @JoinColumn (name = "department_headquarter_id")
98 @OneToOne (targetEntity = BusinessHeadquarter.class, cascade = CascadeType.REFRESH)
99 private Headquarter departmentHeadquarter;
102 * Department i18n key
104 @Basic (optional = false)
105 @Column (name = "department_i18n_key", length = 100, nullable = false)
106 private String departmentI18nKey;
112 @GeneratedValue (strategy = GenerationType.IDENTITY)
113 @Column (name = "department_id", nullable = false, updatable = false)
114 private Long departmentId;
117 * Department lead employee
119 @JoinColumn (name = "department_lead_id")
120 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
121 private Employable departmentLead;
124 * User owner instance
126 @JoinColumn (name = "department_user_id")
127 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
128 private User departmentUserOwner;
131 * Default constructor
133 public BusinessDepartment () {
137 * Constructor with all required fields
139 * @param departmentCompany Basic data instance
140 * @param departmentName Department name
142 public BusinessDepartment (final BasicData departmentCompany, final String departmentName) {
143 // Call other constructor
146 // Validate parameter
147 if (null == departmentCompany) {
149 throw new NullPointerException("departmentCompany is null"); //NOI18N
150 } else if (null == departmentName) {
152 throw new NullPointerException("departmentName is null"); //NOI18N
153 } else if (departmentName.isEmpty()) {
155 throw new IllegalArgumentException("departmentName is empty"); //NOI18N
159 this.departmentCompany = departmentCompany;
160 this.departmentI18nKey = departmentName;
164 public int compareTo (final Department department) {
165 // Check parameter on null-reference and equality to this
166 if (null == department) {
168 throw new NullPointerException("department is null"); //NOI18N
169 } else if (department.equals(this)) {
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()),
181 BranchOffices.compare(this.getDepartmentBranchOffice(), department.getDepartmentBranchOffice()),
182 // ... finally department's i18n key
183 this.getDepartmentI18nKey().compareTo(department.getDepartmentI18nKey())
187 final int comparison = Comparables.checkAll(comparators);
194 public boolean equals (final Object object) {
195 if (null == object) {
197 } else if (this.getClass() != object.getClass()) {
201 final Department other = (Department) object;
203 if (!Objects.equals(this.getDepartmentId(), other.getDepartmentId())) {
205 } else if (!Objects.equals(this.getDepartmentCompany(), other.getDepartmentCompany())) {
207 } else if (!Objects.equals(this.getDepartmentI18nKey(), other.getDepartmentI18nKey())) {
215 public BranchOffice getDepartmentBranchOffice () {
216 return this.departmentBranchOffice;
220 public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
221 this.departmentBranchOffice = departmentBranchOffice;
225 public BasicData getDepartmentCompany () {
226 return this.departmentCompany;
230 public void setDepartmentCompany (final BasicData departmentCompany) {
231 this.departmentCompany = departmentCompany;
235 @SuppressWarnings ("ReturnOfDateField")
236 public Date getDepartmentCreated () {
237 return this.departmentCreated;
241 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
242 public void setDepartmentCreated (final Date departmentCreated) {
243 this.departmentCreated = departmentCreated;
247 public Headquarter getDepartmentHeadquarter () {
248 return this.departmentHeadquarter;
252 public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
253 this.departmentHeadquarter = departmentHeadquarter;
257 public String getDepartmentI18nKey () {
258 return this.departmentI18nKey;
262 public void setDepartmentI18nKey (final String departmentI18nKey) {
263 this.departmentI18nKey = departmentI18nKey;
267 public Long getDepartmentId () {
268 return this.departmentId;
272 public void setDepartmentId (final Long departmentId) {
273 this.departmentId = departmentId;
277 public Employable getDepartmentLead () {
278 return this.departmentLead;
282 public void setDepartmentLead (final Employable departmentLead) {
283 this.departmentLead = departmentLead;
287 public User getDepartmentUserOwner () {
288 return this.departmentUserOwner;
292 public void setDepartmentUserOwner (final User departmentUserOwner) {
293 this.departmentUserOwner = departmentUserOwner;
297 public int hashCode () {
300 hash = 53 * hash + Objects.hashCode(this.getDepartmentId());
301 hash = 53 * hash + Objects.hashCode(this.getDepartmentCompany());
302 hash = 53 * hash + Objects.hashCode(this.getDepartmentI18nKey());