]> git.mxchange.org Git - jproduct-core.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Wed, 16 Sep 2015 13:04:33 +0000 (15:04 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 16 Sep 2015 13:04:33 +0000 (15:04 +0200)
- 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 <roland@mxchange.org>

lib/jcore.jar
src/org/mxchange/jshopcore/exceptions/CustomerAlreadyRegisteredException.java [new file with mode: 0644]
src/org/mxchange/jshopcore/exceptions/QueryNotExecutedException.java [new file with mode: 0644]
src/org/mxchange/jshopcore/model/customer/Customer.java
src/org/mxchange/jshopcore/model/customer/CustomerUtils.java [new file with mode: 0644]
src/org/mxchange/jshopcore/model/customer/ShopCustomer.java

index a56fe629d293ed633c7a4581c04dad90d8cd7caf..4426c96e512eef4eac4d14f9f802bb47fc8361a4 100644 (file)
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 (file)
index 0000000..ae77abe
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..afd51fa
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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());
+       }
+}
index b6059f788f7b35c502255c49e351afb71bd7c7eb..5cf92d3e7f42997c36039482ecf1976e3f341968 100644 (file)
@@ -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 (file)
index 0000000..e712d77
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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;
+       }
+}
index a9202bda32bee0ce537589566a5496a0e1e0d8e5..7ed3df971019f5b91f14767c424b2d57ef343520 100644 (file)
@@ -24,6 +24,11 @@ import org.mxchange.jcore.model.contact.BaseContact;
  * @author Roland Haeder<roland@mxchange.org>
  */
 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;
+       }
 }