From 3b6abb29616d902daa07d0ab0ec3bd31037826d9 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 16 Sep 2015 15:04:33 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20added=20new=20field=20contactId?= =?utf8?q?=20for=20linking=20customer=20->=20contact=20-=20added=20new=20e?= =?utf8?q?xceptions=20-=20added=20CustomerUtils=20class=20which=20helps=20?= =?utf8?q?generating=20customer=20number=20and=20such=20-=20updated=20jar?= =?utf8?q?=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/jcore.jar | Bin 27933 -> 28035 bytes .../CustomerAlreadyRegisteredException.java | 42 +++++++ .../exceptions/QueryNotExecutedException.java | 42 +++++++ .../jshopcore/model/customer/Customer.java | 21 ++++ .../model/customer/CustomerUtils.java | 107 ++++++++++++++++++ .../model/customer/ShopCustomer.java | 20 ++++ 6 files changed, 232 insertions(+) create mode 100644 src/org/mxchange/jshopcore/exceptions/CustomerAlreadyRegisteredException.java create mode 100644 src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java create mode 100644 src/org/mxchange/jshopcore/model/customer/CustomerUtils.java diff --git a/lib/jcore.jar b/lib/jcore.jar index a56fe629d293ed633c7a4581c04dad90d8cd7caf..4426c96e512eef4eac4d14f9f802bb47fc8361a4 100644 GIT binary patch delta 2894 zcmZWr3s6+o8UF6wb?@cwT|qz)76Qm)c`Wi2l&3rtkcS{hc!;p9%gRHN5RzB}0vaDN ztrI-a*hcb_HZ)1}GoleKbw*NT`Oc&40IsZBT z<2&d6k8|$SEoskf$x>f!FlvAb934)dv?N-N45wFI3U*Jq0k|hiJ59MO(mQiHKbq56>oBl6AGqPd{;vpzNg|XZaKsG-&gUr;wdrKsqECU81{wU!tN#pvm#TVXNAQn zD|Q4xLaOJO|4x&|b_R)Okd%BHmcV2d71AZ8u=y<%pmvhQ?#jE^GQoDC@Bl)IDSe?o!l`PyHqy8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RMzNlf=V^VpKz+MCU@Erqlu-`xmQYCl5KpN5wK6jZ4LaHijDk>Mc7uUFIY8z{$ zyvn<9@w=*?&mops(ZeZiN&F-o(~-&3AQWK+3-Xx1M4l!4E2qMODG0_)BrpdoKrtU| znIRWuF{W^~!4+V=3!6ZR!0ebWpgP%83QEU3y+pH??wBP#r9&U{6oMR-A=R|aLOCk@ zo-F!*5h75ju&*!pV5oA}WNmUObE&Q=DtMl``h3=x@z~sy8!(sm0qSmwqF2iVU2U@4 zOoU6V7IpMl(~(4Yh zd`n=u&PC54M@hp>fnuG5@Sqfy$k5G}l2RLgSa}c|R`6G7n4|OIsM0W3$2`mza8X}@ zow}Fbp))J?TB`*XQ2xr7OKVW8K$MqOS65cV3e*{VN){@Z*x9nVC2P~hj&)hp9bIc% zJ=Gl#Zxm=Sun3D4=IhYU1giNUqL&_ELs3|a63)drXQaszfm z(cfl1K)c=1%1S!!o)qLZ(2N!Zi4>+r&~N-|3jgDc=LS=y9mg3S~!4qB|bL zgzhrrl29zIZ}UO(TOH~?g_>iX>0fhK#jE(+6hAispO%h}xsA8cwN{^>-?5+nDD+WV zBz4CKvbPyA@`Cf5F(bq4Ytlyn{l0CosXH~@@NZ{(n?u>k5@*^s?I!k~BR;a9mX5G~ z_SCPhCKKsvQs*<3uCGg_rR|2PDE}E85rFLS9=O+r2MR4_#ICy|=xlqqsXSD;xuQtr z_mq3uJx!*=14koGb;Iox;&UiR@J7)tV|u{qO5FLwIA>{6#g?MxXHg;Bs-9~`~b zp}w8!w~)zfUK?*Nk?^B{qaXA3WG@@H&7qvH7{tnumDsz-vsV2#sNh`RSK&OjQO^46 Jz!j?{_. + */ +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; + } } -- 2.39.5