/*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 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
/**
* An utilities class for customers
- *
+ * <p>
* @author Roland Haeder
*/
public class CustomerUtils extends BaseFrameworkSystem {
/**
- * No instance from this class
- */
- private CustomerUtils () {
- }
-
- /**
- * Generates an unique customer number by checking is existence
- *
+ * Generates an unique access key.
+ * <p>
* @param em Entity manager instance
- * @return Generated customer number (not used before)
- * @throws java.sql.SQLException If any SQL error occured
+ * @param customer Customer instance
+ * <p>
+ * @return An unique access key
*/
- public static String generateCustomerNumber (final EntityManager em) throws SQLException {
+ public static String generateAccessKey (final EntityManager em, final Customer customer) {
// Trace message
- // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
// em cannot be null
if (null == em) {
throw new IllegalStateException("Entity manager is closed.");
}
- // Fake customer instance
- Customer customer = null;
+ // Generate fake order instance
+ Orderable orderable = null;
- // Generate number
- String customerNumber = null;
+ // Generate access keyy
+ String accessKey = null;
// Default is found
boolean isFound = true;
- long part1 = 0;
- long part2 = 0;
// Is the number used?
while (isFound) {
// Both number parts
- part1 = Math.round(Math.random() * 100000);
- part2 = Math.round(Math.random() * 1000);
+ String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1_000_000)), em, customer.getCustomerNumber());
- // Generate new number
- customerNumber = String.format("%s-%s", part1, part2); //NOI18N
+ // 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 it
+ // Try this
try {
- // Get instance
- customer = em.getReference(ShopCustomer.class, customerNumber);
+ // Get reference
+ orderable = em.getReference(ShopOrder.class, accessKey);
} catch (final EntityNotFoundException ex) {
- // Not found
+ // Not found, so abort loop here
isFound = false;
}
}
// Trace message
- // TODO: utils.getLogger().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;
}
/**
- * Generates an unique access key.
- *
+ * Generates an unique customer number by checking is existence
+ * <p>
* @param em Entity manager instance
- * @param customer Customer instance
- * @return An unique access key
+ * <p>
+ * @return Generated customer number (not used before)
+ * <p>
+ * @throws java.sql.SQLException If any SQL error occured
*/
- public static String generateAccessKey (final EntityManager em, final Customer customer) {
+ public static String generateCustomerNumber (final EntityManager em) throws SQLException {
// Trace message
- // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
// em cannot be null
if (null == em) {
throw new IllegalStateException("Entity manager is closed.");
}
- // Generate fake order instance
- Orderable orderable = null;
-
- // Generate access keyy
- String accessKey = null;
+ // Generate number
+ String customerNumber = 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)), em, customer.getCustomerNumber());
+ long part1 = Math.round(Math.random() * 100_000);
+ long part2 = Math.round(Math.random() * 1_000);
- // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
- accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
+ // Generate new number
+ customerNumber = String.format("%s-%s", part1, part2); //NOI18N
- // Try this
+ // Try it
try {
- // Get reference
- orderable = em.getReference(ShopOrder.class, accessKey);
+ // Get instance
+ Customer customer = em.getReference(ShopCustomer.class, customerNumber);
} catch (final EntityNotFoundException ex) {
- // Not found, so abort loop here
+ // Not found
isFound = false;
}
}
// Trace message
- // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
-
+ // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
// Found one
- return accessKey;
+ return customerNumber;
+ }
+
+ /**
+ * No instance from this class
+ */
+ private CustomerUtils () {
}
}