2 * Copyright (C) 2016 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 * Generates an unique access key.
38 * @param em Entity manager instance
39 * @param customer Customer instance
41 * @return An unique access key
43 public static String generateAccessKey (final EntityManager em, final Customer customer) {
45 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
50 throw new NullPointerException("em is null"); //NOI18N
51 } else if (!em.isOpen()) {
53 throw new IllegalStateException("Entity manager is closed.");
56 // Generate fake order instance
57 Orderable orderable = null;
59 // Generate access keyy
60 String accessKey = null;
63 boolean isFound = true;
65 // Is the number used?
68 String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1_000_000)), em, customer.getCustomerNumber());
70 // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
71 accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
76 orderable = em.getReference(ShopOrder.class, accessKey);
77 } catch (final EntityNotFoundException ex) {
78 // Not found, so abort loop here
84 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
90 * Generates an unique customer number by checking is existence
92 * @param em Entity manager instance
94 * @return Generated customer number (not used before)
96 * @throws java.sql.SQLException If any SQL error occured
98 public static String generateCustomerNumber (final EntityManager em) throws SQLException {
100 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
105 throw new NullPointerException("em is null"); //NOI18N
106 } else if (!em.isOpen()) {
108 throw new IllegalStateException("Entity manager is closed.");
112 String customerNumber = null;
115 boolean isFound = true;
117 // Is the number used?
120 long part1 = Math.round(Math.random() * 100_000);
121 long part2 = Math.round(Math.random() * 1_000);
123 // Generate new number
124 customerNumber = String.format("%s-%s", part1, part2); //NOI18N
129 Customer customer = em.getReference(ShopCustomer.class, customerNumber);
130 } catch (final EntityNotFoundException ex) {
137 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
139 return customerNumber;
143 * No instance from this class
145 private CustomerUtils () {