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;
72 // Is the number used?
75 part1 = Math.round(Math.random() * 100000);
76 part2 = Math.round(Math.random() * 1000);
78 // Generate new number
79 customerNumber = String.format("%s-%s", part1, part2); //NOI18N
84 customer = em.getReference(ShopCustomer.class, customerNumber);
85 } catch (final EntityNotFoundException ex) {
92 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
95 return customerNumber;
99 * Generates an unique access key.
101 * @param em Entity manager instance
102 * @param customer Customer instance
103 * @return An unique access key
105 public static String generateAccessKey (final EntityManager em, final Customer customer) {
107 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
112 throw new NullPointerException("em is null"); //NOI18N
113 } else if (!em.isOpen()) {
115 throw new IllegalStateException("Entity manager is closed.");
118 // Generate fake order instance
119 Orderable orderable = null;
121 // Generate access keyy
122 String accessKey = null;
125 boolean isFound = true;
127 // Is the number used?
130 String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), em, customer.getCustomerNumber());
132 // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
133 accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
138 orderable = em.getReference(ShopOrder.class, accessKey);
139 } catch (final EntityNotFoundException ex) {
140 // Not found, so abort loop here
146 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));