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