From 26855ad25011c8582d45f809688dff4838950d8b Mon Sep 17 00:00:00 2001 From: talig Date: Thu, 12 Apr 2018 10:45:02 +0300 Subject: Introduce unique value RESTs Change-Id: I6c62c8794fce2a8d42b0a8c57b8714d938c3063c Issue-ID: SDC-1177 Signed-off-by: talig --- .../org/openecomp/core/util/UniqueValueUtil.java | 66 ++++++++++++++-------- 1 file changed, 41 insertions(+), 25 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java index 36b76a0772..86c3d64d59 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java @@ -26,12 +26,13 @@ import org.openecomp.sdc.common.errors.ErrorCode; import java.util.Optional; public class UniqueValueUtil { + private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION"; private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists."; + private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_'; private final UniqueValueDao uniqueValueDao; - public UniqueValueUtil(UniqueValueDao uniqueValueDao) { this.uniqueValueDao = uniqueValueDao; } @@ -43,12 +44,10 @@ public class UniqueValueUtil { * @param uniqueCombination the unique combination */ public void createUniqueValue(String type, String... uniqueCombination) { - Optional value = formatValue(uniqueCombination); - if (!value.isPresent()) { - return; - } - validateUniqueValue(type, value.get(), uniqueCombination); - uniqueValueDao.create(new UniqueValueEntity(type, value.get())); + formatValue(uniqueCombination).ifPresent(formattedValue -> { + validateUniqueValue(type, formattedValue, uniqueCombination); + uniqueValueDao.create(new UniqueValueEntity(type, formattedValue)); + }); } /** @@ -58,11 +57,9 @@ public class UniqueValueUtil { * @param uniqueCombination the unique combination */ public void deleteUniqueValue(String type, String... uniqueCombination) { - Optional value = formatValue(uniqueCombination); - if (!value.isPresent()) { - return; - } - uniqueValueDao.delete(new UniqueValueEntity(type, value.get())); + formatValue(uniqueCombination).ifPresent( + formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue))); + } /** @@ -74,7 +71,7 @@ public class UniqueValueUtil { * @param uniqueContext the unique context */ public void updateUniqueValue(String type, String oldValue, String newValue, - String... uniqueContext) { + String... uniqueContext) { if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) { createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue})); deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue})); @@ -88,31 +85,50 @@ public class UniqueValueUtil { * @param uniqueCombination the unique combination */ public void validateUniqueValue(String type, String... uniqueCombination) { - Optional value = formatValue(uniqueCombination); - if (!value.isPresent()) { - return; - } - validateUniqueValue(type, value.get(), uniqueCombination); + formatValue(uniqueCombination) + .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination)); } - private void validateUniqueValue(String type, String value, String... uniqueCombination) { - if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) { + /** + * Checks if a unique value is taken. + * + * @return true if the unique value is occupied, false otherwise + */ + public boolean isUniqueValueOccupied(String type, String... uniqueCombination) { + return formatValue(uniqueCombination) + .map(formattedValue -> isUniqueValueOccupied(type, formattedValue)) + .orElse(false); + } + + private void validateUniqueValue(String type, String formattedValue, + String... uniqueCombination) { + if (isUniqueValueOccupied(type, formattedValue)) { throw new CoreException(new ErrorCode.ErrorCodeBuilder() .withCategory(ErrorCategory.APPLICATION) .withId(UNIQUE_VALUE_VIOLATION) - .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type, - uniqueCombination[uniqueCombination.length - 1])).build()); + .withMessage(String + .format(UNIQUE_VALUE_VIOLATION_MSG, type, getValueWithoutContext(uniqueCombination))) + .build()); } } + private boolean isUniqueValueOccupied(String type, String formattedValue) { + return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null; + } + private static Optional formatValue(String[] uniqueCombination) { if (uniqueCombination == null || uniqueCombination.length == 0 - || uniqueCombination[uniqueCombination.length - 1] == null) { + || getValueWithoutContext(uniqueCombination) == null) { return Optional.empty(); } uniqueCombination[uniqueCombination.length - 1] = - uniqueCombination[uniqueCombination.length - 1].toLowerCase(); - return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_')); + getValueWithoutContext(uniqueCombination).toLowerCase(); + return Optional.of(CommonMethods + .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR)); + } + + private static String getValueWithoutContext(String... uniqueCombination) { + return uniqueCombination[uniqueCombination.length - 1]; } } -- cgit 1.2.3-korg