+/*
+ * 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;
+ }
+}