*/
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 javax.persistence.EntityManager;
+import javax.persistence.EntityNotFoundException;
+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;
+import org.mxchange.jshopcore.model.order.Orderable;
+import org.mxchange.jshopcore.model.order.ShopOrder;
/**
* An utilities class for customers
- *
+ * <p>
* @author Roland Haeder
*/
public class CustomerUtils extends BaseFrameworkSystem {
/**
* Generates an unique customer number by checking is existence
- *
- * @param connection Connection instance
- * @param logger Logger instance
+ * <p>
+ * @param em Entity manager 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 EntityManager em) 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) {
+ // em cannot be null
+ if (null == em) {
// Abort here
- throw new NullPointerException("connection is null");
+ throw new NullPointerException("em is null"); //NOI18N
+ } else if (!em.isOpen()) {
+ // Not open
+ throw new IllegalStateException("Entity manager is closed.");
}
- // Prepare statement
- PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1");
+ // Fake customer instance
+ Customer customer = null;
// 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;
+ // Declare variables
+ long part1;
+ long part2;
+
// Is the number used?
while (isFound) {
- // Debug message
- logger.logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
+ // Both number parts
+ part1 = Math.round(Math.random() * 100_000);
+ part2 = Math.round(Math.random() * 1_000);
+
+ // Generate new number
+ customerNumber = String.format("%s-%s", part1, part2); //NOI18N
+
+ // Try it
+ try {
+ // Get instance
+ customer = em.getReference(ShopCustomer.class, customerNumber);
+ } catch (final EntityNotFoundException ex) {
+ // Not found
+ isFound = false;
+ }
+ }
+
+ // Trace message
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
+ // Found one
+ return customerNumber;
+ }
- // Insert customer number
- statement.setString(0, customerNumber);
+ /**
+ * Generates an unique access key.
+ * <p>
+ * @param em Entity manager instance
+ * @param customer Customer instance
+ * @return An unique access key
+ */
+ public static String generateAccessKey (final EntityManager em, final Customer customer) {
+ // Trace message
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
- // Find it
- statement.execute();
+ // em cannot be null
+ if (null == em) {
+ // Abort here
+ throw new NullPointerException("em is null"); //NOI18N
+ } else if (!em.isOpen()) {
+ // Not open
+ throw new IllegalStateException("Entity manager is closed.");
+ }
- // Get result
- ResultSet result = statement.getResultSet();
+ // Generate fake order instance
+ Orderable orderable = null;
- // Rewind it
- result.beforeFirst();
+ // Generate access keyy
+ String accessKey = null;
- // Found a record?
- if (result.isLast()) {
- // Not found
+ // 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() * 1_000_000)), em, customer.getCustomerNumber());
+
+ // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
+ accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
+
+ // Try this
+ try {
+ // Get reference
+ orderable = em.getReference(ShopOrder.class, accessKey);
+ } catch (final EntityNotFoundException ex) {
+ // Not found, so abort loop here
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));
-
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
// Found one
- return customerNumber;
+ return accessKey;
}
}