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