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.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.codec.digest.Sha2Crypt;
25 import org.mxchange.jcore.BaseFrameworkSystem;
28 * An utilities class for customers
30 * @author Roland Haeder
32 public class CustomerUtils extends BaseFrameworkSystem {
35 * No instance from this class
37 private CustomerUtils () {
41 * Generates an unique customer number by checking is existence
43 * @param connection Connection instance
44 * @return Generated customer number (not used before)
45 * @throws java.sql.SQLException If any SQL error occured
47 public static String generateCustomerNumber (final Connection connection) throws SQLException {
49 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
50 // connection cannot be null
51 if (null == connection) {
53 throw new NullPointerException("connection is null"); //NOI18N
57 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1"); //NOI18N
60 String customerNumber = null;
63 boolean isFound = true;
67 // Is the number used?
70 part1 = Math.round(Math.random() * 100000);
71 part2 = Math.round(Math.random() * 1000);
73 // Generate new number
74 customerNumber = String.format("%s-%s", part1, part2); //NOI18N
77 // TODO: utils.getLogger().logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
78 // Insert customer number
79 statement.setString(1, customerNumber);
85 ResultSet result = statement.getResultSet();
87 // Try to get next result
88 isFound = result.next();
92 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
94 return customerNumber;
98 * Generates an unique access key.
100 * @param connection Connection instance
101 * @param customer Customer instance
102 * @return An unique access key
103 * @throws java.sql.SQLException If any SQL error occured
105 public static String generateAccessKey (final Connection connection, final Customer customer) throws SQLException {
107 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
108 // connection cannot be null
109 if (null == connection) {
111 throw new NullPointerException("connection is null"); //NOI18N
115 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `orders` WHERE `access_key` = ? LIMIT 1"); //NOI18N
117 // Generate access keyy
118 String accessKey = null;
121 boolean isFound = true;
123 // Is the number used?
126 String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), connection, customer.getCustomerNumber());
128 // Generate new number
129 accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
132 // TODO: utils.getLogger().logDebug(MessageFormat.format("generateAccessKey: accessKey={0}", accessKey));
133 // Insert customer number
134 statement.setString(1, accessKey);
140 ResultSet result = statement.getResultSet();
142 // Try to get next result
143 isFound = result.next();
147 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));