summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib')
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java66
1 files changed, 41 insertions, 25 deletions
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<String> 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<String> 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<String> 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<String> 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];
}
}