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