forked from snowcycle/RegisterApp-Java
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEmployeeUpdateCommand.java
More file actions
97 lines (85 loc) · 2.81 KB
/
EmployeeUpdateCommand.java
File metadata and controls
97 lines (85 loc) · 2.81 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
// This will update an employee
package edu.uark.registerapp.commands.employees;
import java.util.Optional;
import java.util.UUID;
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.exceptions.NotFoundException;
import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException;
import edu.uark.registerapp.models.api.Employee;
import edu.uark.registerapp.models.entities.EmployeeEntity;
import edu.uark.registerapp.models.enums.EmployeeClassification;
import edu.uark.registerapp.models.repositories.EmployeeRepository;
@Service
public class EmployeeUpdateCommand implements ResultCommandInterface<Employee>
{
@Override
public Employee execute()
{
this.validateProperties();
this.updateEmployeeEntity();
return this.apiEmployee;
}
// Helper methods
// Validate incoming Employee request object, names should not be blank
private void validateProperties()
{
if (StringUtils.isBlank(this.apiEmployee.getFirstName()))
{
throw new UnprocessableEntityException("first name");
}
if (StringUtils.isBlank(this.apiEmployee.getLastName()))
{
throw new UnprocessableEntityException("last name");
}
if (EmployeeClassification.map(this.apiEmployee.getClassification()) == EmployeeClassification.NOT_DEFINED)
{
throw new UnprocessableEntityException("classification");
}
}
@Transactional
private void updateEmployeeEntity()
{
// Query employee entity from database
final Optional<EmployeeEntity> queriedEmployeeEntity =
this.employeeRepository.findById(this.employeeId);
if (!queriedEmployeeEntity.isPresent())
{
// No record with the associated record ID exists in the database.
throw new NotFoundException("Employee");
}
// Update queried employee entity with date from incoming Employee object
this.apiEmployee = queriedEmployeeEntity.get().synchronize(this.apiEmployee);
// Save updated employee entity
this.employeeRepository.save(queriedEmployeeEntity.get());
}
// Properties
// Getters and Setters for the universally unique Identifiers of that tuple
private UUID employeeId;
public UUID getEmployeeId()
{
return this.employeeId;
}
public EmployeeUpdateCommand setEmployeeId(final UUID employeeId)
{
this.employeeId = employeeId;
return this;
}
// Getters and Setters for the API
// Return updated data of the Employee object
private Employee apiEmployee;
public Employee getApiEmployee()
{
return this.apiEmployee;
}
public EmployeeUpdateCommand setApiEmployee(final Employee apiEmployee)
{
this.apiEmployee = apiEmployee;
return this;
}
@Autowired
private EmployeeRepository employeeRepository;
}