From: Roland Häder <roland@mxchange.org>
Date: Sat, 26 Aug 2017 22:02:17 +0000 (+0200)
Subject: Please cherry-pick/rename:
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=712c4a3b877db33586d465f32de09489038aac40;p=pizzaservice-ejb.git

Please cherry-pick/rename:
- added admin/general company employee session bean and implemented all methods
- implemented isCompanyNameUsed() and used dependency injection for injecting
  other EJB

Signed-off-by: Roland Häder <roland@mxchange.org>
---

diff --git a/src/conf/persistence.xml b/src/conf/persistence.xml
index afe5d2c..1f690d0 100644
--- a/src/conf/persistence.xml
+++ b/src/conf/persistence.xml
@@ -7,7 +7,7 @@
     <class>org.mxchange.jcontactsbusiness.branch.CompanyBranchOffice</class>
     <class>org.mxchange.jcontactsbusiness.department.CompanyDepartment</class>
     <class>org.mxchange.jcontactsbusiness.employee.CompanyEmployee</class>
-    <class>org.mxchange.jcontactsbusiness.headquarters.CompanyHeadQuartersData</class>
+    <class>org.mxchange.jcontactsbusiness.headquarters.CompanyHeadquartersData</class>
     <class>org.mxchange.jcontactsbusiness.jobposition.EmployeePosition</class>
     <class>org.mxchange.jcontactsbusiness.logo.CompanyLogo</class>
     <class>org.mxchange.jcontactsbusiness.opening_times.BusinessOpeningTimes</class>
diff --git a/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaAdminBusinessDataSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaAdminBusinessDataSessionBean.java
index ae72875..9975e38 100644
--- a/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaAdminBusinessDataSessionBean.java
+++ b/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaAdminBusinessDataSessionBean.java
@@ -16,13 +16,14 @@
  */
 package org.mxchange.jcontactsbusiness.basicdata;
 
-import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData;
 import java.text.MessageFormat;
+import java.util.GregorianCalendar;
 import java.util.List;
+import java.util.Objects;
 import javax.ejb.Stateless;
 import javax.persistence.Query;
+import org.mxchange.jcontactsbusiness.exceptions.basicdata.BusinessDataAlreadyAddedException;
 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
-import org.mxchange.jcontactsbusiness.basicdata.AdminBusinessDataSessionBeanRemote;
 
 /**
  * An administrative stateless session bean for business data
@@ -45,9 +46,54 @@ public class PizzaAdminBusinessDataSessionBean extends BasePizzaDatabaseBean imp
 		super();
 	}
 
+	@Override
+	public BusinessBasicData addCompanyBasicData (final BusinessBasicData basicData) throws BusinessDataAlreadyAddedException {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
+
+		// Is the instance set?
+		if (null == basicData) {
+			// Throw NPE
+			throw new NullPointerException("basicData is null"); //NOI18N
+		} else if (basicData.getCompanyDataId() != null) {
+			// Should be null
+			throw new IllegalArgumentException(MessageFormat.format("basicData.companyDataId={0} - is not null", basicData.getCompanyDataId())); //NOI18N
+		}
+
+		// Get all available entries
+		List<BusinessBasicData> list = this.allCompanyBasicData();
+
+		// Is the list filled?
+		if (!list.isEmpty()) {
+			// Then check each entry
+			for (final BusinessBasicData entry : list) {
+				// Is the company name matching?
+				if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {
+					// Found match
+					throw new BusinessDataAlreadyAddedException(basicData);
+				}
+			}
+		}
+
+		// Now add current date
+		basicData.setCompanyCreated(new GregorianCalendar());
+
+		// Persist it
+		this.getEntityManager().persist(basicData);
+
+		// Flush it (bad performance!)
+		this.getEntityManager().flush();
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.companyDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getCompanyDataId())); //NOI18N
+
+		// Return updated instance
+		return basicData;
+	}
+
 	@Override
 	@SuppressWarnings ("unchecked")
-	public List<BusinessBasicData> allBusinessContacts () {
+	public List<BusinessBasicData> allCompanyBasicData () {
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N
 
diff --git a/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaBusinessDataSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaBusinessDataSessionBean.java
index 74e177f..d6ab8f0 100644
--- a/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaBusinessDataSessionBean.java
+++ b/src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaBusinessDataSessionBean.java
@@ -17,6 +17,9 @@
 package org.mxchange.jcontactsbusiness.basicdata;
 
 import java.text.MessageFormat;
+import java.util.List;
+import java.util.Objects;
+import javax.ejb.EJB;
 import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
@@ -36,6 +39,12 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
 	 */
 	private static final long serialVersionUID = 56_389_504_892_184_571L;
 
