]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jshopcore/model/customer/CustomerUtils.java
@JoinColumn and @Column doesn't work together
[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  *
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          *
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                 long part1 = 0;
70                 long part2 = 0;
71
72                 // Is the number used?
73                 while (isFound) {
74                         // Both number parts
75                         part1 = Math.round(Math.random() * 100000);
76                         part2 = Math.round(Math.random() * 1000);
77
78                         // Generate new number
79                         customerNumber = String.format("%s-%s", part1, part2); //NOI18N
80
81                         // Try it
82                         try {
83                                 // Get instance
84                                 customer = em.getReference(ShopCustomer.class, customerNumber);
85                         } catch (final EntityNotFoundException ex) {
86                                 // Not found
87                                 isFound = false;
88                         }
89                 }
90
91                 // Trace message
92                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
93
94                 // Found one
95                 return customerNumber;
96         }
97
98         /**
99          * Generates an unique access key.
100          *
101          * @param em Entity manager instance
102          * @param customer Customer instance
103          * @return An unique access key
104          */
105         public static String generateAccessKey (final EntityManager em, final Customer customer) {
106                 // Trace message
107                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: connection={0} - CALLED!", connection));
108
109                 // em cannot be null
110                 if (null == em) {
111                         // Abort here
112                         throw new NullPointerException("em is null"); //NOI18N
113                 } else if (!em.isOpen()) {
114                         // Not open
115                         throw new IllegalStateException("Entity manager is closed.");
116                 }
117
118                 // Generate fake order instance
119                 Orderable orderable = null;
120
121                 // Generate access keyy
122                 String accessKey = null;
123
124                 // Default is found
125                 boolean isFound = true;
126
127                 // Is the number used?
128                 while (isFound) {
129                         // Both number parts
130                         String randString = String.format("%s:%s:%s", Long.toHexString(Math.round(Math.random() * 1000000)), em, customer.getCustomerNumber());
131
132                         // Generate access key, use SHA512 hashing and BASE64-encoding for strong key generation
133                         accessKey = Base64.encodeBase64String(Sha2Crypt.sha512Crypt(randString.getBytes()).getBytes()).substring(0, 100);
134
135                         // Try this
136                         try {
137                                 // Get reference
138                                 orderable = em.getReference(ShopOrder.class, accessKey);
139                         } catch (final EntityNotFoundException ex) {
140                                 // Not found, so abort loop here
141                                 isFound = false;
142                         }
143                 }
144
145                 // Trace message
146                 // TODO: utils.getLogger().logTrace(MessageFormat.format("generateAccessKey: accessKey={0} - EXIT!", accessKey));
147
148                 // Found one
149                 return accessKey;
150         }
151 }