]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/headquarter/JobsAdminHeadquarterSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / headquarter / JobsAdminHeadquarterSessionBean.java
1 /*
2  * Copyright (C) 2017 - 2024 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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcontactsbusiness.model.headquarter;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.model.contact.Contact;
25 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
27 import org.mxchange.jcontactsbusiness.model.utils.HeadquarterUtils;
28 import org.mxchange.jcountry.model.data.Country;
29 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
30 import org.mxchange.jusercore.model.user.User;
31
32 /**
33  * A stateless session bean for administrative branch office purposes
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "adminHeadquarter", description = "An administrative statless bean for handling branch office data (all)")
38 public class JobsAdminHeadquarterSessionBean extends BaseJobsEnterpriseBean implements AdminHeadquarterSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 58_467_386_571_706L;
44
45         /**
46          * General branch office bean
47          */
48         @EJB (lookup = "java:global/jjobs-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
49         private HeadquarterSessionBeanRemote headquarterBean;
50
51         /**
52          * Default constructor
53          */
54         public JobsAdminHeadquarterSessionBean () {
55                 // Call super constructor
56                 super();
57         }
58
59         @Override
60         public Headquarter addHeadquarter (final Headquarter headquarter) throws HeadquarterAlreadyAddedException {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarter(): headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N
63
64                 // Validate parameter
65                 if (null == headquarter) {
66                         // Throw NPE
67                         throw new NullPointerException("headquarter is null"); //NOI18N
68                 } else if (headquarter.getHeadquarterId() instanceof Long) {
69                         // Should not happen
70                         throw new IllegalArgumentException("headquarter.branchId should not be set."); //NOI18N
71                 } else if (this.isHeadquarterFound(headquarter)) {
72                         // Already added, abort here
73                         throw new HeadquarterAlreadyAddedException(headquarter);
74                 }
75
76                 // Add created timestamp
77                 headquarter.setHeadquarterEntryCreated(new Date());
78
79                 // Is contact employee set?
80                 if (headquarter.getHeadquarterContactEmployee() instanceof Contact) {
81                         // Get managed lead contact
82                         final Contact managedContact = this.createManaged(headquarter.getHeadquarterContactEmployee());
83
84                         // Set it back
85                         headquarter.setHeadquarterContactEmployee(managedContact);
86                 }
87
88                 // Is user instance set?
89                 if (headquarter.getHeadquarterUserOwner() instanceof User) {
90                         // Get managed instance back
91                         final User managedUser = this.createManaged(headquarter.getHeadquarterUserOwner());
92
93                         // Set it back in branch office
94                         headquarter.setHeadquarterUserOwner(managedUser);
95                 }
96
97                 // Is user instance set?
98                 if (headquarter.getHeadquarterCountry() instanceof Country) {
99                         // Get managed instance back
100                         final Country managedCountry = this.createManaged(headquarter.getHeadquarterCountry());
101
102                         // Set it back in branch office
103                         headquarter.setHeadquarterCountry(managedCountry);
104                 }
105
106                 // Set "created" timestamp on any number assigned
107                 this.setAllPhoneEntriesCreated(headquarter);
108
109                 // Get opening times
110                 final List<OpeningTime> openingTimes = headquarter.getHeadquarterOpeningTimes();
111
112                 // Debugging:
113                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addHeadquarter(): headquarter.headquarterOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes));
114
115                 // Is opening times set and not empty?
116                 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
117                         // Add created timestamp for all times
118                         this.setAllOpeningTimesCreated(openingTimes);
119                 } else {
120                         // Set all to null
121                         headquarter.setHeadquarterOpeningTimes(null);
122                 }
123
124                 // Persist it
125                 this.getEntityManager().persist(headquarter);
126
127                 // Trace message
128                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarter(): headquarter.branchId={1} - EXIT!", this.getClass().getSimpleName(), headquarter.getHeadquarterId())); //NOI18N
129
130                 // Return updated instance
131                 return headquarter;
132         }
133
134         /**
135          * Checks if given branch office's address is already persisted. The whole
136          * (persisted) list is being loaded and each address is being matched
137          * against the given branch office's address.
138          * <p>
139          * @param headquarter Headquarter office being checked
140          * <p>
141          * @return Whether it has been found
142          */
143         private boolean isHeadquarterFound (final Headquarter headquarter) {
144                 // Get whole list
145                 final List<Headquarter> headquarters = this.headquarterBean.fetchAllHeadquarters();
146
147                 // Default is not found
148                 boolean isFound = false;
149
150                 // Check all single addresses
151                 for (final Headquarter hq : headquarters) {
152                         // Is the same address found?
153                         if (HeadquarterUtils.isSameAddress(hq, headquarter)) {
154                                 // Found one
155                                 isFound = true;
156                                 break;
157                         }
158                 }
159
160                 // Return flag
161                 return isFound;
162         }
163
164 }