X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjshopcore%2Fmodel%2Fcustomer%2FCustomerUtils.java;h=d763937e536bd2962a3191bb19c470756102007d;hb=2f41e66470201a3057be8757cc53f737d53e2c82;hp=ce15d8f8df538c2327730b25eeb8ff917d9d94a6;hpb=6126a768e5387d79605a680f24421a2662b02c24;p=jproduct-core.git diff --git a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java index ce15d8f..d763937 100644 --- a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java +++ b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java @@ -1,5 +1,5 @@ /* - * 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 @@ -27,27 +27,22 @@ import org.mxchange.jshopcore.model.order.ShopOrder; /** * An utilities class for customers - * + *

* @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. + *

* @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 + *

+ * @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) { @@ -58,53 +53,51 @@ public class CustomerUtils extends BaseFrameworkSystem { 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 + *

* @param em Entity manager instance - * @param customer Customer instance - * @return An unique access key + *

+ * @return Generated customer number (not used before) + *

+ * @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) { @@ -115,11 +108,8 @@ public class CustomerUtils extends BaseFrameworkSystem { 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; @@ -127,25 +117,31 @@ public class CustomerUtils extends BaseFrameworkSystem { // 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 () { } }