Fixed duplicate label validation

This commit is contained in:
Dominique Eifländer 2021-09-28 10:37:44 +02:00
parent bed6dec605
commit 980d370e4d

View File

@ -119,7 +119,7 @@ public class DictionaryController implements DictionaryResource {
if (typeValueRequest.getLabel() != null) { if (typeValueRequest.getLabel() != null) {
checkForDuplicateLabels(typeResult.getDossierTemplateId(), typeResult.getDossierId(), typeId, typeValueRequest.getLabel()); checkForDuplicateLabels(typeResult.getDossierTemplateId(), typeResult.getDossierId(), typeValueRequest.getType(), typeValueRequest.getLabel());
} }
@ -154,11 +154,12 @@ public class DictionaryController implements DictionaryResource {
typeRequest.setLabel(label); typeRequest.setLabel(label);
} }
checkForDuplicateLabels(typeRequest.getDossierTemplateId(), typeRequest.getDossierId(), typeRequest.getId(), typeRequest.getLabel()); checkForDuplicateLabels(typeRequest.getDossierTemplateId(), typeRequest.getDossierId(), typeRequest.getType(), typeRequest.getLabel());
if (dictionaryPersistenceService.getCumulatedTypes(typeRequest.getDossierTemplateId(), typeRequest.getDossierId()) if (dictionaryPersistenceService.getCumulatedTypes(typeRequest.getDossierTemplateId(), typeRequest.getDossierId())
.stream() .stream()
.anyMatch(typeResult -> typeResult.getType().equals(typeRequest.getType()))) { .anyMatch(typeResult -> typeRequest.getDossierId() != null && typeResult.getDossierId() != null && typeRequest.getDossierId().equals(typeResult.getDossierId()) && typeRequest.getType().equals(typeResult.getType()) && typeRequest.getDossierTemplateId().equals(typeResult.getDossierTemplateId())
|| typeRequest.getType().equals(typeResult.getType()) && typeRequest.getDossierTemplateId().equals(typeResult.getDossierTemplateId()))) {
throw new ConflictException("The type already exists, could not be added again."); throw new ConflictException("The type already exists, could not be added again.");
} }
String color = typeRequest.getHexColor(); String color = typeRequest.getHexColor();
@ -276,11 +277,12 @@ public class DictionaryController implements DictionaryResource {
} }
private void checkForDuplicateLabels(String dossierTemplateId, String dossierId, String typeId, String labelToCheck) { private void checkForDuplicateLabels(String dossierTemplateId, String dossierId, String type, String labelToCheck) {
List<Type> typeResponse = dictionaryPersistenceService.getCumulatedTypes(dossierTemplateId, dossierId); List<Type> typeResponse = dictionaryPersistenceService.getCumulatedTypes(dossierTemplateId, dossierId);
for (Type res : typeResponse) { for (Type res : typeResponse) {
if (!typeId.equals(res.getId()) && labelToCheck.equals(res.getLabel())) { if (dossierId != null && res.getDossierId() != null && res.getDossierId().equals(dossierId) && !type.equals(res.getType()) && res.getDossierTemplateId().equals(dossierTemplateId) && dossierId.equals(dossierId) && labelToCheck.equals(res.getLabel())
|| !type.equals(res.getType()) && res.getDossierTemplateId().equals(dossierTemplateId) && labelToCheck.equals(res.getLabel())) {
throw new ConflictException("Label must be unique."); throw new ConflictException("Label must be unique.");
} }
} }