-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathEmployeeSignInCommand.java
More file actions
99 lines (79 loc) · 3.56 KB
/
EmployeeSignInCommand.java
File metadata and controls
99 lines (79 loc) · 3.56 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
package edu.uark.registerapp.commands.employees;
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.ActiveUesrRepository;
import edu.uark.registerapp.models.repositories.EmployeeRepository;
import java.util.Arrays;
import java.util.Optional;
import javax.transaction.Transactional;
@Service
public class EmployeeSignInCommand implements ResultCommandInterface<Employee>{
@Override
public Employee execute(){
this.validateProperties();
return new Employee(this.SignInEmployee());
}
private void validateProperties(){
if(StringUtils.isBlank(this.employeeSignIn.getEmployeeId()){
throw new UnprocessableEntityException("Employee ID");
}
try{
Integer.parseInt(this.employeeSignIn.getEmployeeId());
} catch(final NumberFormatException e){
throw new UnprocessableEntityException("Employee ID");
} if(StringUtils.isBlank(this.employeeSignin.getPassword()){
throw new UnprocessableEntityException("Password");
}
}
@Transactional
private EmployeeEntity SignInEmployee(){
final Optional<EmployeeEntity> employeeEntity =
this.employeeRepository.findByEmployeeId(
Integer.parseInt(this.employeeSignIn.getEmployeeId()));
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(!activeUserRepository.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();
}
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;
}