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.stereotype.Service;
|
9
|
import vldc.aswi.dao.AssemblyRepository;
|
10
|
import vldc.aswi.dao.FunctionRepository;
|
11
|
import vldc.aswi.dao.LocationRepository;
|
12
|
import vldc.aswi.dao.OperatorRepository;
|
13
|
import vldc.aswi.dao.parameter.ParameterInConfigurationRepository;
|
14
|
import vldc.aswi.dao.parameter.ParameterRepository;
|
15
|
import vldc.aswi.dao.parameter.ParameterTypeRepository;
|
16
|
import vldc.aswi.dao.parameter.ParameterValueRepository;
|
17
|
import vldc.aswi.domain.*;
|
18
|
import vldc.aswi.domain.parameter.Parameter;
|
19
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
20
|
import vldc.aswi.domain.parameter.ParameterValue;
|
21
|
|
22
|
import javax.transaction.Transactional;
|
23
|
import java.util.ArrayList;
|
24
|
import java.util.LinkedList;
|
25
|
import java.util.List;
|
26
|
|
27
|
/**
|
28
|
* Manager for Parameter.
|
29
|
*/
|
30
|
@Service
|
31
|
@Slf4j
|
32
|
public class ParameterManagerImpl implements ParameterManager {
|
33
|
|
34
|
/** Autowired parameter repository. */
|
35
|
@Autowired
|
36
|
private ParameterRepository parameterRepository;
|
37
|
|
38
|
/** Autowired assembly repository. */
|
39
|
@Autowired
|
40
|
private AssemblyRepository assemblyRepository;
|
41
|
|
42
|
/** Autowired parameterType repository. */
|
43
|
@Autowired
|
44
|
private ParameterTypeRepository parameterTypeRepository;
|
45
|
|
46
|
/** Autowired location repository. */
|
47
|
@Autowired
|
48
|
private LocationRepository locationRepository;
|
49
|
|
50
|
/** Autowired function repository. */
|
51
|
@Autowired
|
52
|
private FunctionRepository functionRepository;
|
53
|
|
54
|
/** Autowired operator repository. */
|
55
|
@Autowired
|
56
|
private OperatorRepository operatorRepository;
|
57
|
|
58
|
/** Autowired parameterValue repository. */
|
59
|
@Autowired
|
60
|
private ParameterValueRepository parameterValueRepository;
|
61
|
|
62
|
/** Autowired parameter in configuration manager. */
|
63
|
@Autowired
|
64
|
private ParameterInConfigurationManager parameterInConfigurationManager;
|
65
|
|
66
|
/**
|
67
|
* Initialization setup for parameter manager. Check if parameter repository contains
|
68
|
* records and if not, initialize default values.
|
69
|
*/
|
70
|
@EventListener(classes = {
|
71
|
ContextRefreshedEvent.class
|
72
|
})
|
73
|
@Order(1)
|
74
|
@Transactional
|
75
|
public void setup() {
|
76
|
if (this.parameterRepository.count() == 0) {
|
77
|
log.info("No parameter present, creating items.");
|
78
|
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Get all parameters from database.
|
84
|
* @return List of parameters.
|
85
|
*/
|
86
|
@Override
|
87
|
public List<Parameter> getParameters() {
|
88
|
List<Parameter> retVal = new LinkedList<>();
|
89
|
this.parameterRepository.findAll().forEach(retVal::add);
|
90
|
return retVal;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Add new parameter into database.
|
95
|
* @param assemblyId - Assembly in which parameter is presented.
|
96
|
* @param parameterValuesList - Parameter values.
|
97
|
*/
|
98
|
@Override
|
99
|
public void addParameters(Long assemblyId, List<Parameter> parameterValuesList) {
|
100
|
Assembly assembly = this.assemblyRepository.getById(assemblyId);
|
101
|
if (parameterValuesList == null) return;
|
102
|
|
103
|
for (Parameter parameterValues : parameterValuesList) {
|
104
|
// Create new parameter and assign him all values.
|
105
|
Parameter parameter = new Parameter();
|
106
|
parameter.setName(parameterValues.getName());
|
107
|
parameter.setNameOfSelect(parameterValues.getNameOfSelect());
|
108
|
parameter.setAssembly(assembly);
|
109
|
parameter.setParameterOrder(parameterValues.getParameterOrder());
|
110
|
parameter.setParameterType(this.parameterTypeRepository.findByName(parameterValues.getParameterType().getName()));
|
111
|
parameter.setDefaultValue(parameterValues.getDefaultValue());
|
112
|
|
113
|
List<Location> locationList = new LinkedList<>();
|
114
|
List<Function> functionList = new LinkedList<>();
|
115
|
List<Operator> operatorList = new LinkedList<>();
|
116
|
List<ParameterValue> valueList = new LinkedList<>();
|
117
|
|
118
|
// Try to put all locations into list.
|
119
|
if (parameterValues.getLocations() != null && parameterValues.getLocations().size() > 0) {
|
120
|
for (Location location : parameterValues.getLocations()) {
|
121
|
locationList.add(this.locationRepository.getById(location.getId()));
|
122
|
}
|
123
|
}
|
124
|
|
125
|
// Try to put all functions into list.
|
126
|
if (parameterValues.getFunctions() != null && parameterValues.getFunctions().size() > 0) {
|
127
|
for (Function function : parameterValues.getFunctions()) {
|
128
|
functionList.add(this.functionRepository.getById(function.getId()));
|
129
|
}
|
130
|
}
|
131
|
|
132
|
// Try to put all operators into list.
|
133
|
if (parameterValues.getOperators() != null && parameterValues.getOperators().size() > 0) {
|
134
|
for (Operator operator : parameterValues.getOperators()) {
|
135
|
operatorList.add(this.operatorRepository.getById(operator.getId()));
|
136
|
}
|
137
|
}
|
138
|
|
139
|
if (parameter.getParameterType().getName().equals("Výčet")) {
|
140
|
// Try to put all values into list.
|
141
|
if (parameterValues.getParameterValues() != null && parameterValues.getParameterValues().size() > 0) {
|
142
|
for (ParameterValue parameterValue : parameterValues.getParameterValues()) {
|
143
|
valueList.add(this.parameterValueRepository.getById(parameterValue.getId()));
|
144
|
}
|
145
|
}
|
146
|
|
147
|
parameter.setParameterValues(valueList);
|
148
|
}
|
149
|
|
150
|
// Set lists of values into parameter.
|
151
|
parameter.setLocations(locationList);
|
152
|
parameter.setFunctions(functionList);
|
153
|
parameter.setOperators(operatorList);
|
154
|
|
155
|
// Insert parameter into database.
|
156
|
this.parameterRepository.save(parameter);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Update parameter - remove deleted parameters, add new parameters, update updated parameters.
|
162
|
* @param assemblyId - Assembly in which parameter is presented.
|
163
|
* @param newAssemblyParameters - List of parameters.
|
164
|
*/
|
165
|
@Override
|
166
|
public void updateParameters(Long assemblyId, List<Parameter> newAssemblyParameters) {
|
167
|
Assembly assembly = this.assemblyRepository.getById(assemblyId);
|
168
|
List<Parameter> currentAssemblyParameters = assembly.getParameters();
|
169
|
|
170
|
if (newAssemblyParameters != null) {
|
171
|
// Remove removed parameters.
|
172
|
newAssemblyParameters = removeRemovedParametersFromList(newAssemblyParameters);
|
173
|
|
174
|
// Find new parameters.
|
175
|
List<Parameter> currentAssemblyParametersNew = new LinkedList<>(currentAssemblyParameters);
|
176
|
List<Parameter> newAssemblyParametersNew = new LinkedList<>(newAssemblyParameters);
|
177
|
newAssemblyParametersNew.removeAll(currentAssemblyParametersNew);
|
178
|
|
179
|
// Find deleted parameters.
|
180
|
List<Parameter> currentAssemblyParametersDeleted = new LinkedList<>(currentAssemblyParameters);
|
181
|
List<Parameter> newAssemblyParametersDeleted = new LinkedList<>(newAssemblyParameters);
|
182
|
currentAssemblyParametersDeleted.removeAll(newAssemblyParametersDeleted);
|
183
|
|
184
|
// Find duplicate elements - for updating.
|
185
|
List<Parameter> currentAssemblyParametersUpdated = new LinkedList<>(currentAssemblyParameters);
|
186
|
List<Parameter> newAssemblyParametersUpdated = new LinkedList<>(newAssemblyParameters);
|
187
|
newAssemblyParametersUpdated.retainAll(currentAssemblyParametersUpdated);
|
188
|
|
189
|
// If no parameter has been deleted or added, then updating is unnecessary.
|
190
|
if (newAssemblyParametersNew.size() == 0 && currentAssemblyParametersDeleted.size() == 0 && newAssemblyParametersUpdated.size() == 0)
|
191
|
return;
|
192
|
|
193
|
// Remove deleted parameters.
|
194
|
for (Parameter parameterWithValues : currentAssemblyParametersDeleted) {
|
195
|
deleteParameter(parameterWithValues.getId());
|
196
|
}
|
197
|
|
198
|
// Update rest parameters.
|
199
|
updateParameters(newAssemblyParametersUpdated);
|
200
|
|
201
|
// Add new parameters
|
202
|
addParameters(assemblyId, newAssemblyParametersNew);
|
203
|
|
204
|
assembly.setParameters(this.parameterRepository.getByAssemblyId(assemblyId));
|
205
|
}
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* Remove removed parameters by web application.
|
210
|
* Web page returns list of parameters, even those, which was removed, but those parameters
|
211
|
* are empty object (null values in every attribute), so we need to remove them from list.
|
212
|
* @param parameterValuesList - List of parameters from web.
|
213
|
* @return Filtered list of parameters.
|
214
|
*/
|
215
|
private List<Parameter> removeRemovedParametersFromList(List<Parameter> parameterValuesList) {
|
216
|
List<Parameter> filteredParametersWithValues = new ArrayList<Parameter>();
|
217
|
|
218
|
for (Parameter parameterWithValues : parameterValuesList) {
|
219
|
// Because name of parameter is required, then we know that parameter
|
220
|
// with name == null is just empty object.
|
221
|
if (parameterWithValues.getName() != null) {
|
222
|
filteredParametersWithValues.add(parameterWithValues);
|
223
|
}
|
224
|
}
|
225
|
|
226
|
return filteredParametersWithValues;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Update updated parameters.
|
231
|
* @param parameterValuesList - List of updated parameters.
|
232
|
*/
|
233
|
private void updateParameters(List<Parameter> parameterValuesList) {
|
234
|
for (Parameter parameterWithValues : parameterValuesList) {
|
235
|
List<ParameterValue> values = new ArrayList<>(parameterWithValues.getParameterValues());
|
236
|
|
237
|
// Get current parameter from database.
|
238
|
Parameter parameter = this.parameterRepository.getById(parameterWithValues.getId());
|
239
|
// Update current parameter values.
|
240
|
parameter.setName(parameterWithValues.getName());
|
241
|
parameter.setParameterOrder(parameterWithValues.getParameterOrder());
|
242
|
parameter.setNameOfSelect(parameterWithValues.getNameOfSelect());
|
243
|
parameter.setAssembly(this.assemblyRepository.getById(parameterWithValues.getAssembly().getId()));
|
244
|
parameter.setParameterType(this.parameterTypeRepository.getById(parameterWithValues.getParameterType().getId()));
|
245
|
parameter.setDefaultValue(parameterWithValues.getDefaultValue());
|
246
|
|
247
|
// Insert changes into database.
|
248
|
this.parameterRepository.save(parameter);
|
249
|
|
250
|
// Update parameter values, locations, functions and operators.
|
251
|
if (parameter.getParameterType().getName().equals("Výčet")) {
|
252
|
updateParameterValues(parameterWithValues.getId(), values);
|
253
|
}
|
254
|
|
255
|
updateParameterLocations(parameterWithValues.getId(), parameterWithValues.getLocations());
|
256
|
updateParameterFunctions(parameterWithValues.getId(), parameterWithValues.getFunctions());
|
257
|
updateParameterOperators(parameterWithValues.getId(), parameterWithValues.getOperators());
|
258
|
}
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Update current parameter locations.
|
263
|
* @param id - Id of parameter.
|
264
|
* @param updatedLocations - List of locations.
|
265
|
*/
|
266
|
private void updateParameterLocations(Long id, List<Location> updatedLocations) {
|
267
|
Parameter parameter = this.parameterRepository.getById(id);
|
268
|
List<Location> currentLocations = parameter.getLocations();
|
269
|
|
270
|
// Get new locations.
|
271
|
List<Location> currentLocationsNew = new LinkedList<>(currentLocations);
|
272
|
List<Location> updatedLocationsNew = new LinkedList<>(updatedLocations);
|
273
|
updatedLocationsNew.removeAll(currentLocationsNew);
|
274
|
|
275
|
// Get deleted locations.
|
276
|
List<Location> currentLocationsDeleted = new LinkedList<>(currentLocations);
|
277
|
List<Location> updatedLocationsDeleted = new LinkedList<>(updatedLocations);
|
278
|
currentLocationsDeleted.removeAll(updatedLocationsDeleted);
|
279
|
|
280
|
// If there is no change, do not update.
|
281
|
if (updatedLocationsNew.size() == 0 && currentLocationsDeleted.size() == 0) return;
|
282
|
|
283
|
// Add new locations.
|
284
|
for (Location newLocation : updatedLocationsNew) {
|
285
|
currentLocations.add(this.locationRepository.getById(newLocation.getId()));
|
286
|
}
|
287
|
|
288
|
// Remove deleted locations.
|
289
|
currentLocations.removeAll(currentLocationsDeleted);
|
290
|
|
291
|
// Insert changes into database.
|
292
|
this.parameterRepository.save(parameter);
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* Update current parameter functions.
|
297
|
* @param id - Id of parameter.
|
298
|
* @param updatedFunctions - List of functions.
|
299
|
*/
|
300
|
private void updateParameterFunctions(Long id, List<Function> updatedFunctions) {
|
301
|
Parameter parameter = this.parameterRepository.getById(id);
|
302
|
List<Function> currentFunctions = parameter.getFunctions();
|
303
|
|
304
|
// Get new functions.
|
305
|
List<Function> currentFunctionsNew = new LinkedList<>(currentFunctions);
|
306
|
List<Function> updatedFunctionsNew = new LinkedList<>(updatedFunctions);
|
307
|
updatedFunctionsNew.removeAll(currentFunctionsNew);
|
308
|
|
309
|
// Get deleted functions.
|
310
|
List<Function> currentFunctionsDeleted = new LinkedList<>(currentFunctions);
|
311
|
List<Function> updatedFunctionsDeleted = new LinkedList<>(updatedFunctions);
|
312
|
currentFunctionsDeleted.removeAll(updatedFunctionsDeleted);
|
313
|
|
314
|
// If there is no change, do not update.
|
315
|
if (updatedFunctionsNew.size() == 0 && currentFunctionsDeleted.size() == 0) return;
|
316
|
|
317
|
// Add new functions.
|
318
|
for (Function newFunction : updatedFunctionsNew) {
|
319
|
currentFunctions.add(this.functionRepository.getById(newFunction.getId()));
|
320
|
}
|
321
|
|
322
|
// Delete removed functions.
|
323
|
currentFunctions.removeAll(currentFunctionsDeleted);
|
324
|
|
325
|
// Insert changes into database.
|
326
|
this.parameterRepository.save(parameter);
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* Update current parameter operators.
|
331
|
* @param id - Id of parameter.
|
332
|
* @param updatedOperators - List of operators.
|
333
|
*/
|
334
|
private void updateParameterOperators(Long id, List<Operator> updatedOperators) {
|
335
|
Parameter parameter = this.parameterRepository.getById(id);
|
336
|
List<Operator> currentOperators = parameter.getOperators();
|
337
|
|
338
|
// Get new operators.
|
339
|
List<Operator> currentOperatorsNew = new LinkedList<>(currentOperators);
|
340
|
List<Operator> updatedOperatorsNew = new LinkedList<>(updatedOperators);
|
341
|
updatedOperatorsNew.removeAll(currentOperatorsNew);
|
342
|
|
343
|
// Get deleted operators.
|
344
|
List<Operator> currentOperatorsDeleted = new LinkedList<>(currentOperators);
|
345
|
List<Operator> updatedOperatorsDeleted = new LinkedList<>(updatedOperators);
|
346
|
currentOperatorsDeleted.removeAll(updatedOperatorsDeleted);
|
347
|
|
348
|
// If there is no changes, do not update.
|
349
|
if (updatedOperatorsNew.size() == 0 && currentOperatorsDeleted.size() == 0) return;
|
350
|
|
351
|
// Insert new operators.
|
352
|
for (Operator newOperator : updatedOperatorsNew) {
|
353
|
currentOperators.add(this.operatorRepository.getById(newOperator.getId()));
|
354
|
}
|
355
|
|
356
|
// Delete removed operators.
|
357
|
currentOperators.removeAll(currentOperatorsDeleted);
|
358
|
|
359
|
// Insert changes into database.
|
360
|
this.parameterRepository.save(parameter);
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* Update current parameter values.
|
365
|
* @param parameterId - ID of parameter.
|
366
|
* @param newParameterValues - List of parameter values.
|
367
|
*/
|
368
|
private void updateParameterValues(Long parameterId, List<ParameterValue> newParameterValues) {
|
369
|
Parameter parameter = this.parameterRepository.getById(parameterId);
|
370
|
List<ParameterValue> currentParameterValues = parameter.getParameterValues();
|
371
|
|
372
|
// Get new parameter values.
|
373
|
List<ParameterValue> currentParameterValuesNew = new LinkedList<>(currentParameterValues);
|
374
|
List<ParameterValue> newParameterValuesNew = new LinkedList<>(newParameterValues);
|
375
|
newParameterValuesNew.removeAll(currentParameterValuesNew);
|
376
|
|
377
|
// Get deleted parameter values.
|
378
|
List<ParameterValue> currentParameterValuesDeleted = new LinkedList<>(currentParameterValues);
|
379
|
List<ParameterValue> newParameterValuesDeleted = new LinkedList<>(newParameterValues);
|
380
|
currentParameterValuesDeleted.removeAll(newParameterValuesDeleted);
|
381
|
|
382
|
// If there is no changes, do not update.
|
383
|
if (newParameterValuesNew.size() == 0 && currentParameterValuesDeleted.size() == 0) return;
|
384
|
|
385
|
// Delete removed parameter values.
|
386
|
for (ParameterValue parameterValue : currentParameterValuesDeleted) {
|
387
|
this.parameterValueRepository.delete(parameterValue);
|
388
|
}
|
389
|
|
390
|
// Add new parameter values.
|
391
|
addParameterValueValues(parameterId, newParameterValuesNew);
|
392
|
}
|
393
|
|
394
|
/**
|
395
|
* Add new parameter values.
|
396
|
* @param parameterId - ID of parameter.
|
397
|
* @param newParameterValuesNew - List of new parameter values.
|
398
|
*/
|
399
|
private void addParameterValueValues(Long parameterId, List<ParameterValue> newParameterValuesNew) {
|
400
|
Parameter parameter = this.parameterRepository.getById(parameterId);
|
401
|
|
402
|
for (ParameterValue parameterValueValues : newParameterValuesNew) {
|
403
|
ParameterValue parameterValue = new ParameterValue();
|
404
|
parameterValue.setValue(parameterValueValues.getValue());
|
405
|
parameterValue.setParameter(parameter);
|
406
|
|
407
|
// Insert new parameterValue into database.
|
408
|
this.parameterValueRepository.save(parameterValue);
|
409
|
}
|
410
|
}
|
411
|
|
412
|
/**
|
413
|
* Delete parameter by id.
|
414
|
* @param parameterId - ID of parameter.
|
415
|
* @return True if parameter was successfully deleted, false if not.
|
416
|
*/
|
417
|
private boolean deleteParameter(Long parameterId) {
|
418
|
try {
|
419
|
Parameter parameter = this.parameterRepository.getById(parameterId);
|
420
|
|
421
|
this.parameterInConfigurationManager.deleteParametersInConfiguration(parameterId, false);
|
422
|
|
423
|
// Delete removed parameter values.
|
424
|
for (ParameterValue parameterValue : parameter.getParameterValues()) {
|
425
|
this.parameterValueRepository.delete(parameterValue);
|
426
|
}
|
427
|
|
428
|
// Delete all functions from parameter in table 'Parametr_ma_Funkce'.
|
429
|
List<Function> functionList = new ArrayList<>(parameter.getFunctions());
|
430
|
for (Function function : functionList) {
|
431
|
parameter.removeFunction(function);
|
432
|
}
|
433
|
|
434
|
// Delete all operators from parameter in table 'Parametr_ma_Operator'.
|
435
|
List<Operator> operatorList = new ArrayList<>(parameter.getOperators());
|
436
|
for (Operator operator : operatorList) {
|
437
|
parameter.removeOperator(operator);
|
438
|
}
|
439
|
|
440
|
// Delete all locations from parameter in table 'Parametr_ma_Umisteni'.
|
441
|
List<Location> locationList = new ArrayList<>(parameter.getLocations());
|
442
|
for (Location location : locationList) {
|
443
|
parameter.removeLocation(location);
|
444
|
}
|
445
|
|
446
|
this.parameterRepository.delete(parameter);
|
447
|
|
448
|
return true;
|
449
|
}
|
450
|
catch (Exception e) {
|
451
|
return false;
|
452
|
}
|
453
|
}
|
454
|
|
455
|
/**
|
456
|
* Delete parameters by assembly id.
|
457
|
* @param assemblyId - ID of assembly.
|
458
|
* @return True if all parameters were successfully deleted, false if not.
|
459
|
*/
|
460
|
@Override
|
461
|
public boolean deleteParameters(Long assemblyId) {
|
462
|
try {
|
463
|
List<Parameter> parameters = this.parameterRepository.getByAssemblyId(assemblyId);
|
464
|
|
465
|
for (Parameter parameter : parameters) {
|
466
|
boolean success = deleteParameter(parameter.getId());
|
467
|
if (success == false) {
|
468
|
return false;
|
469
|
}
|
470
|
}
|
471
|
return true;
|
472
|
} catch (Exception e) {
|
473
|
return false;
|
474
|
}
|
475
|
}
|
476
|
}
|