1
|
package vldc.aswi.service.parameter;
|
2
|
|
3
|
import lombok.extern.slf4j.Slf4j;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.context.event.ContextRefreshedEvent;
|
6
|
import org.springframework.context.event.EventListener;
|
7
|
import org.springframework.core.annotation.Order;
|
8
|
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
9
|
import org.springframework.stereotype.Service;
|
10
|
import vldc.aswi.dao.parameter.ParameterInConfigurationRepository;
|
11
|
import vldc.aswi.domain.Function;
|
12
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
13
|
|
14
|
import javax.transaction.Transactional;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.LinkedList;
|
17
|
import java.util.List;
|
18
|
|
19
|
/**
|
20
|
* Manager for ParameterInConfiguration.
|
21
|
*/
|
22
|
@Service
|
23
|
@Slf4j
|
24
|
public class ParameterInConfigurationManagerImpl implements ParameterInConfigurationManager {
|
25
|
|
26
|
/** Autowired parameter in configuration repository. */
|
27
|
@Autowired
|
28
|
private ParameterInConfigurationRepository parameterInConfigurationRepository;
|
29
|
|
30
|
/**
|
31
|
* Initialization setup for parameter in configuration manager.
|
32
|
* Check if table "Parametr_konfigurace" exists.
|
33
|
*/
|
34
|
@EventListener(classes = {
|
35
|
ContextRefreshedEvent.class
|
36
|
})
|
37
|
@Order(4)
|
38
|
@Transactional
|
39
|
public void setup() {
|
40
|
try {
|
41
|
if (this.parameterInConfigurationRepository.count() == 0) {
|
42
|
// Just checking if table exists.
|
43
|
}
|
44
|
}
|
45
|
catch (InvalidDataAccessResourceUsageException e) {
|
46
|
log.error("Table \"Parametr_konfigurace\" did not exists in database!");
|
47
|
System.exit(1);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Get all parameter in configuration from database.
|
53
|
* @return List of parameter in configuration.
|
54
|
*/
|
55
|
@Override
|
56
|
public List<ParameterInConfiguration> getAllParameterInConfiguration() {
|
57
|
List<ParameterInConfiguration> retVal = new LinkedList<>();
|
58
|
this.parameterInConfigurationRepository.findAll().forEach(retVal::add);
|
59
|
return retVal;
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Delete all parameters in configuration by configuration id.
|
64
|
* @param id - ID of parameter / configuration.
|
65
|
* @return List of deleted parameters in configuration.
|
66
|
*/
|
67
|
@Override
|
68
|
public boolean deleteParametersInConfiguration(Long id, boolean configurationUsed) {
|
69
|
try {
|
70
|
List<ParameterInConfiguration> parameterInConfigurations = null;
|
71
|
|
72
|
if (configurationUsed) {
|
73
|
parameterInConfigurations = this.parameterInConfigurationRepository.getAllByConfigurationId(id);
|
74
|
}
|
75
|
else {
|
76
|
parameterInConfigurations = this.parameterInConfigurationRepository.getAllByParameterId(id);
|
77
|
}
|
78
|
for (ParameterInConfiguration parameterInConfiguration : parameterInConfigurations) {
|
79
|
boolean success = deleteParameterInConfiguration(parameterInConfiguration);
|
80
|
if (!success) {
|
81
|
return false;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
// Delete all parameters in configuration.
|
86
|
this.parameterInConfigurationRepository.deleteAll(parameterInConfigurations);
|
87
|
return true;
|
88
|
} catch (Exception e) {
|
89
|
return false;
|
90
|
}
|
91
|
}
|
92
|
|
93
|
private boolean deleteParameterInConfiguration(ParameterInConfiguration parameterInConfiguration) {
|
94
|
try {
|
95
|
|
96
|
// Delete all functions used in current parameter in table 'Parametr_Konfigurace_ma_Funkce'.
|
97
|
List<Function> functionList = new ArrayList<>(parameterInConfiguration.getFunctions());
|
98
|
for (Function function : functionList) {
|
99
|
parameterInConfiguration.removeFunction(function);
|
100
|
}
|
101
|
|
102
|
return true;
|
103
|
}
|
104
|
catch (Exception e) {
|
105
|
|
106
|
return false;
|
107
|
}
|
108
|
}
|
109
|
}
|