--- /dev/null
+/*
+ * 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()));
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
*/
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
*
--- /dev/null
+/*
+ * 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;
+ }
+}
* @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.
*/
*/
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;
+ }
}