From: Roland Häder Date: Wed, 19 Apr 2017 18:54:46 +0000 (+0200) Subject: renamed class + removed generic method as it exists in jcustomer-core, too. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e89a8577b4481006875d1849d242e9eabc5f44d0;p=jshop-core.git renamed class + removed generic method as it exists in jcustomer-core, too. Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java b/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java deleted file mode 100644 index b0b33f9..0000000 --- a/src/org/mxchange/jshopcore/model/customer/CustomerUtils.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2016 Roland Häder - * - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.jshopcore.model.customer; - -import java.sql.SQLException; -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.jcustomercore.model.customer.Customer; -import org.mxchange.jshopcore.model.order.Orderable; -import org.mxchange.jshopcore.model.order.ShopOrder; - -/** - * An utilities class for customers - *

- * @author Roland Häder - */ -public class CustomerUtils { - - /** - * Generates an unique access key. - *

- * @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)); - - // 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."); - } - - // Generate fake order instance - Orderable orderable = null; - - // 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() * 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; - } - } - - // Trace message - // @TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey)); - // Found one - return accessKey; - } - - /** - * Generates an unique customer number by checking is existence - *

- * @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 EntityManager em) throws SQLException { - // Trace message - // @TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection)); - - // 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."); - } - - // Generate number - String customerNumber = null; - - // Default is found - boolean isFound = true; - - // Is the number used? - while (isFound) { - // Both number parts - long part1 = Math.round(Math.random() * 100_000); - long part2 = Math.round(Math.random() * 1_000); - - // Generate new number - customerNumber = String.format("%s-%s", part1, part2); //NOI18N - - // Try it - try { - // Get instance - Customer customer = em.getReference(Customer.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; - } - - /** - * Private constructor - */ - private CustomerUtils () { - // Prevents making instances from utilities classes - } - -} diff --git a/src/org/mxchange/jshopcore/model/customer/ShopCustomerUtils.java b/src/org/mxchange/jshopcore/model/customer/ShopCustomerUtils.java new file mode 100644 index 0000000..145fa85 --- /dev/null +++ b/src/org/mxchange/jshopcore/model/customer/ShopCustomerUtils.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2016 Roland Häder + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jshopcore.model.customer; + +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.jcustomercore.model.customer.Customer; +import org.mxchange.jshopcore.model.order.Orderable; +import org.mxchange.jshopcore.model.order.ShopOrder; + +/** + * An utilities class for customers + *

+ * @author Roland Häder + */ +public class ShopCustomerUtils { + + /** + * Generates an unique access key. + *

+ * @param em Entity manager instance + * @param customer Customer instance + *

+ * @return An unique access key + */ + public static String generateAccessKey (final EntityManager em, final Customer customer) { + // 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."); + } + + // Generate fake order instance + Orderable orderable = null; + + // 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() * 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; + } + } + + // Found one + return accessKey; + } + + /** + * Private constructor + */ + private ShopCustomerUtils () { + // Prevents making instances from utilities classes + } + +}