]> git.mxchange.org Git - jcontacts-business-core.git/blob - src/org/mxchange/jcontactsbusiness/model/branchoffice/BusinessBranchOffice.java
Updated jar(s)
[jcontacts-business-core.git] / src / org / mxchange / jcontactsbusiness / model / branchoffice / BusinessBranchOffice.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.branchoffice;
18
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;
59
60 /**
61  * A POJO for company branch offices
62  * <p>
63  * @author Roland Häder<roland@mxchange.org>
64  */
65 @Entity (name = "company_branch_offices")
66 @Table (name = "company_branch_offices")
67 @NamedQueries (
68                 {
69                         @NamedQuery (name = "AllBranchOffices", query = "SELECT bo FROM company_branch_offices AS bo ORDER BY bo.branchId ASC")
70                 }
71 )
72 @SuppressWarnings ("PersistenceUnitPresent")
73 public class BusinessBranchOffice implements BranchOffice {
74
75         /**
76          * Serial number
77          */
78         @Transient
79         private static final long serialVersionUID = 47_957_817_276_871_852L;
80
81         /**
82          * Branch office's city name
83          */
84         @Basic (optional = false)
85         @Column (name = "branch_city", length = 100, nullable = false)
86         private String branchCity;
87
88         /**
89          * Company that has this branch office
90          */
91         @JoinColumn (name = "branch_company_id", nullable = false, updatable = false)
92         @OneToOne (targetEntity = BusinessBasicData.class, optional = false, cascade = CascadeType.REFRESH)
93         private BasicData branchCompany;
94
95         /**
96          * Reference to contact person
97          */
98         @JoinColumn (name = "branch_contact_employee_id", referencedColumnName = "employee_id")
99         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
100         private Employable branchContactEmployee;
101
102         /**
103          * Branch office's country code
104          */
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;
108
109         /**
110          * Timestamp when this entry has been created
111          */
112         @Basic (optional = false)
113         @Temporal (TemporalType.TIMESTAMP)
114         @Column (name = "branch_entry_created", nullable = false, updatable = false)
115         private Date branchCreated;
116
117         /**
118          * Branch office's main email address (example: branch-name@company.com)
119          */
120         @Column (name = "branch_email_address", length = 100)
121         private String branchEmailAddress;
122
123         /**
124          * Branch office's main fax number: +ccxxxxxxxxxx
125          */
126         @JoinColumn (name = "branch_fax_number_id", referencedColumnName = "fax_id")
127         @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
128         private DialableFaxNumber branchFaxNumber;
129
130         /**
131          * Branch office's house number
132          */
133         @Basic (optional = false)
134         @Column (name = "branch_house_number", nullable = false)
135         private Short branchHouseNumber;
136
137         /**
138          * House number's extension
139          */
140         @Column (name = "branch_house_number_extension")
141         private String branchHouseNumberExtension;
142
143         /**
144          * Id number
145          */
146         @Id
147         @GeneratedValue (strategy = GenerationType.IDENTITY)
148         @Column (name = "branch_id", nullable = false, updatable = false)
149         private Long branchId;
150
151         /**
152          * Branch office's main land-line number: +ccxxxxxxxxxx
153          */
154         @JoinColumn (name = "branch_landline_number_id", referencedColumnName = "landline_id")
155         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
156         private DialableLandLineNumber branchLandLineNumber;
157
158         /**
159          * Branch office's last house number
160          */
161         @Column (name = "branch_last_house_number")
162         private Short branchLastHouseNumber;
163
164         /**
165          * Numer of this branch office
166          */
167         @Column (name = "branch_number")
168         private Long branchNumber;
169
170         /**
171          * Opening times for this branch office
172          */
173         @JoinTable (
174                         name = "branch_opening_times",
175                         joinColumns =
176                         @JoinColumn (
177                                         name = "branch_opening_id",
178                                         referencedColumnName = "branch_id"
179                         ),
180                         inverseJoinColumns =
181                         @JoinColumn (
182                                         name = "opening_branch_id",
183                                         referencedColumnName = "opening_times_id"
184                         )
185         )
186         @ManyToMany (targetEntity = BusinessOpeningTime.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
187         private List<OpeningTime> branchOpeningTimes;
188
189         /**
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.
193          */
194         @JoinColumn (name = "branch_owner_employee_id", referencedColumnName = "employee_id")
195         @OneToOne (targetEntity = BusinessEmployee.class, cascade = CascadeType.REFRESH)
196         private Employable branchOwnerEmployee;
197
198         /**
199          * Branch office's store (if multiple-store building)
200          */
201         @Column (name = "branch_store")
202         private Short branchStore;
203
204         /**
205          * Branch office's street name
206          */
207         @Basic (optional = false)
208         @Column (name = "branch_street", length = 100, nullable = false)
209         private String branchStreet;
210
211         /**
212          * Branch office's suite number (if applyable)
213          */
214         @Column (name = "branch_suite_number")
215         private Short branchSuiteNumber;
216
217         /**
218          * User owner instance
219          */
220         @JoinColumn (name = "branch_user_id", referencedColumnName = "user_id")
221         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
222         private User branchUserOwner;
223
224         /**
225          * Branch office's ZIP code
226          */
227         @Basic (optional = false)
228         @Column (name = "branch_zip_code", nullable = false)
229         private Integer branchZipCode;
230
231         /**
232          * Default constructor for JPA
233          */
234         public BusinessBranchOffice () {
235         }
236
237         /**
238          * Constructor with all required fields. This constructor may throw
239          * exceptions when one parameter is not valid or NULL.
240          * <p>
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
247          */
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
250                 this();
251
252                 // Check all parameter
253                 if (null == branchCity) {
254                         // Throw NPE
255                         throw new NullPointerException("branchCity is null"); //NOI18N
256                 } else if (branchCity.isEmpty()) {
257                         // Throw IAE
258                         throw new IllegalArgumentException("branchCity is empty"); //NOI18N
259                 } else if (null == branchCompany) {
260                         // Throw NPE
261                         throw new NullPointerException("branchCompany is null"); //NOI18N
262                 } else if (branchCompany.getBasicDataId() == null) {
263                         // Throw NPE again
264                         throw new NullPointerException("branchCompany.basicDataId is null"); //NOI18N
265                 } else if (branchCompany.getBasicDataId() < 1) {
266                         // Throw IAE again
267                         throw new IllegalArgumentException(MessageFormat.format("branchCompany.basicDataId={0} is invalid", branchCompany.getBasicDataId())); //NOI18N
268                 } else if (null == branchCountry) {
269                         // Throw NPE again
270                         throw new NullPointerException("branchCountry is null"); //NOI18N
271                 } else if (branchCountry.getCountryId() == null) {
272                         // Throw NPE again
273                         throw new NullPointerException("branchCountry.countryId is null"); //NOI18N
274                 } else if (branchCountry.getCountryId() < 1) {
275                         // Throw IAE again
276                         throw new IllegalArgumentException(MessageFormat.format("branchCountry.countryId={0} is invalid", branchCountry.getCountryId())); //NOI18N
277                 } else if (null == branchStreet) {
278                         // Throw NPE
279                         throw new NullPointerException("branchStreet is null"); //NOI18N
280                 } else if (branchStreet.isEmpty()) {
281                         // Throw IAE
282                         throw new IllegalArgumentException("branchStreet is empty"); //NOI18N
283                 } else if (null == branchZipCode) {
284                         // Throw NPE
285                         throw new NullPointerException("branchZipCode is null"); //NOI18N
286                 } else if (branchZipCode < 1) {
287                         // Throw IAE
288                         throw new IllegalArgumentException(MessageFormat.format("branchZipCode={0} is out of range.", branchZipCode)); //NOI18N
289                 } else if (null == branchHouseNumber) {
290                         // Throw NPE
291                         throw new NullPointerException("branchHouseNumber is null"); //NOI18N
292                 } else if (branchHouseNumber < 1) {
293                         // Throw IAE
294                         throw new IllegalArgumentException(MessageFormat.format("branchHouseNumber={0} is out of range.", branchHouseNumber)); //NOI18N
295                 }
296
297                 // Set all fields
298                 this.branchCity = branchCity;
299                 this.branchCompany = branchCompany;
300                 this.branchCountry = branchCountry;
301                 this.branchStreet = branchStreet;
302                 this.branchZipCode = branchZipCode;
303                 this.branchHouseNumber = branchHouseNumber;
304         }
305
306         @Override
307         public int compareTo (final BranchOffice branchOffice) {
308                 // Check parameter on null-reference and equality to this
309                 if (null == branchOffice) {
310                         // Should not happen
311                         throw new NullPointerException("branchOffice is null"); //NOI18N
312                 } else if (branchOffice.equals(this)) {
313                         // Same object
314                         return 0;
315                 }
316
317                 // Init comparisons
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()),
325                         // ... then country
326                         this.getBranchCountry().compareTo(branchOffice.getBranchCountry()),
327                         // ... next city
328                         this.getBranchCity().compareToIgnoreCase(branchOffice.getBranchCity()),
329                         // ... then ZIP code
330                         this.getBranchZipCode().compareTo(branchOffice.getBranchZipCode()),
331                         // ... and street
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()),
337                         // ... and extension
338                         StringUtils.compareIgnoreCase(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension()),
339                         // ... store ...
340                         SafeNumberUtils.compare(this.getBranchStore(), branchOffice.getBranchStore()),
341                         // ... suite number ...
342                         SafeNumberUtils.compare(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())
343                 };
344
345                 // Check all values
346                 final int comparison = Comparables.checkAll(comparators);
347
348                 // Return value
349                 return comparison;
350         }
351
352         @Override
353         public boolean equals (final Object object) {
354                 if (null == object) {
355                         return false;
356                 } else if (this.getClass() != object.getClass()) {
357                         return false;
358                 }
359
360                 // @todo Maybe a bit unsafe cast?
361                 final BranchOffice branchOffice = (BranchOffice) object;
362
363                 if (!Objects.equals(this.getBranchId(), branchOffice.getBranchId())) {
364                         return false;
365                 } else if (!Objects.equals(this.getBranchCity(), branchOffice.getBranchCity())) {
366                         return false;
367                 } else if (!Objects.equals(this.getBranchCountry(), branchOffice.getBranchCountry())) {
368                         return false;
369                 } else if (!Objects.equals(this.getBranchHouseNumber(), branchOffice.getBranchHouseNumber())) {
370                         return false;
371                 } else if (!Objects.equals(this.getBranchHouseNumberExtension(), branchOffice.getBranchHouseNumberExtension())) {
372                         return false;
373                 } else if (!Objects.equals(this.getBranchLastHouseNumber(), branchOffice.getBranchLastHouseNumber())) {
374                         return false;
375                 } else if (!Objects.equals(this.getBranchNumber(), branchOffice.getBranchNumber())) {
376                         return false;
377                 } else if (!Objects.equals(this.getBranchStore(), branchOffice.getBranchStore())) {
378                         return false;
379                 } else if (!Objects.equals(this.getBranchStreet(), branchOffice.getBranchStreet())) {
380                         return false;
381                 } else if (!Objects.equals(this.getBranchSuiteNumber(), branchOffice.getBranchSuiteNumber())) {
382                         return false;
383                 } else if (!Objects.equals(this.getBranchZipCode(), branchOffice.getBranchZipCode())) {
384                         return false;
385                 }
386
387                 return true;
388         }
389
390         @Override
391         public String getBranchCity () {
392                 return this.branchCity;
393         }
394
395         @Override
396         public void setBranchCity (final String branchCity) {
397                 this.branchCity = branchCity;
398         }
399
400         @Override
401         public BasicData getBranchCompany () {
402                 return this.branchCompany;
403         }
404
405         @Override
406         public void setBranchCompany (final BasicData branchCompany) {
407                 this.branchCompany = branchCompany;
408         }
409
410         @Override
411         public Employable getBranchContactEmployee () {
412                 return this.branchContactEmployee;
413         }
414
415         @Override
416         public void setBranchContactEmployee (final Employable branchContactEmployee) {
417                 this.branchContactEmployee = branchContactEmployee;
418         }
419
420         @Override
421         public Country getBranchCountry () {
422                 return this.branchCountry;
423         }
424
425         @Override
426         public void setBranchCountry (final Country branchCountry) {
427                 this.branchCountry = branchCountry;
428         }
429
430         @Override
431         @SuppressWarnings ("ReturnOfDateField")
432         public Date getBranchCreated () {
433                 return this.branchCreated;
434         }
435
436         @Override
437         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
438         public void setBranchCreated (final Date branchCreated) {
439                 this.branchCreated = branchCreated;
440         }
441
442         @Override
443         public String getBranchEmailAddress () {
444                 return this.branchEmailAddress;
445         }
446
447         @Override
448         public void setBranchEmailAddress (final String branchEmailAddress) {
449                 this.branchEmailAddress = branchEmailAddress;
450         }
451
452         @Override
453         public DialableFaxNumber getBranchFaxNumber () {
454                 return this.branchFaxNumber;
455         }
456
457         @Override
458         public void setBranchFaxNumber (final DialableFaxNumber branchFaxNumber) {
459                 this.branchFaxNumber = branchFaxNumber;
460         }
461
462         @Override
463         public Short getBranchHouseNumber () {
464                 return this.branchHouseNumber;
465         }
466
467         @Override
468         public void setBranchHouseNumber (final Short branchHouseNumber) {
469                 this.branchHouseNumber = branchHouseNumber;
470         }
471
472         @Override
473         public String getBranchHouseNumberExtension () {
474                 return this.branchHouseNumberExtension;
475         }
476
477         @Override
478         public void setBranchHouseNumberExtension (final String branchHouseNumberExtension) {
479                 this.branchHouseNumberExtension = branchHouseNumberExtension;
480         }
481
482         @Override
483         public Long getBranchId () {
484                 return this.branchId;
485         }
486
487         @Override
488         public void setBranchId (final Long branchId) {
489                 this.branchId = branchId;
490         }
491
492         @Override
493         public DialableLandLineNumber getBranchLandLineNumber () {
494                 return this.branchLandLineNumber;
495         }
496
497         @Override
498         public void setBranchLandLineNumber (final DialableLandLineNumber branchLandLineNumber) {
499                 this.branchLandLineNumber = branchLandLineNumber;
500         }
501
502         @Override
503         public Short getBranchLastHouseNumber () {
504                 return this.branchLastHouseNumber;
505         }
506
507         @Override
508         public void setBranchLastHouseNumber (final Short branchLastHouseNumber) {
509                 this.branchLastHouseNumber = branchLastHouseNumber;
510         }
511
512         @Override
513         public Long getBranchNumber () {
514                 return this.branchNumber;
515         }
516
517         @Override
518         public void setBranchNumber (final Long branchNumber) {
519                 this.branchNumber = branchNumber;
520         }
521
522         @Override
523         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
524         public List<OpeningTime> getBranchOpeningTimes () {
525                 return this.branchOpeningTimes;
526         }
527
528         @Override
529         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
530         public void setBranchOpeningTimes (final List<OpeningTime> branchOpeningTimes) {
531                 this.branchOpeningTimes = branchOpeningTimes;
532         }
533
534         @Override
535         public Employable getBranchOwnerEmployee () {
536                 return this.branchOwnerEmployee;
537         }
538
539         @Override
540         public void setBranchOwnerEmployee (final Employable branchOwnerEmployee) {
541                 this.branchOwnerEmployee = branchOwnerEmployee;
542         }
543
544         @Override
545         public Short getBranchStore () {
546                 return this.branchStore;
547         }
548
549         @Override
550         public void setBranchStore (final Short branchStore) {
551                 this.branchStore = branchStore;
552         }
553
554         @Override
555         public String getBranchStreet () {
556                 return this.branchStreet;
557         }
558
559         @Override
560         public void setBranchStreet (final String branchStreet) {
561                 this.branchStreet = branchStreet;
562         }
563
564         @Override
565         public Short getBranchSuiteNumber () {
566                 return this.branchSuiteNumber;
567         }
568
569         @Override
570         public void setBranchSuiteNumber (final Short branchSuiteNumber) {
571                 this.branchSuiteNumber = branchSuiteNumber;
572         }
573
574         @Override
575         public User getBranchUserOwner () {
576                 return this.branchUserOwner;
577         }
578
579         @Override
580         public void setBranchUserOwner (final User branchUserOwner) {
581                 this.branchUserOwner = branchUserOwner;
582         }
583
584         @Override
585         public Integer getBranchZipCode () {
586                 return this.branchZipCode;
587         }
588
589         @Override
590         public void setBranchZipCode (final Integer branchZipCode) {
591                 this.branchZipCode = branchZipCode;
592         }
593
594         @Override
595         public int hashCode () {
596                 int hash = 7;
597
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());
609
610                 return hash;
611         }
612
613 }