+	/**
+	 * Administrative EJB
+	 */
+	@EJB
+	private AdminBusinessDataSessionBeanRemote adminBusinessDataBean;
+
 	/**
 	 * Default constructor
 	 */
@@ -45,7 +54,7 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
 	}
 
 	@Override
-	public BusinessBasicData findBusinessDataById (final Long businessDataId) throws BusinessDataNotFoundException {
+	public BusinessBasicData findBasicDataById (final Long companyDataId) throws BusinessDataNotFoundException {
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBusinessDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
 
@@ -53,7 +62,7 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
 		Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
 
 		// Set parameter
-		query.setParameter("businessDataId", businessDataId); //NOI18N
+		query.setParameter("companyDataId", companyDataId); //NOI18N
 
 		// Get single instance
 		BusinessBasicData businessData = null;
@@ -67,7 +76,7 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
 			this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBusinessDataById: Found contact={1}", this.getClass().getSimpleName(), businessData)); //NOI18N
 		} catch (final NoResultException ex) {
 			// No result found
-			throw new BusinessDataNotFoundException(businessDataId, ex);
+			throw new BusinessDataNotFoundException(companyDataId, ex);
 		}
 
 		// Trace message
@@ -77,4 +86,43 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
 		return businessData;
 	}
 
+	@Override
+	public Boolean isCompanyNameUsed (final String companyName) {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: companyName={1} - CALLED!", this.getClass().getSimpleName(), companyName)); //NOI18N
+
+		// Is the parameter valid?
+		if (null == companyName) {
+			// Throw NPE
+			throw new NullPointerException("companyName is null"); //NOI18N
+		} else if (companyName.isEmpty()) {
+			// Throw IAE
+			throw new IllegalArgumentException("companyName is empty"); //NOI18N
+		}
+
+		// Default is not found
+		Boolean isCompanyNameUsed = Boolean.FALSE;
+
+		// Get whole list
+		List<BusinessBasicData> list = this.adminBusinessDataBean.allCompanyBasicData();
+
+		// Check whole list
+		for (final BusinessBasicData basicData : list) {
+			// Is name used?
+			if (Objects.equals(basicData.getCompanyName(), companyName)) {
+				// Found one
+				isCompanyNameUsed = Boolean.TRUE;
+
+				// Abort search
+				break;
+			}
+		}
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: isCompanyNameUsed={1} - EXIT!", this.getClass().getSimpleName(), isCompanyNameUsed)); //NOI18N
+
+		// Return it
+		return isCompanyNameUsed;
+	}
+
 }
