-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathEmployeeSignInCommand.java
More file actions
112 lines (98 loc) · 3.54 KB
/
EmployeeSignInCommand.java
File metadata and controls
112 lines (98 loc) · 3.54 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
package edu.uark.registerapp.commands.employees;
import java.util.Arrays;
import java.util.Optional;
import javax.transaction.Transactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.uark.registerapp.commands.ResultCommandInterface;
import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper;
import edu.uark.registerapp.commands.exceptions.UnauthorizedException;
import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException;
import edu.uark.registerapp.models.api.Employee;
import edu.uark.registerapp.models.api.EmployeeSignIn;
import edu.uark.registerapp.models.entities.ActiveUserEntity;
import edu.uark.registerapp.models.entities.EmployeeEntity;
import edu.uark.registerapp.models.repositories.ActiveUserRepository;
import edu.uark.registerapp.models.repositories.EmployeeRepository;
@Service
public class EmployeeSignInCommand implements ResultCommandInterface<Employee> {
@Override
public Employee execute() {
this.validateProperties();
return new Employee(this.SignInEmployee());
}
// Helper methods
private void validateProperties() {
//check if employeeID is blank
if (StringUtils.isBlank(this.employeeSignIn.getEmployeeId())) {
throw new UnprocessableEntityException("employee ID");
}
//check if you can get only a number out of the ID
try {
Integer.parseInt(this.employeeSignIn.getEmployeeId());
} catch (final NumberFormatException e) {
//throw an exception if the ID is not just numbers
throw new UnprocessableEntityException("employee ID");
}
if (StringUtils.isBlank(this.employeeSignIn.getPassword())) {
//throw exception if the password is blank
throw new UnprocessableEntityException("password");
}
}
@Transactional
private EmployeeEntity SignInEmployee() {
//tries to find existing employee using the id
final Optional<EmployeeEntity> employeeEntity =
this.employeeRepository.findByEmployeeId(
Integer.parseInt(this.employeeSignIn.getEmployeeId()));
//verifies ifthe employee exists
if (!employeeEntity.isPresent()
|| !Arrays.equals(
employeeEntity.get().getPassword(),
EmployeeHelper.hashPassword(this.employeeSignIn.getPassword()))
) {
throw new UnauthorizedException();
}
final Optional<ActiveUserEntity> activeUserEntity =
this.activeUserRepository
.findByEmployeeId(employeeEntity.get().getId());
if (!activeUserEntity.isPresent()) {
this.activeUserRepository.save(
(new ActiveUserEntity())
.setSessionKey(this.sessionId)
.setEmployeeId(employeeEntity.get().getId())
.setClassification(
employeeEntity.get().getClassification())
.setName(
employeeEntity.get().getFirstName()
.concat(" ")
.concat(employeeEntity.get().getLastName())));
} else {
this.activeUserRepository.save(
activeUserEntity.get().setSessionKey(this.sessionId));
}
return employeeEntity.get();
}
// Properties
private EmployeeSignIn employeeSignIn;
public EmployeeSignIn getEmployeeSignIn() {
return this.employeeSignIn;
}
public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn) {
this.employeeSignIn = employeeSignIn;
return this;
}
private String sessionId;
public String getSessionId() {
return this.sessionId;
}
public EmployeeSignInCommand setSessionId(final String sessionId) {
this.sessionId = sessionId;
return this;
}
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private ActiveUserRepository activeUserRepository;
}