-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProcessService.java
120 lines (98 loc) · 4.4 KB
/
ProcessService.java
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
package com.cruru.process.service;
import com.cruru.advice.InternalServerException;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.process.controller.request.ProcessCreateRequest;
import com.cruru.process.controller.request.ProcessUpdateRequest;
import com.cruru.process.controller.response.ProcessSimpleResponse;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.ProcessType;
import com.cruru.process.domain.repository.ProcessRepository;
import com.cruru.process.exception.ProcessNotFoundException;
import com.cruru.process.exception.badrequest.ProcessCountException;
import com.cruru.process.exception.badrequest.ProcessDeleteFixedException;
import com.cruru.process.exception.badrequest.ProcessDeleteRemainingApplicantException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ProcessService {
private static final int MAX_PROCESS_COUNT = 5;
private static final int ZERO = 0;
private final ApplicantRepository applicantRepository;
private final ProcessRepository processRepository;
public ProcessSimpleResponse toProcessSimpleResponse(Process process) {
return new ProcessSimpleResponse(process.getId(), process.getName());
}
@Transactional
public void create(ProcessCreateRequest request, Dashboard dashboard) {
validateProcessCount(dashboard);
List<Process> processes = findAllByDashboard(dashboard);
rearrangeProcesses(request.sequence(), processes);
processRepository.save(toEvaluateProcess(request, dashboard));
}
private void validateProcessCount(Dashboard dashboard) {
int size = processRepository.countByDashboard(dashboard);
if (size >= MAX_PROCESS_COUNT) {
throw new ProcessCountException(MAX_PROCESS_COUNT);
}
}
public List<Process> findAllByDashboard(Dashboard dashboard) {
return processRepository.findAllByDashboard(dashboard);
}
private void rearrangeProcesses(int newProcessSequence, List<Process> processes) {
processes.stream()
.filter(process -> process.getSequence() >= newProcessSequence)
.forEach(Process::increaseSequenceNumber);
}
private Process toEvaluateProcess(ProcessCreateRequest request, Dashboard dashboard) {
return new Process(request.sequence(), request.name(), request.description(), ProcessType.EVALUATE, dashboard);
}
public List<Process> findAllByDashboard(Long dashboardId) {
return processRepository.findAllByDashboardId(dashboardId);
}
public Process findApplyProcessOnDashboard(Dashboard dashboard) {
List<Process> processes = findAllByDashboard(dashboard);
return processes.stream()
.filter(Process::isApplyType)
.findFirst()
.orElseThrow(InternalServerException::new);
}
@Transactional
public Process update(ProcessUpdateRequest request, long processId) {
Process process = findById(processId);
if (changeExists(request, process)) {
process.updateName(request.name());
process.updateDescription(request.description());
}
return process;
}
public Process findById(Long processId) {
return processRepository.findById(processId)
.orElseThrow(ProcessNotFoundException::new);
}
private boolean changeExists(ProcessUpdateRequest request, Process process) {
return !(request.name().equals(process.getName()) && request.description().equals(process.getDescription()));
}
@Transactional
public void delete(long processId) {
Process process = findById(processId);
validateFixedProcess(process);
validateApplicantRemains(process);
processRepository.deleteById(processId);
}
private void validateFixedProcess(Process process) {
if (process.isFixed()) {
throw new ProcessDeleteFixedException();
}
}
private void validateApplicantRemains(Process process) {
long applicantCount = applicantRepository.countByProcess(process);
if (applicantCount > ZERO) {
throw new ProcessDeleteRemainingApplicantException();
}
}
}