From cfd8a4fe5b768e9f5712211066fc358d10d0c1eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 1 Nov 2017 22:35:35 +0100 Subject: [PATCH] Please cherry-pick: - added EJBs for headquarters entity, general and administrative - added method for updating all "phone created" timestamps in above entity - renamed entity class name in persistence.xml MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- src/conf/persistence.xml | 2 +- .../database/BaseAddressbookDatabaseBean.java | 87 +++++++--- ...AddressbookAdminDepartmentSessionBean.java | 6 +- ...dressbookAdminHeadquartersSessionBean.java | 153 ++++++++++++++++++ .../AddressbookHeadquartersSessionBean.java | 57 +++++++ 5 files changed, 281 insertions(+), 24 deletions(-) create mode 100644 src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookAdminHeadquartersSessionBean.java create mode 100644 src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookHeadquartersSessionBean.java diff --git a/src/conf/persistence.xml b/src/conf/persistence.xml index a4c0ea1..3243071 100644 --- a/src/conf/persistence.xml +++ b/src/conf/persistence.xml @@ -10,7 +10,7 @@ org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice org.mxchange.jcontactsbusiness.model.department.BusinessDepartment org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee - org.mxchange.jcontactsbusiness.model.headquarters.BusinessHeadquartersData + org.mxchange.jcontactsbusiness.model.headquarters.BusinessHeadquarters org.mxchange.jcontactsbusiness.model.jobposition.EmployeePosition org.mxchange.jcontactsbusiness.model.logo.BusinessLogo org.mxchange.jcontactsbusiness.model.opening_time.BusinessOpeningTime diff --git a/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java index bf329a7..fe45cf3 100644 --- a/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java +++ b/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java @@ -30,12 +30,14 @@ import javax.mail.internet.InternetAddress; import org.mxchange.jcontacts.model.contact.Contact; import org.mxchange.jcontacts.model.contact.ContactUtils; import org.mxchange.jcontacts.model.contact.UserContact; +import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData; import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice; import org.mxchange.jcontactsbusiness.model.branchoffice.BusinessBranchOffice; import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee; -import org.mxchange.jcontactsbusiness.model.headquarters.BusinessHeadquartersData; -import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData; +import org.mxchange.jcontactsbusiness.model.employee.Employable; +import org.mxchange.jcontactsbusiness.model.headquarters.BusinessHeadquarters; +import org.mxchange.jcontactsbusiness.model.headquarters.Headquarter; import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jcountry.model.data.Country; @@ -52,8 +54,6 @@ import org.mxchange.jphone.utils.PhoneUtils; import org.mxchange.jusercore.model.user.LoginUser; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.UserUtils; -import org.mxchange.jcontactsbusiness.model.employee.Employable; -import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; /** * A helper class for beans that access the database. @@ -404,28 +404,28 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { /** * Get back a managed instance from given branch office *

- * @param branchOffice Unmanaged/detached branch office instance + * @param headquarter Unmanaged/detached branch office instance *

* @return Managed basic data instance */ - protected BranchOffice createManaged (final BranchOffice branchOffice) { + protected BranchOffice createManaged (final BranchOffice headquarter) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N // user should not be null - if (null == branchOffice) { + if (null == headquarter) { // Abort here - throw new NullPointerException("branchOffice is null"); //NOI18N - } else if (branchOffice.getBranchId() == null) { + throw new NullPointerException("headquarter is null"); //NOI18N + } else if (headquarter.getBranchId() == null) { // Id is set - throw new NullPointerException("branchOffice.branchOfficeId is null"); //NOI18N - } else if (branchOffice.getBranchId() < 1) { + throw new NullPointerException("headquarter.headquarterId is null"); //NOI18N + } else if (headquarter.getBranchId() < 1) { // Id is set - throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchOfficeId={0} is invalid", branchOffice.getBranchId())); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("headquarter.headquarterId={0} is invalid", headquarter.getBranchId())); //NOI18N } // Try to find it (should be there) - final BranchOffice managedBranchOffice = this.getEntityManager().find(BusinessBranchOffice.class, branchOffice.getBranchId()); + final BranchOffice managedBranchOffice = this.getEntityManager().find(BusinessBranchOffice.class, headquarter.getBranchId()); // Should be there assert (managedBranchOffice instanceof BasicData) : "managedBranchOffice is null"; //NOI18N @@ -480,24 +480,24 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { *

* @return Managed basic data instance */ - protected HeadquartersData createManaged (final HeadquartersData headquarters) { + protected Headquarter createManaged (final Headquarter headquarters) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: branchOffice={1} - CALLED!", this.getClass().getSimpleName(), headquarters)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarters)); //NOI18N // user should not be null if (null == headquarters) { // Abort here - throw new NullPointerException("branchOffice is null"); //NOI18N + throw new NullPointerException("headquarter is null"); //NOI18N } else if (headquarters.getHeadquartersId() == null) { // Id is set - throw new NullPointerException("branchOffice.headquartersId is null"); //NOI18N + throw new NullPointerException("headquarter.headquartersId is null"); //NOI18N } else if (headquarters.getHeadquartersId() < 1) { // Id is set - throw new IllegalArgumentException(MessageFormat.format("branchOffice.headquartersId={0} is invalid", headquarters.getHeadquartersId())); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("headquarter.headquartersId={0} is invalid", headquarters.getHeadquartersId())); //NOI18N } // Try to find it (should be there) - final HeadquartersData managedHeadquarters = this.getEntityManager().find(BusinessHeadquartersData.class, headquarters.getHeadquartersId()); + final Headquarter managedHeadquarters = this.getEntityManager().find(BusinessHeadquarters.class, headquarters.getHeadquartersId()); // Should be there assert (managedHeadquarters instanceof BasicData) : "managedHeadquarters is null"; //NOI18N @@ -690,6 +690,53 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N } + /** + * Updates all branch office's phone entry's created timestamps + *

+ * @param headquarter Headquarters instance to update + */ + protected void setAllPhoneEntriesCreated (final Headquarter headquarter) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllPhoneEntriesCreated: headquarter={0} - CALLED!", headquarter)); //NOI18N + + // The contact instance must be valid + if (null == headquarter) { + // Throw NPE again + throw new NullPointerException("headquarter is null"); //NOI18N + } else if (headquarter.getHeadquartersId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("headquarter.branchId={0} is unexpected.", headquarter.getHeadquartersId())); + } + + // Get all phone instances + final DialableLandLineNumber landLineNumber = headquarter.getHeadquartersLandLineNumber(); + final DialableFaxNumber faxNumber = headquarter.getHeadquartersFaxNumber(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllPhoneEntriesCreated: landLineNumber={0},faxNumber={1}", landLineNumber, faxNumber)); //NOI18N + + // Is a phone number instance set? + if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N + + // Set updated timestamp + landLineNumber.setPhoneEntryCreated(new Date()); + } + + // Is a fax number instance set? + if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) { + // Debug message + this.getLoggerBeanLocal().logDebug("setAllPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N + + // Set updated timestamp + faxNumber.setPhoneEntryCreated(new Date()); + } + + // Trace message + this.getLoggerBeanLocal().logTrace("setAllPhoneEntriesCreated: EXIT!"); //NOI18N + } + /** * Updates all company's phone entry's created timestamps *

