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