]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jshopcore/model/customer/CustomerUtils.java
1ef7cfb1545eb146f21e45dde5b6b8a5552e757e
[jcustomer-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.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;
27
28 /**
29  * An utilities class for customers
30  * <p>
31  * @author Roland Haeder
32  */
33 public class CustomerUtils extends BaseFrameworkSystem {
34
35         /**
36          * No instance from this class
37          */
38         private CustomerUtils () {
39         }
40
41         /**
42          * Generates an unique customer number by checking is existence
43          * <p>
44          * @param em Entity manager instance
45          * <p>
46          * @return Generated customer number (not used before)
47          * <p>
48          * @throws java.sql.SQLException If any SQL error occured
49          */
50         public static String generateCustomerNumber (final EntityManager em) throws SQLException {
51                 // Trace message
52                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: connection={0} - CALLED!", connection));
53
54                 // em cannot be null
55                 if (null == em) {
56                         // Abort here
57                         throw new NullPointerException("em is null"); //NOI18N
58                 } else if (!em.isOpen()) {
59                         // Not open
60                         throw new IllegalStateException("Entity manager is closed.");
61                 }
62
63                 // Generate number
64                 String customerNumber = null;
65
66                 // Default is found
67                 boolean isFound = true;
68
69                 // Is the number used?
70                 while (isFound) {
71                         // Both number parts
72                         long part1 = Math.round(Math.random() * 100_000);
73                         long part2 = Math.round(Math.random() * 1_000);
74
75                         // Generate new number
76                         customerNumber = String.format("%s-%s", part1, part2); //NOI18N
77
78                         // Try it
79                         try {
80                                 // Get instance
81                                 Customer customer = em.getReference(ShopCustomer.class, customerNumber);
82                         } catch (final EntityNotFoundException ex) {
83                                 // Not found
84                                 isFound = false;
85                         }
86                 }
87
88                 // Trace message
89                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
90                 // Found one
91                 return customerNumber;
92         }
93
94         /**
95          * Generates an unique access key.
96          * <p>
97          * @param em Entity manager instance
98          * @param customer Customer instance
99          * <p>
100          * @return An unique access key
101          */
102         public static String generateAccessKey (final EntityManager em, final Customer customer) {
103                 // Trace message
104                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
105
106                 // em cannot be null
107                 if (null == em) {
108                         // Abort here
109                         throw new NullPointerException("em is null"); //NOI18N
110                 } else if (!em.isOpen()) {
111                         // Not open
112                         throw new IllegalStateException("Entity manager is closed.");
113                 }
114
115                 // Generate fake order instance
116                 Orderable orderable = null;
117
118                 // Generate access keyy
119                 String accessKey = null;
120
121                 // Default is found
122                 boolean isFound = true;
123
124                 // Is the number used?
125                 while (isFound) {
126                         // Both number parts
127                         String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1_000_000)), em, customer.getCustomerNumber());
128
129                         // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
130                         accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
131
132                         // Try this
133                         try {
134                                 // Get reference
135                                 orderable = em.getReference(ShopOrder.class, accessKey);
136                         } catch (final EntityNotFoundException ex) {
137                                 // Not found, so abort loop here
138                                 isFound = false;
139                         }
140                 }
141
142                 // Trace message
143                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
144                 // Found one
145                 return accessKey;
146         }
147 }