]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jshopcore/model/customer/CustomerUtils.java
e712d771953cb3555ba7d4eb5ed2bd3c2f33e765
[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.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import org.mxchange.jcore.BaseFrameworkSystem;
25 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
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          * @param logger Logger 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 Connection connection, final LoggerBeanLocal logger) throws SQLException {
49                 // logger cannot be null
50                 if (null == logger) {
51                         // Abort here
52                         throw new NullPointerException("logger is null");
53                 }
54
55                 // Trace message
56                 logger.logTrace(MessageFormat.format("generateCustomerNumber: connection={0},logger={1} - CALLED!", connection, logger));
57
58                 // connection cannot be null
59                 if (null == connection) {
60                         // Abort here
61                         throw new NullPointerException("connection is null");
62                 }
63
64                 // Prepare statement
65                 PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `customer` WHERE `customer_number` = ? LIMIT 1");
66
67                 // Generate number
68                 String customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" +  Long.toString(Math.round(Math.random() * 1000));
69
70                 // Default is found
71                 boolean isFound = true;
72
73                 // Is the number used?
74                 while (isFound) {
75                         // Debug message
76                         logger.logDebug(MessageFormat.format("generateCustomerNumber: customerNumber={0}", customerNumber));
77
78                         // Insert customer number
79                         statement.setString(0, customerNumber);
80
81                         // Find it
82                         statement.execute();
83
84                         // Get result
85                         ResultSet result = statement.getResultSet();
86
87                         // Rewind it
88                         result.beforeFirst();
89
90                         // Found a record?
91                         if (result.isLast()) {
92                                 // Not found
93                                 isFound = false;
94                                 break;
95                         }
96
97                         // Generate new number
98                         customerNumber = Long.toString(Math.round(Math.random() * 100000)) + "-" +  Long.toString(Math.round(Math.random() * 1000));
99                 }
100
101                 // Trace message
102                 logger.logTrace(MessageFormat.format("generateCustomerNumber: customerNumber={0} - EXIT!", customerNumber));
103
104                 // Found one
105                 return customerNumber;
106         }
107 }