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