2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jshopcore.model.customer;
19 import java.sql.SQLException;
20 import javax.persistence.EntityManager;
21 import javax.persistence.EntityNotFoundException;
22 import org.apache.commons.codec.binary.Base64;
23 import org.apache.commons.codec.digest.Sha2Crypt;
24 import org.mxchange.jcore.BaseFrameworkSystem;
25 import org.mxchange.jshopcore.model.order.Orderable;
26 import org.mxchange.jshopcore.model.order.ShopOrder;
29 * An utilities class for customers
31 * @author Roland Haeder
33 public class CustomerUtils extends BaseFrameworkSystem {
36 * No instance from this class
38 private CustomerUtils () {
42 * Generates an unique customer number by checking is existence
44 * @param em Entity manager instance
45 * @return Generated customer number (not used before)
46 * @throws java.sql.SQLException If any SQL error occured
48 public static String generateCustomerNumber (final EntityManager em) throws SQLException {
50 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
55 throw new NullPointerException("em is null"); //NOI18N
56 } else if (!em.isOpen()) {
58 throw new IllegalStateException("Entity manager is closed.");
61 // Fake customer instance
62 Customer customer = null;
65 String customerNumber = null;
68 boolean isFound = true;
74 // Is the number used?
77 part1 = Math.round(Math.random() * 100_000);
78 part2 = Math.round(Math.random() * 1_000);
80 // Generate new number
81 customerNumber = String.format("%s-%s", part1, part2); //NOI18N
86 customer = em.getReference(ShopCustomer.class, customerNumber);
87 } catch (final EntityNotFoundException ex) {
94 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
97 return customerNumber;
101 * Generates an unique access key.
103 * @param em Entity manager instance
104 * @param customer Customer instance
105 * @return An unique access key
107 public static String generateAccessKey (final EntityManager em, final Customer customer) {
109 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
114 throw new NullPointerException("em is null"); //NOI18N
115 } else if (!em.isOpen()) {
117 throw new IllegalStateException("Entity manager is closed.");
120 // Generate fake order instance
121 Orderable orderable = null;
123 // Generate access keyy
124 String accessKey = null;
127 boolean isFound = true;
129 // Is the number used?
132 String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1_000_000)), em, customer.getCustomerNumber());
134 // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
135 accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
140 orderable = em.getReference(ShopOrder.class, accessKey);
141 } catch (final EntityNotFoundException ex) {
142 // Not found, so abort loop here
148 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));