forked from ZipCodeCore/FinalGroupProjects-Java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomerServices.java
More file actions
216 lines (194 loc) · 9.45 KB
/
CustomerServices.java
File metadata and controls
216 lines (194 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package runner.services;
import com.mifmif.common.regex.Generex;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import runner.entities.Account;
import runner.entities.Address;
import runner.entities.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import runner.entities.Login;
import runner.repositories.CustomerRepo;
import runner.repositories.LoginRepo;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
public class CustomerServices {
//object for logging information,warning,error
private final static Logger loggerService = Logger.getLogger(CustomerServices.class.getName());
private CustomerRepo customerRepo;
//Autowired the customerService
@Autowired
public CustomerServices(CustomerRepo customerRepo) {
loggerService.log(Level.INFO, "The repository for customer has been autowired to services");
this.customerRepo = customerRepo;
}
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
//save the customer in the DB
public Customer createCustomer(Customer customer) {
loggerService.log(Level.INFO, "The customer information is being checked");
if(!checkLogin(customer.getLogin())) {
customer.getLogin().setPassword(bCryptPasswordEncoder.encode(customer.getLogin().getPassword())); //encrypts the password before saving
customer.setSocialSecurity(bCryptPasswordEncoder.encode(customer.getSocialSecurity()));
//get the accounts from the customer and set the customer id in accounts table before saving the Customer.
customer.getAccounts().stream().forEach(account -> account.setCustomer(customer));
loggerService.log(Level.INFO, "The customer information is being saved" + checkLogin(customer.getLogin()));
return (Customer) customerRepo.save(customer);
}
return null;
}
public Boolean checkLogin(Login login) {
List<String> logins= customerRepo.findAllLoginsNative();
long count = logins.stream().filter(name -> name.equalsIgnoreCase(login.getUsername())).count();
return count!=0 ? true:false;
}
public Customer readCustomer(Long id) {
loggerService.log(Level.INFO, "The customer information is being read");
Customer customer = customerRepo.findCustomerById(id);
if (customer != null) {
loggerService.log(Level.INFO, "The customer is found and being returned");
return customer;
} else {
loggerService.log(Level.WARNING, "The customer could not be found, returned null");
return null;
}
}
public Customer readCustomerByLogin(String name) {
loggerService.log(Level.INFO, "The customer information is being read");
// Login login =
Customer customer = customerRepo.findCustomerByLoginUsername(name);
if (customer != null) {
loggerService.log(Level.INFO, "The customer is found and being returned");
return customer;
} else {
loggerService.log(Level.WARNING, "The customer could not be found, returned null");
return null;
}
}
public int deleteCustomer(Long id) {
Customer customer = customerRepo.findCustomerById(id);
Set<Account> accounts; // To collect all accounts belonging to this customer
List<Account> result ; //To collect accounts with balance greater than 0
if (customer != null) {
accounts=getAllAccounts(id);
result = accounts.stream().filter((account) -> account.getBalance() > 0).collect(Collectors.toList());
loggerService.log(Level.INFO, "Account list size " + result.size());
if(result.size()==0 && accounts.size()!=0) {
customerRepo.delete(customer);
loggerService.log(Level.INFO, "User has been deleted as account balance for all accounts =0");
return 0;
}
else if(result.size()>0) {
loggerService.log(Level.WARNING, "The customer had a balance greater than 0 and could not remove the account # " + id);
return 2;
}
}
return 1;
}
public Customer updateCustomer(Long id, Customer customer) throws Exception {
loggerService.log(Level.INFO, "Finding the customer to be updated");
Customer customerFromDB = customerRepo.findCustomerById(id);
Set<Account> accountSetFromDB ;
if (customerFromDB != null) {
loggerService.log(Level.INFO, "Customer with id to be updated found " + customerFromDB.getId());
customer.getAddress().setId(customerFromDB.getAddress().getId());
customer.setId(customerFromDB.getId());
accountSetFromDB = customerFromDB.getAccounts();
//Once the existing accounts are added , we will add more accounts
for (Account account : customer.getAccounts()) {
account.setCustomer(customerFromDB);
account.setEncryptedUrl(generateRandomUrl());
accountSetFromDB.add(account);
}
customer.setAccounts(accountSetFromDB);
customerRepo.save(customer);
loggerService.log(Level.INFO, "Customer with Id " + customerFromDB.getId() + "has been updated");
return customerFromDB;
} else {
loggerService.log(Level.WARNING, "Customer with Id " + id + "not found in db");
throw new Exception("id not found to be udapted");
}
}
public int updateCustomerPhoneNumber(Long id, String phone) throws ParseException {
loggerService.log(Level.INFO, "Finding the customer to be updated");
Customer customer = customerRepo.findCustomerById(id);
Pattern pattern = Pattern.compile("([0-9]{10}|\\(?[0-9]{3}\\)?[- ]{0,1}[0-9]{3}[- ]{0,1}[0-9]{4})");
Matcher matcher = pattern.matcher(phone);
if (matcher.matches()) {
if (customer != null) {
loggerService.log(Level.INFO, "Customer with id " + customer.getId() + " found to be updated");
customer.setPhoneNumber(phone);
customerRepo.save(customer);
loggerService.log(Level.INFO, "User with Id " + customer.getId() + " phone number has been updated");
return 0;
} else {
return 1;
}
}
return 2;
}
public int updateCustomerEmail(Long id, String email) {
//return 0 when updated , 1 is customer not found and 2 if value does not match the REGEX
loggerService.log(Level.INFO, "Finding the user to be updated");
Customer customer = customerRepo.findCustomerById(id);
Pattern patternEmail = Pattern.compile("^([\\w-\\.]+){1,64}@([\\w&&[^_]]+){2,255}.[a-z]{2,}$", Pattern.CASE_INSENSITIVE);
Matcher matcher = patternEmail.matcher(email);
if (matcher.matches()) {
if (customer != null) {
loggerService.log(Level.INFO, "customer with id " + customer.getId() + " found to be updated");
customer.setEmail(email);
customerRepo.save(customer);
loggerService.log(Level.INFO, "customer with Id " + customer.getId() + " email id has been updated");
return 0;
} else {
return 1;
}
}
return 2;
}
public Customer updateCustomerAddress(Long id, Address address) {
loggerService.log(Level.INFO, "Finding the customer to be updated");
Customer customer = customerRepo.findCustomerById(id);
if (customer != null) {
address.setId(customer.getAddress().getId());
loggerService.log(Level.INFO, "customer with id "+id+ " found to be updated");
customer.setAddress(address);
customerRepo.save(customer);
loggerService.log(Level.INFO, "customer with Id " + customer.getId() + " address has been updated");
return customer;
}
return null;
}
//Delete if not needed
public Set<Account> getAllAccounts(Long id) {
loggerService.log(Level.INFO, "Finding the customer to get all accounts");
Customer customer = customerRepo.findCustomerById(id);
if (customer != null) {
loggerService.log(Level.INFO, "Accounts belonging to "+id+ " use has been found ,accounts are being returned");
return customer.getAccounts();
}
return null;
}
//superceded by Set<Account> findAccountsByCustomer_LoginUsername (String login) in AccountRepo
/* public Set<Account> getAllAccounts(String username) {
loggerService.log(Level.INFO, "Finding the customer to get all accounts");
Login login = loginRepo.findLoginByUsername(username);
Customer customer = customerRepo.findCustomerById(login.getId());
if (customer != null) {
loggerService.log(Level.INFO, "Accounts belonging to "+login.getId()+ " use has been found ,accounts are being returned");
return customer.getAccounts();
}
return null;
}*/
//generate 35-40 random characters
public String generateRandomUrl() {
Generex generex = new Generex("[A-Za-z0-9]{35,40}");
String randomString = generex.random();
return randomString;
}
}