diff --git a/src/java/org/mxchange/jcontactsbusiness/employee/PizzaAdminCompanyEmployeeSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/employee/PizzaAdminCompanyEmployeeSessionBean.java
new file mode 100644
index 0000000..aa4a95c
--- /dev/null
+++ b/src/java/org/mxchange/jcontactsbusiness/employee/PizzaAdminCompanyEmployeeSessionBean.java
@@ -0,0 +1,42 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcontactsbusiness.employee;
+
+import javax.ejb.Stateless;
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
+
+/**
+ * A stateless bean for administrative purposes for company employees.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Stateless (name = "adminCompanyEmployee", description = "An administrative statless bean for handling company employees")
+public class PizzaAdminCompanyEmployeeSessionBean extends BasePizzaDatabaseBean implements AdminCompanyEmployeeSessionBeanRemote {
+
+	/**
+	 * Serial number
+	 */
+	private static final long serialVersionUID = 26_458_796_703_761L;
+
+	/**
+	 * Default constructor
+	 */
+	public PizzaAdminCompanyEmployeeSessionBean () {
+		super();
+	}
+
+}
diff --git a/src/java/org/mxchange/jcontactsbusiness/employee/PizzaCompanyEmployeeSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/employee/PizzaCompanyEmployeeSessionBean.java
new file mode 100644
index 0000000..2d32bc8
--- /dev/null
+++ b/src/java/org/mxchange/jcontactsbusiness/employee/PizzaCompanyEmployeeSessionBean.java
@@ -0,0 +1,108 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcontactsbusiness.employee;
+
+import java.text.MessageFormat;
+import java.util.List;
+import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
+import javax.persistence.Query;
+import org.mxchange.jcontactsbusiness.exceptions.employee.CompanyEmployeeNotFoundException;
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
+
+/**
+ * A stateless bean for general purposes for company employees.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Stateless (name = "companyEmployee", description = "A general statless bean for handling company employees")
+public class PizzaCompanyEmployeeSessionBean extends BasePizzaDatabaseBean implements CompanyEmployeeSessionBeanRemote {
+
+	/**
+	 * Serial number
+	 */
+	private static final long serialVersionUID = 26_458_796_703_761L;
+
+	/**
+	 * Default constructor
+	 */
+	public PizzaCompanyEmployeeSessionBean () {
+		super();
+	}
+
+	@Override
+	@SuppressWarnings ("unchecked")
+	public List<Employee> allCompanyEmployees () {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCompanyEmployees(): CALLED!", this.getClass().getSimpleName())); //NOI18N
+
+		// Get named query
+		Query query = this.getEntityManager().createNamedQuery("AllCompanyEmployees"); //NOI18N
+
+		// Get list form it
+		List<Employee> employees = query.getResultList();
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCompanyEmployees(): employees.size()={1} - EXIT!", this.getClass().getSimpleName(), employees.size())); //NOI18N
+
+		// Return it
+		return employees;
+	}
+
+	@Override
+	public Employee findCompanyEmployeeById (final Long employeeId) throws CompanyEmployeeNotFoundException {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById(): employeeId={1} - CALLED!", this.getClass().getSimpleName(), employeeId)); //NOI18N
+
+		// Is the employee id valid?
+		if (null == employeeId) {
+			// Throw NPE
+			throw new NullPointerException("employeeId is null"); //NOI18N
+		} else if (employeeId < 1) {
+			// Not valid
+			throw new IllegalArgumentException(MessageFormat.format("employeeId={0} is not valid", employeeId)); //NOI18N
+		}
+
+		// Now get named query
+		Query query = this.getEntityManager().createNamedQuery("SearchCompanyEmployeeById"); //NOI18N
+
+		// Set parameter
+		query.setParameter("employeeId", employeeId); //NOI18N
+
+		// Declare instance
+		Employee employee;
+
+		// Try to find a result
+		try {
+			// Find a single result
+			employee = (Employee) query.getSingleResult();
+
+			// Log trace message
+			this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById: Found employee={1}", this.getClass().getSimpleName(), employee)); //NOI18N
+		} catch (final NoResultException ex) {
+			// No result found
+			throw new CompanyEmployeeNotFoundException(employeeId, ex);
+		}
+
+		// Log trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById: employee={1} - EXIT!", this.getClass().getSimpleName(), employee)); //NOI18N
+
+		// Return found instance
+		return employee;
+	}
+
+}