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.FetchType;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.JoinTable;
33 import javax.persistence.ManyToMany;
34 import javax.persistence.NamedQueries;
35 import javax.persistence.NamedQuery;
36 import javax.persistence.OneToOne;
37 import javax.persistence.Table;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40 import javax.persistence.Transient;
41 import org.apache.commons.lang3.StringUtils;
42 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
43 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
44 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
45 import org.mxchange.jcontactsbusiness.model.employee.Employable;
46 import org.mxchange.jcontactsbusiness.model.employee.Employees;
47 import org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime;
48 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
49 import org.mxchange.jcoreutils.Comparables;
50 import org.mxchange.jcoreutils.SafeNumberUtils;
51 import org.mxchange.jcountry.model.data.Country;
52 import org.mxchange.jcountry.model.data.CountryData;
53 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
54 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
55 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
56 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
57 import org.mxchange.jusercore.model.user.LoginUser;
58 import org.mxchange.jusercore.model.user.User;
61 * A POJO for company branch offices
63 * @author Roland Häder<roland@mxchange.org>
65 @Entity (name = "company_branch_offices")
66 @Table (name = "company_branch_offices")
69 @NamedQuery (name = "AllBranchOffices", query = "SELECT bo FROM company_branch_offices AS bo ORDER BY bo.branchId ASC")
72 @SuppressWarnings ("PersistenceUnitPresent")
73 public class BusinessBranchOffice implements BranchOffice {
79 private static final long serialVersionUID = 47_957_817_276_871_852L;
82 * Branch office's city name
84 @Basic (optional = false)
85 @Column (name = "branch_city", length = 100, nullable = false)
86 private String branchCity;
89 * Company that has this branch office
91 @JoinColumn (name = "branch_company_id", nullable = false, updatable = false)
92 @OneToOne (targetEntity = BusinessBasicData.class, optional = false, cascade = CascadeType.REFRESH)
93 private BasicData branchCompany;
96 * Reference to contact person
98 @JoinColumn (name = "branch_contact_employee_id", referencedColumnName = "employee_id")
99 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
100 private Employable branchContactEmployee;
103 * Branch office's country code
105 @JoinColumn (name = "branch_country_id", referencedColumnName = "country_id", nullable = false)
106 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
107 private Country branchCountry;
110 * Timestamp when this entry has been created
112 @Basic (optional = false)
113 @Temporal (TemporalType.TIMESTAMP)
114 @Column (name = "branch_entry_created", nullable = false, updatable = false)
115 private Date branchCreated;
118 * Branch office's main email address (example: branch-name@company.com)
120 @Column (name = "branch_email_address", length = 100)
121 private String branchEmailAddress;
124 * Branch office's main fax number: +ccxxxxxxxxxx
126 @JoinColumn (name = "branch_fax_number_id", referencedColumnName = "fax_id")
127 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
128 private DialableFaxNumber branchFaxNumber;
131 * Branch office's house number
133 @Basic (optional = false)
134 @Column (name = "branch_house_number", nullable = false)
135 private Short branchHouseNumber;
138 * House number's extension
140 @Column (name = "branch_house_number_extension")
141 private String branchHouseNumberExtension;
147 @GeneratedValue (strategy = GenerationType.IDENTITY)
148 @Column (name = "branch_id", nullable = false, updatable = false)
149 private Long branchId;
152 * Branch office's main land-line number: +ccxxxxxxxxxx
154 @JoinColumn (name = "branch_landline_number_id", referencedColumnName = "landline_id")
155 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
156 private DialableLandLineNumber branchLandLineNumber;
159 * Branch office's last house number
161 @Column (name = "branch_last_house_number")
162 private Short branchLastHouseNumber;
165 * Numer of this branch office
167 @Column (name = "branch_number")
168 private Long branchNumber;
171 * Opening times for this branch office
174 name = "branch_opening_times",
177 name = "branch_opening_id",
178 referencedColumnName = "branch_id"
182 name = "opening_branch_id",
183 referencedColumnName = "opening_times_id"
186 @ManyToMany (targetEntity = BusinessOpeningTime.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
187 private List<OpeningTime> branchOpeningTimes;
190 * Reference to branch office owner (for example some franchise supermarkets
191 * will have an owning person (here generally referred as "employee") who
192 * will own one or more branch offices.
194 @JoinColumn (name = "branch_owner_employee_id", referencedColumnName = "employee_id")
195 @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
196 private Employable branchOwnerEmployee;
199 * Branch office's store (if multiple-store building)
201 @Column (name = "branch_store")
202 private Short branchStore;
205 * Branch office's street name
207 @Basic (optional = false)
208 @Column (name = "branch_street", length = 100, nullable = false)
209 private String branchStreet;
212 * Branch office's suite number (if applyable)
214 @Column (name = "branch_suite_number")
215 private Short branchSuiteNumber;
218 * User owner instance
220 @JoinColumn (name = "branch_user_id", referencedColumnName = "user_id")
221 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
222 private User branchUserOwner;
225 * Branch office's ZIP code
227 @Basic (optional = false)
228 @Column (name = "branch_zip_code", nullable = false)
229 private Integer branchZipCode;
232 * Default constructor for JPA
234 public BusinessBranchOffice () {
238 * Constructor with all required fields. This constructor may throw
239 * exceptions when one parameter is not valid or NULL.
241 * @param branchCity Branch office's city
242 * @param branchCompany Branch office's assigned company
243 * @param branchCountry Branch office's country
244 * @param branchStreet Branch office's street
245 * @param branchZipCode Branch office's ZIP code
246 * @param branchHouseNumber Branch office's house number
248 public BusinessBranchOffice (final String branchCity, final BasicData branchCompany, final Country branchCountry, final String branchStreet, final Integer branchZipCode, final Short branchHouseNumber) {
249 // Call other constructor
252 // Check all parameter
253 if (null == branchCity) {
255 throw new NullPointerException("branchCity is null"); //NOI18N
256 } else if (branchCity.isEmpty()) {
258 throw new IllegalArgumentException("branchCity is empty"); //NOI18N
259 } else if (null == branchCompany) {
261 throw new NullPointerException("branchCompany is null"); //NOI18N
262 } else if (branchCompany.getBasicDataId() == null) {
264 throw new NullPointerException("branchCompany.basicDataId is null"); //NOI18N
265 } else if (branchCompany.getBasicDataId() < 1) {
267 throw new IllegalArgumentException(MessageFormat.format("branchCompany.basicDataId={0} is invalid", branchCompany.getBasicDataId())); //NOI18N
268 } else if (null == branchCountry) {
270 throw new NullPointerException("branchCountry is null"); //NOI18N
271 } else if (branchCountry.getCountryId() == null) {
273 throw new NullPointerException("branchCountry.countryId is null"); //NOI18N
274 } else if (branchCountry.getCountryId() < 1) {
276 throw new IllegalArgumentException(MessageFormat.format("branchCountry.countryId={0} is invalid", branchCountry.getCountryId())); //NOI18N
277 } else if (null == branchStreet) {
279 throw new NullPointerException("branchStreet is null"); //NOI18N
280 } else if (branchStreet.isEmpty()) {
282 throw new IllegalArgumentException("branchStreet is empty"); //NOI18N
283 } else if (null == branchZipCode) {
285 throw new NullPointerException("branchZipCode is null"); //NOI18N
286 } else if (branchZipCode < 1) {
288 throw new IllegalArgumentException(MessageFormat.format("branchZipCode={0} is out of range.", branchZipCode)); //NOI18N
289 } else if (null == branchHouseNumber) {
291 throw new NullPointerException("branchHouseNumber is null"); //NOI18N
292 } else if (branchHouseNumber < 1) {
294 throw new IllegalArgumentException(MessageFormat.format("branchHouseNumber={0} is out of range.", branchHouseNumber)); //NOI18N
298 this.branchCity = branchCity;
299 this.branchCompany = branchCompany;
300 this.branchCountry = branchCountry;
301 this.branchStreet = branchStreet;
302 this.branchZipCode = branchZipCode;
303 this.branchHouseNumber = branchHouseNumber;
307 public int compareTo (final BranchOffice branchOffice) {
308 // Check parameter on null-reference and equality to this
309 if (null == branchOffice) {
311 throw new NullPointerException("branchOffice is null"); //NOI18N
312 } else if (branchOffice.equals(this)) {
318 final int[] comparators = {
319 // First compare basic data
320 this.getBranchCompany().compareTo(branchOffice.getBranchCompany()),
321 // ... branch office number
322 SafeNumberUtils.compare(this.getBranchNumber(), branchOffice.getBranchNumber()),
323 // ... contact person
324 Employees.compare(this.getBranchContactEmployee(), branchOffice.getBranchContactEmployee()),
326 this.getBranchCountry().compareTo(branchOffice.getBranchCountry()),
328 this.getBranchCity().compareToIgnoreCase(branchOffice.getBranchCity()),
330 this.getBranchZipCode().compareTo(branchOffice.getBranchZipCode()),
332 this.getBranchStreet().compareToIgnoreCase(branchOffice.getBranchStreet()),
333 // ... and house number
334 this.getBranchHouseNumber().compareTo(branchOffice.getBranchHouseNumber()),
335 // ... and house number
336 SafeNumberUtils.compare(this.getBranchLastHouseNumber(), branchOffice.getBranchLastHouseNumber()),
338 StringUtils.compareIgnoreCase(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension()),
340 SafeNumberUtils.compare(this.getBranchStore(), branchOffice.getBranchStore()),
341 // ... suite number ...
342 SafeNumberUtils.compare(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())
346 final int comparison = Comparables.checkAll(comparators);
353 public boolean equals (final Object object) {
354 if (null == object) {
356 } else if (this.getClass() != object.getClass()) {
360 // @todo Maybe a bit unsafe cast?
361 final BranchOffice branchOffice = (BranchOffice) object;
363 if (!Objects.equals(this.getBranchId(), branchOffice.getBranchId())) {
365 } else if (!Objects.equals(this.getBranchCity(), branchOffice.getBranchCity())) {
367 } else if (!Objects.equals(this.getBranchCountry(), branchOffice.getBranchCountry())) {
369 } else if (!Objects.equals(this.getBranchHouseNumber(), branchOffice.getBranchHouseNumber())) {
371 } else if (!Objects.equals(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension())) {
373 } else if (!Objects.equals(this.getBranchLastHouseNumber(), branchOffice.getBranchLastHouseNumber())) {
375 } else if (!Objects.equals(this.getBranchNumber(), branchOffice.getBranchNumber())) {
377 } else if (!Objects.equals(this.getBranchStore(), branchOffice.getBranchStore())) {
379 } else if (!Objects.equals(this.getBranchStreet(), branchOffice.getBranchStreet())) {
381 } else if (!Objects.equals(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())) {
383 } else if (!Objects.equals(this.getBranchZipCode(), branchOffice.getBranchZipCode())) {
391 public String getBranchCity () {
392 return this.branchCity;
396 public void setBranchCity (final String branchCity) {
397 this.branchCity = branchCity;
401 public BasicData getBranchCompany () {
402 return this.branchCompany;
406 public void setBranchCompany (final BasicData branchCompany) {
407 this.branchCompany = branchCompany;
411 public Employable getBranchContactEmployee () {
412 return this.branchContactEmployee;
416 public void setBranchContactEmployee (final Employable branchContactEmployee) {
417 this.branchContactEmployee = branchContactEmployee;
421 public Country getBranchCountry () {
422 return this.branchCountry;
426 public void setBranchCountry (final Country branchCountry) {
427 this.branchCountry = branchCountry;
431 @SuppressWarnings ("ReturnOfDateField")
432 public Date getBranchCreated () {
433 return this.branchCreated;
437 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
438 public void setBranchCreated (final Date branchCreated) {
439 this.branchCreated = branchCreated;
443 public String getBranchEmailAddress () {
444 return this.branchEmailAddress;
448 public void setBranchEmailAddress (final String branchEmailAddress) {
449 this.branchEmailAddress = branchEmailAddress;
453 public DialableFaxNumber getBranchFaxNumber () {
454 return this.branchFaxNumber;
458 public void setBranchFaxNumber (final DialableFaxNumber branchFaxNumber) {
459 this.branchFaxNumber = branchFaxNumber;
463 public Short getBranchHouseNumber () {
464 return this.branchHouseNumber;
468 public void setBranchHouseNumber (final Short branchHouseNumber) {
469 this.branchHouseNumber = branchHouseNumber;
473 public String getBranchHouseNumberExtension () {
474 return this.branchHouseNumberExtension;
478 public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
479 this.branchHouseNumberExtension = branchHouseNumberExtension;
483 public Long getBranchId () {
484 return this.branchId;
488 public void setBranchId (final Long branchId) {
489 this.branchId = branchId;
493 public DialableLandLineNumber getBranchLandLineNumber () {
494 return this.branchLandLineNumber;
498 public void setBranchLandLineNumber (final DialableLandLineNumber branchLandLineNumber) {
499 this.branchLandLineNumber = branchLandLineNumber;
503 public Short getBranchLastHouseNumber () {
504 return this.branchLastHouseNumber;
508 public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
509 this.branchLastHouseNumber = branchLastHouseNumber;
513 public Long getBranchNumber () {
514 return this.branchNumber;
518 public void setBranchNumber (final Long branchNumber) {
519 this.branchNumber = branchNumber;
523 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
524 public List<OpeningTime> getBranchOpeningTimes () {
525 return this.branchOpeningTimes;
529 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
530 public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
531 this.branchOpeningTimes = branchOpeningTimes;
535 public Employable getBranchOwnerEmployee () {
536 return this.branchOwnerEmployee;
540 public void setBranchOwnerEmployee (final Employable branchOwnerEmployee) {
541 this.branchOwnerEmployee = branchOwnerEmployee;
545 public Short getBranchStore () {
546 return this.branchStore;
550 public void setBranchStore (final Short branchStore) {
551 this.branchStore = branchStore;
555 public String getBranchStreet () {
556 return this.branchStreet;
560 public void setBranchStreet (final String branchStreet) {
561 this.branchStreet = branchStreet;
565 public Short getBranchSuiteNumber () {
566 return this.branchSuiteNumber;
570 public void setBranchSuiteNumber (final Short branchSuiteNumber) {
571 this.branchSuiteNumber = branchSuiteNumber;
575 public User getBranchUserOwner () {
576 return this.branchUserOwner;
580 public void setBranchUserOwner (final User branchUserOwner) {
581 this.branchUserOwner = branchUserOwner;
585 public Integer getBranchZipCode () {
586 return this.branchZipCode;
590 public void setBranchZipCode (final Integer branchZipCode) {
591 this.branchZipCode = branchZipCode;
595 public int hashCode () {
598 hash = 53 * hash + Objects.hashCode(this.getBranchId());
599 hash = 53 * hash + Objects.hashCode(this.getBranchCity());
600 hash = 53 * hash + Objects.hashCode(this.getBranchCountry());
601 hash = 53 * hash + Objects.hashCode(this.getBranchNumber());
602 hash = 53 * hash + Objects.hashCode(this.getBranchHouseNumber());
603 hash = 53 * hash + Objects.hashCode(this.getBranchHouseNumberExtension());
604 hash = 53 * hash + Objects.hashCode(this.getBranchLastHouseNumber());
605 hash = 53 * hash + Objects.hashCode(this.getBranchStore());
606 hash = 53 * hash + Objects.hashCode(this.getBranchStreet());
607 hash = 53 * hash + Objects.hashCode(this.getBranchSuiteNumber());
608 hash = 53 * hash + Objects.hashCode(this.getBranchZipCode());