From f59c329266508f23dab21a6f03e6f03f424bc7cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 3 Nov 2017 22:41:05 +0100 Subject: [PATCH] Continued: - added exception for already added employees - fixed serial numbers, must be globally unique - sorted imports - added utilities class for employees MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../DepartmentNotFoundException.java | 2 +- .../EmployeeAlreadyAddedException.java | 38 ++++ .../HeadquarterAlreadyAddedException.java | 2 +- .../OpeningTimeNotFoundException.java | 2 +- .../model/employee/BusinessEmployee.java | 23 ++- .../model/employee/Employable.java | 4 +- .../model/employee/Employees.java | 163 ++++++++++++++++++ 7 files changed, 221 insertions(+), 13 deletions(-) create mode 100644 src/org/mxchange/jcontactsbusiness/exceptions/employee/EmployeeAlreadyAddedException.java create mode 100644 src/org/mxchange/jcontactsbusiness/model/employee/Employees.java diff --git a/src/org/mxchange/jcontactsbusiness/exceptions/department/DepartmentNotFoundException.java b/src/org/mxchange/jcontactsbusiness/exceptions/department/DepartmentNotFoundException.java index 81e7a97..91b1ad8 100644 --- a/src/org/mxchange/jcontactsbusiness/exceptions/department/DepartmentNotFoundException.java +++ b/src/org/mxchange/jcontactsbusiness/exceptions/department/DepartmentNotFoundException.java @@ -28,7 +28,7 @@ public class DepartmentNotFoundException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 23_759_801_876_416_573L; + private static final long serialVersionUID = 23_759_801_876_416_574L; /** * Constructor with company department id diff --git a/src/org/mxchange/jcontactsbusiness/exceptions/employee/EmployeeAlreadyAddedException.java b/src/org/mxchange/jcontactsbusiness/exceptions/employee/EmployeeAlreadyAddedException.java new file mode 100644 index 0000000..441a9e8 --- /dev/null +++ b/src/org/mxchange/jcontactsbusiness/exceptions/employee/EmployeeAlreadyAddedException.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness.exceptions.employee; + +/** + * An exception thrown when a employee (entity) has not found. + *

+ * @author Roland Häder + */ +public class EmployeeAlreadyAddedException extends Exception { + + /** + * Serial number + */ + private static final long serialVersionUID = 23_759_801_876_416_573L; + + /** + * Default constructor + */ + public EmployeeAlreadyAddedException () { + super(); + } + +} diff --git a/src/org/mxchange/jcontactsbusiness/exceptions/headquarter/HeadquarterAlreadyAddedException.java b/src/org/mxchange/jcontactsbusiness/exceptions/headquarter/HeadquarterAlreadyAddedException.java index 41629c1..78c90b0 100644 --- a/src/org/mxchange/jcontactsbusiness/exceptions/headquarter/HeadquarterAlreadyAddedException.java +++ b/src/org/mxchange/jcontactsbusiness/exceptions/headquarter/HeadquarterAlreadyAddedException.java @@ -29,7 +29,7 @@ public class HeadquarterAlreadyAddedException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 23_759_801_876_416_573L; + private static final long serialVersionUID = 23_759_801_876_416_575L; /** * Constructor with a headquarter instance diff --git a/src/org/mxchange/jcontactsbusiness/exceptions/opening_time/OpeningTimeNotFoundException.java b/src/org/mxchange/jcontactsbusiness/exceptions/opening_time/OpeningTimeNotFoundException.java index 7fc610b..4471aec 100644 --- a/src/org/mxchange/jcontactsbusiness/exceptions/opening_time/OpeningTimeNotFoundException.java +++ b/src/org/mxchange/jcontactsbusiness/exceptions/opening_time/OpeningTimeNotFoundException.java @@ -28,7 +28,7 @@ public class OpeningTimeNotFoundException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 23_759_801_876_416_573L; + private static final long serialVersionUID = 23_759_801_876_416_576L; /** * Constructor with opening time id diff --git a/src/org/mxchange/jcontactsbusiness/model/employee/BusinessEmployee.java b/src/org/mxchange/jcontactsbusiness/model/employee/BusinessEmployee.java index fb9c820..e3e1e48 100644 --- a/src/org/mxchange/jcontactsbusiness/model/employee/BusinessEmployee.java +++ b/src/org/mxchange/jcontactsbusiness/model/employee/BusinessEmployee.java @@ -59,8 +59,7 @@ import org.mxchange.jusercore.model.user.User; @Table (name = "company_employees") @NamedQueries ( { - @NamedQuery (name = "AllEmployees", query = "SELECT e FROM company_employees AS e ORDER BY e.employeeId ASC"), - } + @NamedQuery (name = "AllEmployees", query = "SELECT e FROM company_employees AS e ORDER BY e.employeeId ASC"),} ) @SuppressWarnings ("PersistenceUnitPresent") public class BusinessEmployee implements Employable { @@ -167,6 +166,14 @@ public class BusinessEmployee implements Employable { public BusinessEmployee () { } + /** + * Constructor with all required data + *

+ * @param employeeCompany Basic company data + * @param employeeNumber Employee number (alternative or extra to + * contact) + * @param employeePersonalData Personal data (contact) + */ public BusinessEmployee (final BasicData employeeCompany, final String employeeNumber, final Contact employeePersonalData) { // Call other constructor this(); @@ -177,7 +184,7 @@ public class BusinessEmployee implements Employable { throw new NullPointerException("employeeCompany is null"); //NOI18N } else if (((null == employeeNumber) || (employeeNumber.isEmpty()) && (null == employeePersonalData))) { // At least one must be given - throw new NullPointerException("employeeNumber or employeeContact must be set."); //NOI18N + throw new NullPointerException("employeeNumber or employeePersonalData must be set."); //NOI18N } // Set all fields @@ -194,15 +201,15 @@ public class BusinessEmployee implements Employable { return false; } - final Employable other = (Employable) object; + final Employable employee = (Employable) object; - if (!Objects.equals(this.getEmployeeId(), other.getEmployeeId())) { + if (!Objects.equals(this.getEmployeeId(), employee.getEmployeeId())) { return false; - } else if (!Objects.equals(this.getEmployeeCompany(), other.getEmployeeCompany())) { + } else if (!Objects.equals(this.getEmployeeCompany(), employee.getEmployeeCompany())) { return false; - } else if (!Objects.equals(this.getEmployeeNumber(), other.getEmployeeNumber())) { + } else if (!Objects.equals(this.getEmployeeNumber(), employee.getEmployeeNumber())) { return false; - } else if (!Objects.equals(this.getEmployeePersonalData(), other.getEmployeePersonalData())) { + } else if (!Objects.equals(this.getEmployeePersonalData(), employee.getEmployeePersonalData())) { return false; } diff --git a/src/org/mxchange/jcontactsbusiness/model/employee/Employable.java b/src/org/mxchange/jcontactsbusiness/model/employee/Employable.java index 532f1da..a62a6fc 100644 --- a/src/org/mxchange/jcontactsbusiness/model/employee/Employable.java +++ b/src/org/mxchange/jcontactsbusiness/model/employee/Employable.java @@ -19,13 +19,13 @@ package org.mxchange.jcontactsbusiness.model.employee; import java.io.Serializable; import java.util.Date; import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice; import org.mxchange.jcontactsbusiness.model.department.Department; +import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter; import org.mxchange.jcontactsbusiness.model.jobposition.JobPosition; import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber; import org.mxchange.jusercore.model.user.User; -import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; -import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter; /** * A POJI for employees diff --git a/src/org/mxchange/jcontactsbusiness/model/employee/Employees.java b/src/org/mxchange/jcontactsbusiness/model/employee/Employees.java new file mode 100644 index 0000000..2d675f1 --- /dev/null +++ b/src/org/mxchange/jcontactsbusiness/model/employee/Employees.java @@ -0,0 +1,163 @@ +/* + * 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 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness.model.employee; + +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.Objects; + +/** + * Utilities class for employees + *

+ * @author Roland Häder + */ +public class Employees implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 158_376_461_255_901L; + + /** + * Checks whether both employees are the same + *

+ * @param employee1 Employee 1 + * @param employee2 Employee 2 + *

+ * @return Whether both are the same + */ + public static boolean isSameEmployeeFound (final Employable employee1, final Employable employee2) { + // Validate parameter + if (null == employee1) { + // Throw NPE + throw new NullPointerException("employee1 is null"); //NOI18N + } else if (employee1.getEmployeeCompany() == null) { + // Throw it again + throw new NullPointerException("employee1.employeeCompany is null"); //NOI18N + } else if (employee1.getEmployeeCompany().getBasicDataId() == null) { + // Throw it again + throw new NullPointerException("employee1.employeeCompany.basicDataId is null"); //NOI18N + } else if (employee1.getEmployeeCompany().getBasicDataId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee1.employeeCompany.basicDataId={0} is invalid.", employee1.getEmployeeCompany().getBasicDataId())); //NOI18N + } else if (employee1.getEmployeeBranchOffice() == null && employee1.getEmployeeDepartment() == null && employee1.getEmployeeHeadquarter() == null) { + // At least one must be set + throw new NullPointerException("employee1.employeeBranchOffice, employee1.employeeDepartment and employee1.employeeHeadquarter is null"); //NOI18N + } else if (employee1.getEmployeeBranchOffice() != null && employee1.getEmployeeBranchOffice().getBranchId() == null) { + // Throw NPE + throw new NullPointerException("employee1.employeeBranchOffice.branchId is null"); //NOI18N + } else if (employee1.getEmployeeBranchOffice() != null && employee1.getEmployeeBranchOffice().getBranchId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee1.employeeBranchOffice.branchId={0} is invalid.", employee1.getEmployeeBranchOffice().getBranchId())); //NOI18N + } else if (employee1.getEmployeeDepartment() != null && employee1.getEmployeeDepartment().getDepartmentId() == null) { + // Throw NPE + throw new NullPointerException("employee1.employeeDepartment.departmentId is null"); //NOI18N + } else if (employee1.getEmployeeDepartment() != null && employee1.getEmployeeDepartment().getDepartmentId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee1.employeeDepartment.departmentId={0} is invalid.", employee1.getEmployeeDepartment().getDepartmentId())); //NOI18N + } else if (employee1.getEmployeeHeadquarter() != null && employee1.getEmployeeHeadquarter().getHeadquarterId() == null) { + // Throw NPE + throw new NullPointerException("employee1.employeeHeadquarter.headquarterId is null"); //NOI18N + } else if (employee1.getEmployeeHeadquarter() != null && employee1.getEmployeeHeadquarter().getHeadquarterId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee1.employeeHeadquarter.headquarterId={0} is invalid.", employee1.getEmployeeHeadquarter().getHeadquarterId())); //NOI18N + } else if ((employee1.getEmployeePersonalData() == null) && (employee1.getEmployeeNumber()== null)) { + // Both are null + throw new NullPointerException("employee1.employeePersonalData and employee1.employeeNumber are null"); //NOI18N + } else if ((employee1.getEmployeePersonalData() != null) && (employee1.getEmployeePersonalData().getContactId() == null)) { + // Throw NPE + throw new NullPointerException("employee1.employeePersonalData.contactId is null"); //NOI18N + } else if ((employee1.getEmployeePersonalData() != null) && (employee1.getEmployeePersonalData().getContactId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee1.employeePersonalData.contactId={0} is invalid.", employee1.getEmployeePersonalData().getContactId())); //NOI18N + } else if ((employee1.getEmployeeNumber() != null) && (employee1.getEmployeeNumber().isEmpty())) { + // Throw it again + throw new IllegalArgumentException("employee1.employeeNumber is empty"); //NOI18N + } else if (null == employee2) { + // Throw NPE + throw new NullPointerException("employee2 is null"); //NOI18N + } else if (employee2.getEmployeeCompany() == null) { + // Throw it again + throw new NullPointerException("employee2.employeeCompany is null"); //NOI18N + } else if (employee2.getEmployeeCompany().getBasicDataId() == null) { + // Throw it again + throw new NullPointerException("employee2.employeeCompany.basicDataId is null"); //NOI18N + } else if (employee2.getEmployeeCompany().getBasicDataId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee2.employeeCompany.basicDataId={0} is invalid.", employee2.getEmployeeCompany().getBasicDataId())); //NOI18N + } else if (employee2.getEmployeeBranchOffice() == null && employee2.getEmployeeDepartment() == null && employee2.getEmployeeHeadquarter() == null) { + // At least one must be set + throw new NullPointerException("employee2.employeeBranchOffice, employee2.employeeDepartment and employee2.employeeHeadquarter is null"); //NOI18N + } else if (employee2.getEmployeeBranchOffice() != null && employee2.getEmployeeBranchOffice().getBranchId() == null) { + // Throw NPE + throw new NullPointerException("employee2.employeeBranchOffice.branchId is null"); //NOI18N + } else if (employee2.getEmployeeBranchOffice() != null && employee2.getEmployeeBranchOffice().getBranchId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee2.employeeBranchOffice.branchId={0} is invalid.", employee2.getEmployeeBranchOffice().getBranchId())); //NOI18N + } else if (employee2.getEmployeeDepartment() != null && employee2.getEmployeeDepartment().getDepartmentId() == null) { + // Throw NPE + throw new NullPointerException("employee2.employeeDepartment.departmentId is null"); //NOI18N + } else if (employee2.getEmployeeDepartment() != null && employee2.getEmployeeDepartment().getDepartmentId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee2.employeeDepartment.departmentId={0} is invalid.", employee2.getEmployeeDepartment().getDepartmentId())); //NOI18N + } else if (employee2.getEmployeeHeadquarter() != null && employee2.getEmployeeHeadquarter().getHeadquarterId() == null) { + // Throw NPE + throw new NullPointerException("employee2.employeeHeadquarter.headquarterId is null"); //NOI18N + } else if (employee2.getEmployeeHeadquarter() != null && employee2.getEmployeeHeadquarter().getHeadquarterId() < 1) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee2.employeeHeadquarter.headquarterId={0} is invalid.", employee2.getEmployeeHeadquarter().getHeadquarterId())); //NOI18N + } else if ((employee2.getEmployeePersonalData() == null) && (employee2.getEmployeeNumber()== null)) { + // Both are null + throw new NullPointerException("employee2.employeePersonalData and employee2.employeeNumber are null"); //NOI18N + } else if ((employee2.getEmployeePersonalData() != null) && (employee2.getEmployeePersonalData().getContactId() == null)) { + // Throw NPE + throw new NullPointerException("employee2.employeePersonalData.contactId is null"); //NOI18N + } else if ((employee2.getEmployeePersonalData() != null) && (employee2.getEmployeePersonalData().getContactId() < 1)) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("employee2.employeePersonalData.contactId={0} is invalid.", employee2.getEmployeePersonalData().getContactId())); //NOI18N + } else if ((employee2.getEmployeeNumber() != null) && (employee2.getEmployeeNumber().isEmpty())) { + // Throw it again + throw new IllegalArgumentException("employee2.employeeNumber is empty"); //NOI18N + } else if (Objects.equals(employee1, employee2)) { + // Found equal entity + return true; + } + + // Same data found? + if (!Objects.equals(employee1.getEmployeeCompany(), employee2.getEmployeeCompany())) { + // Not the same companies + return false; + } else if (!Objects.equals(employee1.getEmployeeNumber(), employee2.getEmployeeNumber())) { + // Not same number + return false; + } else if (!Objects.equals(employee1.getEmployeePersonalData(), employee2.getEmployeePersonalData())) { + // Not same personal data + return false; + } + + // Is same employee + return true; + } + + /** + * Private constructor + */ + private Employees () { + // No instances from utilities + } + +} -- 2.39.5