import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.text.MessageFormat;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.digest.Sha2Crypt;
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();
+ // Try to get next result
+ isFound = result.next();
+ }
- // Found a record?
- if (result.isLast()) {
- // Not found
- isFound = false;
- break;
- }
+ // Trace message
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
+ // Found one
+ return customerNumber;
+ }
+
+ /**
+ * Generates an unique access key.
+ *
+ * @param connection Connection instance
+ * @param customer Customer instance
+ * @return An unique access key
+ * @throws java.sql.SQLException If any SQL error occured
+ */
+ public static String generateAccessKey (final Connection connection, final Customer customer) throws SQLException {
+ // Trace message
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
+ // connection cannot be null
+ if (null == connection) {
+ // Abort here
+ throw new NullPointerException("connection is null"); //NOI18N
+ }
+
+ // Prepare statement
+ PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `orders` WHERE `access_key` = ? LIMIT 1"); //NOI18N
+
+ // Generate access keyy
+ String accessKey = null;
+
+ // Default is found
+ boolean isFound = true;
+
+ // Is the number used?
+ while (isFound) {
+ // Both number parts
+ String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), connection, customer.getCustomerNumber());
// Generate new number
- customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" + Long.toString(Math.round(Math.random() * 1000));
+ accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
+
+ // Debug message
+ // TODO: utils.getLogger().logDebug(MessageFormat.format("generateAccessKey: accessKey={0}", accessKey));
+ // Insert customer number
+ statement.setString(1, accessKey);
+
+ // Find it
+ statement.execute();
+
+ // Get result
+ ResultSet result = statement.getResultSet();
+
+ // 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("generateAccessKey: accessKey={0} - EXIT!", accessKey));
// Found one
- return customerNumber;
+ return accessKey;
}
}