diff --git a/src/java/org/mxchange/jcontactsbusiness/model/department/AddressbookAdminDepartmentSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/model/department/AddressbookAdminDepartmentSessionBean.java index 34a2ca0..9d96fdd 100644 --- a/src/java/org/mxchange/jcontactsbusiness/model/department/AddressbookAdminDepartmentSessionBean.java +++ b/src/java/org/mxchange/jcontactsbusiness/model/department/AddressbookAdminDepartmentSessionBean.java @@ -23,11 +23,11 @@ import javax.ejb.EJB; import javax.ejb.Stateless; import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException; import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice; -import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData; import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; import org.mxchange.jusercore.model.user.User; import org.mxchange.jcontactsbusiness.model.employee.Employable; import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; +import org.mxchange.jcontactsbusiness.model.headquarters.Headquarter; /** * A stateless session bean for administrative department purposes @@ -107,9 +107,9 @@ public class AddressbookAdminDepartmentSessionBean extends BaseAddressbookDataba } // Is headquarters set? - if (department.getDepartmentHeadquarters() instanceof HeadquartersData) { + if (department.getDepartmentHeadquarters() instanceof Headquarter) { // Get managed headquarters - final HeadquartersData managedHeadquarters = this.createManaged(department.getDepartmentHeadquarters()); + final Headquarter managedHeadquarters = this.createManaged(department.getDepartmentHeadquarters()); // Set it back department.setDepartmentHeadquarters(managedHeadquarters); diff --git a/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookAdminHeadquartersSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookAdminHeadquartersSessionBean.java new file mode 100644 index 0000000..b2e0d8f --- /dev/null +++ b/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookAdminHeadquartersSessionBean.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness.model.headquarters; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jcontactsbusiness.exceptions.headquarters.HeadquartersAlreadyAddedException; +import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; +import org.mxchange.jusercore.model.user.User; + +/** + * A stateless session bean for administrative branch office purposes + *

+ * @author Roland Häder + */ +@Stateless (name = "adminHeadquarter", description = "An administrative statless bean for handling branch office data (all)") +public class AddressbookAdminHeadquartersSessionBean extends BaseAddressbookDatabaseBean implements AdminHeadquartersSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_467_386_571_701L; + + /** + * General branch office bean + */ + @EJB (lookup = "java:global/addressbook-ejb/headquarters!org.mxchange.jcontactsbusiness.model.headquarters.HeadquarterSessionBeanRemote") + private HeadquartersSessionBeanRemote headquartersBean; + + /** + * Default constructor + */ + public AddressbookAdminHeadquartersSessionBean () { + // Call super constructor + super(); + } + + @Override + public Headquarter addHeadquarters (final Headquarter headquarter) throws HeadquartersAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarters(): headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N + + // Validate parameter + if (null == headquarter) { + // Throw NPE + throw new NullPointerException("headquarter is null"); //NOI18N + } else if (headquarter.getHeadquartersId() instanceof Long) { + // Should not happen + throw new IllegalArgumentException("headquarter.branchId should not be set."); //NOI18N + } else if (this.isHeadquarterFound(headquarter)) { + // Already added, abort here + throw new HeadquartersAlreadyAddedException(headquarter); + } + + // Add created timestamp + headquarter.setHeadquartersCreated(new Date()); + + // Is user instance set? + if (headquarter.getHeadquartersUserOwner() instanceof User) { + // Get managed instance back + final User managedUser = this.createManaged(headquarter.getHeadquartersUserOwner()); + + // Set it back in branch office + headquarter.setHeadquartersUserOwner(managedUser); + } + + // Is user instance set? + if (headquarter.getHeadquartersCountry() instanceof Country) { + // Get managed instance back + final Country managedCountry = this.createManaged(headquarter.getHeadquartersCountry()); + + // Set it back in branch office + headquarter.setHeadquartersCountry(managedCountry); + } + + // Set "created" timestamp on any number assigned + this.setAllPhoneEntriesCreated(headquarter); + + // Get opening times + final List openingTimes = headquarter.getHeadquartersOpeningTimes(); + + // Debugging: + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addHeadquarters(): headquarter.headquarterOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); + + // Is opening times set and not empty? + if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) { + // Add created timestamp for all times + this.setAllOpeningTimesCreated(openingTimes); + } else { + // Set all to null + headquarter.setHeadquartersOpeningTimes(null); + } + + // Persist it + this.getEntityManager().persist(headquarter); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarters(): headquarter.branchId={1} - EXIT!", this.getClass().getSimpleName(), headquarter.getHeadquartersId())); //NOI18N + + // Return updated instance + return headquarter; + } + + /** + * Checks if given branch office's address is already persisted. The whole + * (persisted) list is being loaded and each address is being matched + * against the given branch office's address. + *

+ * @param headquarter Headquarters office being checked + *

+ * @return Whether it has been found + */ + private boolean isHeadquarterFound (final Headquarter headquarter) { + // Get whole list + final List headquarters = this.headquartersBean.allHeadquarters(); + + // Default is not found + boolean isFound = false; + + // Check all single addresses + for (final Headquarter hq : headquarters) { + // Is the same address found? + if (Headquarters.isSameAddress(hq, headquarter)) { + // Found one + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + +} diff --git a/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookHeadquartersSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookHeadquartersSessionBean.java new file mode 100644 index 0000000..967a722 --- /dev/null +++ b/src/java/org/mxchange/jcontactsbusiness/model/headquarters/AddressbookHeadquartersSessionBean.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness.model.headquarters; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; + +/** + * A stateless session bean for general headquarters purposes + *

+ * @author Roland Häder + */ +@Stateless (name = "headquarters", description = "A general statless bean for handling headquarters data (all)") +public class AddressbookHeadquartersSessionBean extends BaseAddressbookDatabaseBean implements HeadquartersSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_467_386_571_701L; + + @Override + @SuppressWarnings ("unchecked") + public List allHeadquarters () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allHeadquarters: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + final Query query = this.getEntityManager().createNamedQuery("AllHeadquarters"); //NOI18N + + // Get list from it + final List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allHeadquarters: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + +} -- 2.39.5