]> git.mxchange.org Git - jshop-core.git/blob - src/org/mxchange/jshopcore/model/customer/CustomerUtils.java
JPA started
[jshop-core.git] / src / org / mxchange / jshopcore / model / customer / CustomerUtils.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jshopcore.model.customer;
18
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;
26
27 /**
28  * An utilities class for customers
29  *
30  * @author Roland Haeder
31  */
32 public class CustomerUtils extends BaseFrameworkSystem {
33
34         /**
35          * No instance from this class
36          */
37         private CustomerUtils () {
38         }
39
40         /**
41          * Generates an unique customer number by checking is existence
42          *
43          * @param connection Connection instance
44          * @return Generated customer number (not used before)
45          * @throws java.sql.SQLException If any SQL error occured
46          */
47         public static String generateCustomerNumber (final Connection connection) throws SQLException {
48                 // Trace message
49                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
50                 // connection cannot be null
51                 if (null == connection) {
52                         // Abort here
53                         throw new NullPointerException("connection is null"); //NOI18N
54                 }
55
56                 // Prepare statement
57                 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1"); //NOI18N
58
59                 // Generate number
60                 String customerNumber = null;
61
62                 // Default is found
63                 boolean isFound = true;
64                 long part1 = 0;
65                 long part2 = 0;
66
67                 // Is the number used?
68                 while (isFound) {
69                         // Both number parts
70                         part1 = Math.round(Math.random() * 100000);
71                         part2 = Math.round(Math.random() * 1000);
72
73                         // Generate new number
74                         customerNumber = String.format("%s-%s", part1, part2); //NOI18N
75
76                         // Debug message
77                         // TODO: utils.getLogger().logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
78                         // Insert customer number
79                         statement.setString(1, customerNumber);
80
81                         // Find it
82                         statement.execute();
83
84                         // Get result
85                         ResultSet result = statement.getResultSet();
86
87                         // Try to get next result
88                         isFound = result.next();
89                 }
90
91                 // Trace message
92                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
93                 // Found one
94                 return customerNumber;
95         }
96
97         /**
98          * Generates an unique access key.
99          *
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
104          */
105         public static String generateAccessKey (final Connection connection, final Customer customer) throws SQLException {
106                 // Trace message
107                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
108                 // connection cannot be null
109                 if (null == connection) {
110                         // Abort here
111                         throw new NullPointerException("connection is null"); //NOI18N
112                 }
113
114                 // Prepare statement
115                 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `orders` WHERE `access_key` = ? LIMIT 1"); //NOI18N
116
117                 // Generate access keyy
118                 String accessKey = null;
119
120                 // Default is found
121                 boolean isFound = true;
122
123                 // Is the number used?
124                 while (isFound) {
125                         // Both number parts
126                         String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), connection, customer.getCustomerNumber());
127
128                         // Generate new number
129                         accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
130
131                         // Debug message
132                         // TODO: utils.getLogger().logDebug(MessageFormat.format("generateAccessKey: accessKey={0}", accessKey));
133                         // Insert customer number
134                         statement.setString(1, accessKey);
135
136                         // Find it
137                         statement.execute();
138
139                         // Get result
140                         ResultSet result = statement.getResultSet();
141
142                         // Try to get next result
143                         isFound = result.next();
144                 }
145
146                 // Trace message
147                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
148                 // Found one
149                 return accessKey;
150         }
151 }