2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jshopcore.model.customer;
19 import java.sql.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import org.mxchange.jcore.BaseFrameworkSystem;
25 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
28 * An utilities class for customers
30 * @author Roland Haeder
32 public class CustomerUtils extends BaseFrameworkSystem {
35 * No instance from this class
37 private CustomerUtils () {
41 * Generates an unique customer number by checking is existence
43 * @param connection Connection instance
44 * @param logger Logger instance
45 * @return Generated customer number (not used before)
46 * @throws java.sql.SQLException If any SQL error occured
48 public static String generateCustomerNumber (final Connection connection, final LoggerBeanLocal logger) throws SQLException {
49 // logger cannot be null
52 throw new NullPointerException("logger is null");
56 logger.logTrace(MessageFormat.format("generateCustomerNumber: connection={0},logger={1} - CALLED!", connection, logger));
58 // connection cannot be null
59 if (null == connection) {
61 throw new NullPointerException("connection is null");
65 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1");
68 String customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000));
71 boolean isFound = true;
73 // Is the number used?
76 logger.logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
78 // Insert customer number
79 statement.setString(0, customerNumber);
85 ResultSet result = statement.getResultSet();
91 if (result.isLast()) {
97 // Generate new number
98 customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000));
102 logger.logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
105 return customerNumber;