From: Roland Haeder Date: Wed, 16 Sep 2015 13:04:33 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3b6abb29616d902daa07d0ab0ec3bd31037826d9;p=jshop-core.git Continued: - added new field contactId for linking customer -> contact - added new exceptions - added CustomerUtils class which helps generating customer number and such - updated jar Signed-off-by:Roland Häder --- diff --git a/lib/jcore.jar b/lib/jcore.jar index a56fe62..4426c96 100644 Binary files a/lib/jcore.jar and b/lib/jcore.jar differ diff --git a/src/org/mxchange/jshopcore/exceptions/CustomerAlreadyRegisteredException.java b/src/org/mxchange/jshopcore/exceptions/CustomerAlreadyRegisteredException.java new file mode 100644 index 0000000..ae77abe --- /dev/null +++ b/src/org/mxchange/jshopcore/exceptions/CustomerAlreadyRegisteredException.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * 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.jshopcore.exceptions; + +import java.text.MessageFormat; +import org.mxchange.jshopcore.model.customer.Customer; + +/** + * An exception thrown when the customer is already registered + * + * @author Roland Haeder + */ +public class CustomerAlreadyRegisteredException extends Exception { + /** + * Serial number + */ + private static final long serialVersionUID = 16_435_892_878_271L; + + /** + * Constructor with already registered customer instance + * + * @param customer Customer instance + */ + public CustomerAlreadyRegisteredException (final Customer customer) { + // Call super contructor + super(MessageFormat.format("Customer {0} already registered with number {1} at record id {2}. Maybe forgot to call isRegistered(customer) ?", customer, customer.getCustomerNumber(), customer.getId())); + } +} diff --git a/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java b/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java new file mode 100644 index 0000000..afd51fa --- /dev/null +++ b/src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * 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.jshopcore.exceptions; + +import java.sql.SQLWarning; +import java.text.MessageFormat; + +/** + * This exception is thrown when a query could not be executed + * + * @author Roland Haeder + */ +public class QueryNotExecutedException extends Exception { + /** + * Serial number + */ + private static final long serialVersionUID = 97_548_375_847_581L; + + /** + * Prepares exception with given sql warnings + * + * @param warnings SQL warnings + */ + public QueryNotExecutedException (final SQLWarning warnings) { + // Call super contructor + super(MessageFormat.format("SQL statement did not execute: {0} with state: {1}", warnings.getMessage(), warnings.getSQLState()), warnings.getCause()); + } +} diff --git a/src/org/mxchange/jshopcore/model/customer/Customer.java b/src/org/mxchange/jshopcore/model/customer/Customer.java index b6059f7..5cf92d3 100644 --- a/src/org/mxchange/jshopcore/model/customer/Customer.java +++ b/src/org/mxchange/jshopcore/model/customer/Customer.java @@ -25,6 +25,27 @@ import org.mxchange.jcore.model.contact.Contact; */ public interface Customer extends Contact { + /** + * Setter for id number from "contact" table + * + * @param contactId Contact id number + */ + public void setContactId (final long contactId); + + /** + * Getter for id number from "contact" table + * + * @return Contact id number + */ + public long getContactId(); + + /** + * Setter for customer number + * + * @param customerNumber Customer number + */ + public void setCustomerNumber (final String customerNumber); + /** * Getter for customer number * diff --git a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java new file mode 100644 index 0000000..e712d77 --- /dev/null +++ b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * 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.jshopcore.model.customer; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.MessageFormat; +import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; + +/** + * An utilities class for customers + * + * @author Roland Haeder + */ +public class CustomerUtils extends BaseFrameworkSystem { + + /** + * No instance from this class + */ + private CustomerUtils () { + } + + /** + * Generates an unique customer number by checking is existence + * + * @param connection Connection instance + * @param logger Logger instance + * @return Generated customer number (not used before) + * @throws java.sql.SQLException If any SQL error occured + */ + public static String generateCustomerNumber (final Connection connection, final LoggerBeanLocal logger) throws SQLException { + // logger cannot be null + if (null == logger) { + // Abort here + throw new NullPointerException("logger is null"); + } + + // Trace message + logger.logTrace(MessageFormat.format("generateCustomerNumber: connection={0},logger={1} - CALLED!", connection, logger)); + + // connection cannot be null + if (null == connection) { + // Abort here + throw new NullPointerException("connection is null"); + } + + // Prepare statement + PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1"); + + // Generate number + String customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000)); + + // Default is found + boolean isFound = true; + + // Is the number used? + while (isFound) { + // Debug message + logger.logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber)); + + // Insert customer number + statement.setString(0, customerNumber); + + // Find it + statement.execute(); + + // Get result + ResultSet result = statement.getResultSet(); + + // Rewind it + result.beforeFirst(); + + // Found a record? + if (result.isLast()) { + // Not found + isFound = false; + break; + } + + // Generate new number + customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000)); + } + + // Trace message + logger.logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber)); + + // Found one + return customerNumber; + } +} diff --git a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java index a9202bd..7ed3df9 100644 --- a/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java +++ b/src/org/mxchange/jshopcore/model/customer/ShopCustomer.java @@ -24,6 +24,11 @@ import org.mxchange.jcore.model.contact.BaseContact; * @author Roland Haeder */ public class ShopCustomer extends BaseContact implements Customer { + /** + * Id number from "contact" table + */ + private long contactId; + /** * Customer number, this is different to the database entry id. */ @@ -34,8 +39,23 @@ public class ShopCustomer extends BaseContact implements Customer { */ private static final long serialVersionUID = 4_328_454_581_751L; + @Override + public long getContactId () { + return this.contactId; + } + + @Override + public void setContactId (final long contactId) { + this.contactId = contactId; + } + @Override public String getCustomerNumber () { return this.customerNumber; } + + @Override + public void setCustomerNumber (final String customerNumber) { + this.customerNumber = customerNumber; + } }