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
* 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");
- }
-
+ public static String generateCustomerNumber (final Connection connection) throws SQLException {
// Trace message
- logger.logTrace(MessageFormat.format("generateCustomerNumber: connection={0},logger={1} - CALLED!", connection, logger));
-
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
// connection cannot be null
if (null == connection) {
// Abort here
- throw new NullPointerException("connection is null");
+ throw new NullPointerException("connection is null"); //NOI18N
}
// Prepare statement
- PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1");
+ PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1"); //NOI18N
// Generate number
- String customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000));
+ String customerNumber = null;
// Default is found
boolean isFound = true;
+ long part1 = 0;
+ long part2 = 0;
// Is the number used?
while (isFound) {
- // Debug message
- logger.logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
+ // Both number parts
+ part1 = Math.round(Math.random() * 100000);
+ part2 = Math.round(Math.random() * 1000);
+
+ // Generate new number
+ customerNumber = String.format("%s-%s", part1, part2); //NOI18N
+ // Debug message
+ // TODO: utils.getLogger().logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
// Insert customer number
- statement.setString(0, customerNumber);
+ statement.setString(1, 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));
+ // Try to get next result
+ isFound = result.next();
}
// Trace message
- logger.logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
-
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
// Found one
return customerNumber;
}