2 * Copyright (C) 2016 - 2018 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.branchoffice;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.JoinTable;
32 import javax.persistence.ManyToMany;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
41 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
42 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
43 import org.mxchange.jcontactsbusiness.model.employee.Employable;
44 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
45 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
46 import org.mxchange.jcoreee.utils.Comparables;
47 import org.mxchange.jcoreee.utils.NumberUtils;
48 import org.mxchange.jcoreee.utils.StringUtils;
49 import org.mxchange.jcountry.model.data.Country;
50 import org.mxchange.jcountry.model.data.CountryData;
51 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
52 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
53 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
54 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
55 import org.mxchange.jusercore.model.user.LoginUser;
56 import org.mxchange.jusercore.model.user.User;
59 * A POJO for company branch offices
61 * @author Roland Häder<roland@mxchange.org>
63 @Entity (name = "company_branch_offices")
64 @Table (name = "company_branch_offices")
67 @NamedQuery (name = "AllBranchOffices", query = "SELECT bo FROM company_branch_offices AS bo ORDER BY bo.branchId ASC")
70 @SuppressWarnings ("PersistenceUnitPresent")
71 public class BusinessBranchOffice implements BranchOffice {
77 private static final long serialVersionUID = 47_957_817_276_871_852L;
80 * Branch office's city name
82 @Basic (optional = false)
83 @Column (name = "branch_city", length = 100, nullable = false)
84 private String branchCity;
87 * Company that has this branch office
89 @JoinColumn (name = "branch_company_id", nullable = false, updatable = false)
90 @OneToOne (targetEntity = BusinessBasicData.class, optional = false, cascade = CascadeType.REFRESH)
91 private BasicData branchCompany;
94 * Reference to contact person
96 @JoinColumn (name = "branch_contact_employee_id", referencedColumnName = "employee_id")
97 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
98 private Employable branchContactEmployee;
101 * Branch office's country code
103 @JoinColumn (name = "branch_country_id", referencedColumnName = "country_id", nullable = false)
104 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
105 private Country branchCountry;
108 * Timestamp when this entry has been created
110 @Basic (optional = false)
111 @Temporal (TemporalType.TIMESTAMP)
112 @Column (name = "branch_entry_created", nullable = false, updatable = false)
113 private Date branchCreated;
116 * Branch office's main email address (example: branch-name@company.com)
118 @Column (name = "branch_email_address", length = 100)
119 private String branchEmailAddress;
122 * Branch office's main fax number: +ccxxxxxxxxxx
124 @JoinColumn (name = "branch_fax_number_id", referencedColumnName = "fax_id")
125 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
126 private DialableFaxNumber branchFaxNumber;
129 * Branch office's house number
131 @Basic (optional = false)
132 @Column (name = "branch_house_number", nullable = false)
133 private Short branchHouseNumber;
136 * House number's extension
138 @Column (name = "branch_house_number_extension")
139 private String branchHouseNumberExtension;
145 @GeneratedValue (strategy = GenerationType.IDENTITY)
146 @Column (name = "branch_id", nullable = false, updatable = false)
147 private Long branchId;
150 * Branch office's main land-line number: +ccxxxxxxxxxx
152 @JoinColumn (name = "branch_landline_number_id", referencedColumnName = "landline_id")
153 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
154 private DialableLandLineNumber branchLandLineNumber;
157 * Branch office's last house number
159 @Column (name = "branch_last_house_number")
160 private Short branchLastHouseNumber;
163 * Numer of this branch office
165 @Column (name = "branch_number")
166 private Long branchNumber;
169 * Opening times for this branch office
171 @JoinTable (name = "branch_opening_times", joinColumns = @JoinColumn(name = "branch_opening_id", referencedColumnName = "branch_id"), inverseJoinColumns = @JoinColumn(name = "opening_branch_id", referencedColumnName = "opening_times_id"))
172 @ManyToMany (targetEntity = BusinessOpeningTime.class, cascade = CascadeType.ALL)
173 private List<OpeningTime> branchOpeningTimes;
176 * Reference to branch office owner (for example some franchise supermarkets
177 * will have an owning person (here generally referred as "employee") who
178 * will own one or more branch offices.
180 @JoinColumn (name = "branch_owner_employee_id", referencedColumnName = "employee_id")
181 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
182 private Employable branchOwnerEmployee;
185 * Branch office's store (if multiple-store building)
187 @Column (name = "branch_store")
188 private Short branchStore;
191 * Branch office's street name
193 @Basic (optional = false)
194 @Column (name = "branch_street", length = 100, nullable = false)
195 private String branchStreet;
198 * Branch office's suite number (if applyable)
200 @Column (name = "branch_suite_number")
201 private Short branchSuiteNumber;
204 * User owner instance
206 @JoinColumn (name = "branch_user_id", referencedColumnName = "user_id")
207 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
208 private User branchUserOwner;
211 * Branch office's ZIP code
213 @Basic (optional = false)
214 @Column (name = "branch_zip_code", nullable = false)
215 private Integer branchZipCode;
218 * Default constructor for JPA
220 public BusinessBranchOffice () {
224 * Constructor with all required fields. This constructor may throw
225 * exceptions when one parameter is not valid or NULL.
227 * @param branchCity Branch office's city
228 * @param branchCompany Branch office's assigned company
229 * @param branchCountry Branch office's country
230 * @param branchStreet Branch office's street
231 * @param branchZipCode Branch office's ZIP code
232 * @param branchHouseNumber Branch office's house number
234 public BusinessBranchOffice (final String branchCity, final BasicData branchCompany, final Country branchCountry, final String branchStreet, final Integer branchZipCode, final Short branchHouseNumber) {
235 // Call other constructor
238 // Check all parameter
239 if (null == branchCity) {
241 throw new NullPointerException("branchCity is null"); //NOI18N
242 } else if (branchCity.isEmpty()) {
244 throw new IllegalArgumentException("branchCity is empty"); //NOI18N
245 } else if (null == branchCompany) {
247 throw new NullPointerException("branchCompany is null"); //NOI18N
248 } else if (branchCompany.getBasicDataId() == null) {
250 throw new NullPointerException("branchCompany.basicDataId is null"); //NOI18N
251 } else if (branchCompany.getBasicDataId() < 1) {
253 throw new IllegalArgumentException(MessageFormat.format("branchCompany.basicDataId={0} is invalid", branchCompany.getBasicDataId())); //NOI18N
254 } else if (null == branchCountry) {
256 throw new NullPointerException("branchCountry is null"); //NOI18N
257 } else if (branchCountry.getCountryId() == null) {
259 throw new NullPointerException("branchCountry.countryId is null"); //NOI18N
260 } else if (branchCountry.getCountryId() < 1) {
262 throw new IllegalArgumentException(MessageFormat.format("branchCountry.countryId={0} is invalid", branchCountry.getCountryId())); //NOI18N
263 } else if (null == branchStreet) {
265 throw new NullPointerException("branchStreet is null"); //NOI18N
266 } else if (branchStreet.isEmpty()) {
268 throw new IllegalArgumentException("branchStreet is empty"); //NOI18N
269 } else if (null == branchZipCode) {
271 throw new NullPointerException("branchZipCode is null"); //NOI18N
272 } else if (branchZipCode < 1) {
274 throw new IllegalArgumentException(MessageFormat.format("branchZipCode={0} is out of range.", branchZipCode)); //NOI18N
275 } else if (null == branchHouseNumber) {
277 throw new NullPointerException("branchHouseNumber is null"); //NOI18N
278 } else if (branchHouseNumber < 1) {
280 throw new IllegalArgumentException(MessageFormat.format("branchHouseNumber={0} is out of range.", branchHouseNumber)); //NOI18N
284 this.branchCity = branchCity;
285 this.branchCompany = branchCompany;
286 this.branchCountry = branchCountry;
287 this.branchStreet = branchStreet;
288 this.branchZipCode = branchZipCode;
289 this.branchHouseNumber = branchHouseNumber;
293 public int compareTo (final BranchOffice branchOffice) {
294 // For performance reasons
295 if (null == branchOffice) {
297 throw new NullPointerException("branchOffice is null"); //NOI18N
298 } else if (Objects.equals(this, branchOffice)) {
304 final int[] comparators = {
305 // A different branch number is a clear indication ...
306 NumberUtils.compare(this.getBranchNumber(), branchOffice.getBranchNumber()),
307 // ... same with id ...
308 NumberUtils.compare(this.getBranchId(), branchOffice.getBranchId()),
310 this.getBranchCountry().compareTo(branchOffice.getBranchCountry()),
312 this.getBranchCity().compareToIgnoreCase(branchOffice.getBranchCity()),
314 this.getBranchZipCode().compareTo(branchOffice.getBranchZipCode()),
316 this.getBranchStreet().compareToIgnoreCase(branchOffice.getBranchStreet()),
317 // ... and house number
318 this.getBranchHouseNumber().compareTo(branchOffice.getBranchHouseNumber()),
319 // ... and house number
320 NumberUtils.compare(this.getBranchLastHouseNumber(), branchOffice.getBranchLastHouseNumber()),
322 StringUtils.compareToIgnoreCase(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension()),
324 NumberUtils.compare(this.getBranchStore(), branchOffice.getBranchStore()),
325 // ... suite number ...
326 NumberUtils.compare(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())
330 final int comparison = Comparables.checkAll(comparators);
337 public boolean equals (final Object object) {
338 if (null == object) {
340 } else if (this.getClass() != object.getClass()) {
344 // @todo Maybe a bit unsafe cast?
345 final BranchOffice branchOffice = (BranchOffice) object;
347 if (!Objects.equals(this.getBranchId(), branchOffice.getBranchId())) {
349 } else if (!Objects.equals(this.getBranchCity(), branchOffice.getBranchCity())) {
351 } else if (!Objects.equals(this.getBranchCountry(), branchOffice.getBranchCountry())) {
353 } else if (!Objects.equals(this.getBranchHouseNumber(), branchOffice.getBranchHouseNumber())) {
355 } else if (!Objects.equals(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension())) {
357 } else if (!Objects.equals(this.getBranchLastHouseNumber(), branchOffice.getBranchLastHouseNumber())) {
359 } else if (!Objects.equals(this.getBranchNumber(), branchOffice.getBranchNumber())) {
361 } else if (!Objects.equals(this.getBranchStore(), branchOffice.getBranchStore())) {
363 } else if (!Objects.equals(this.getBranchStreet(), branchOffice.getBranchStreet())) {
365 } else if (!Objects.equals(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())) {
367 } else if (!Objects.equals(this.getBranchZipCode(), branchOffice.getBranchZipCode())) {
375 public String getBranchCity () {
376 return this.branchCity;
380 public void setBranchCity (final String branchCity) {
381 this.branchCity = branchCity;
385 public BasicData getBranchCompany () {
386 return this.branchCompany;
390 public void setBranchCompany (final BasicData branchCompany) {
391 this.branchCompany = branchCompany;
395 public Employable getBranchContactEmployee () {
396 return this.branchContactEmployee;
400 public void setBranchContactEmployee (final Employable branchContactEmployee) {
401 this.branchContactEmployee = branchContactEmployee;
405 public Country getBranchCountry () {
406 return this.branchCountry;
410 public void setBranchCountry (final Country branchCountry) {
411 this.branchCountry = branchCountry;
415 @SuppressWarnings ("ReturnOfDateField")
416 public Date getBranchCreated () {
417 return this.branchCreated;
421 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
422 public void setBranchCreated (final Date branchCreated) {
423 this.branchCreated = branchCreated;
427 public String getBranchEmailAddress () {
428 return this.branchEmailAddress;
432 public void setBranchEmailAddress (final String branchEmailAddress) {
433 this.branchEmailAddress = branchEmailAddress;
437 public DialableFaxNumber getBranchFaxNumber () {
438 return this.branchFaxNumber;
442 public void setBranchFaxNumber (final DialableFaxNumber branchFaxNumber) {
443 this.branchFaxNumber = branchFaxNumber;
447 public Short getBranchHouseNumber () {
448 return this.branchHouseNumber;
452 public void setBranchHouseNumber (final Short branchHouseNumber) {
453 this.branchHouseNumber = branchHouseNumber;
457 public String getBranchHouseNumberExtension () {
458 return this.branchHouseNumberExtension;
462 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
463 this.branchHouseNumberExtension = branchHouseNumberExtension;
467 public Long getBranchId () {
468 return this.branchId;
472 public void setBranchId (final Long branchId) {
473 this.branchId = branchId;
477 public DialableLandLineNumber getBranchLandLineNumber () {
478 return this.branchLandLineNumber;
482 public void setBranchLandLineNumber (final DialableLandLineNumber branchLandLineNumber) {
483 this.branchLandLineNumber = branchLandLineNumber;
487 public Short getBranchLastHouseNumber () {
488 return this.branchLastHouseNumber;
492 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
493 this.branchLastHouseNumber = branchLastHouseNumber;
497 public Long getBranchNumber () {
498 return this.branchNumber;
502 public void setBranchNumber (final Long branchNumber) {
503 this.branchNumber = branchNumber;
507 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
508 public List<OpeningTime> getBranchOpeningTimes () {
509 return this.branchOpeningTimes;
513 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
514 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
515 this.branchOpeningTimes = branchOpeningTimes;
519 public Employable getBranchOwnerEmployee () {
520 return this.branchOwnerEmployee;
524 public void setBranchOwnerEmployee (final Employable branchOwnerEmployee) {
525 this.branchOwnerEmployee = branchOwnerEmployee;
529 public Short getBranchStore () {
530 return this.branchStore;
534 public void setBranchStore (final Short branchStore) {
535 this.branchStore = branchStore;
539 public String getBranchStreet () {
540 return this.branchStreet;
544 public void setBranchStreet (final String branchStreet) {
545 this.branchStreet = branchStreet;
549 public Short getBranchSuiteNumber () {
550 return this.branchSuiteNumber;
554 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
555 this.branchSuiteNumber = branchSuiteNumber;
559 public User getBranchUserOwner () {
560 return this.branchUserOwner;
564 public void setBranchUserOwner (final User branchUserOwner) {
565 this.branchUserOwner = branchUserOwner;
569 public Integer getBranchZipCode () {
570 return this.branchZipCode;
574 public void setBranchZipCode (final Integer branchZipCode) {
575 this.branchZipCode = branchZipCode;
579 public int hashCode () {
582 hash = 53 * hash + Objects.hashCode(this.getBranchId());
583 hash = 53 * hash + Objects.hashCode(this.getBranchCity());
584 hash = 53 * hash + Objects.hashCode(this.getBranchCountry());
585 hash = 53 * hash + Objects.hashCode(this.getBranchNumber());
586 hash = 53 * hash + Objects.hashCode(this.getBranchHouseNumber());
587 hash = 53 * hash + Objects.hashCode(this.getBranchHouseNumberExtension());
588 hash = 53 * hash + Objects.hashCode(this.getBranchLastHouseNumber());
589 hash = 53 * hash + Objects.hashCode(this.getBranchStore());
590 hash = 53 * hash + Objects.hashCode(this.getBranchStreet());
591 hash = 53 * hash + Objects.hashCode(this.getBranchSuiteNumber());
592 hash = 53 * hash + Objects.hashCode(this.getBranchZipCode());