import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCategory;
import org.openecomp.sdc.common.errors.ErrorCode;
+import org.openecomp.sdc.common.utils.CommonUtil;
+import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import java.util.Optional;
-/**
- * The type Unique value util.
- */
public class UniqueValueUtil {
- /**
- * The constant UNIQUE_VALUE_VIOLATION.
- */
public 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 UniqueValueDao uniqueValueDao =
UniqueValueDaoFactory.getInstance().createInterface();
+ private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
/**
* Create unique value.
* @param uniqueCombination the unique combination
*/
public static void createUniqueValue(String type, String... uniqueCombination) {
+
+
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
Optional<String> value = formatValue(uniqueCombination);
if (!value.isPresent()) {
return;
}
validateUniqueValue(type, value.get(), uniqueCombination);
uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
}
/**
* @param uniqueCombination the unique combination
*/
public static void deleteUniqueValue(String type, String... uniqueCombination) {
+
+
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
Optional<String> value = formatValue(uniqueCombination);
if (!value.isPresent()) {
return;
}
uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
}
/**
*/
public static void updateUniqueValue(String type, String oldValue, String newValue,
String... uniqueContext) {
+
+
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
if ((newValue != null && oldValue != null
&& !newValue.toLowerCase().equals(oldValue.toLowerCase()))
|| newValue == null || oldValue == null) {
createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
}
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
}
/**
* @param uniqueCombination the unique combination
*/
public static void validateUniqueValue(String type, String... uniqueCombination) {
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
Optional<String> value = formatValue(uniqueCombination);
if (!value.isPresent()) {
return;
}
validateUniqueValue(type, value.get(), uniqueCombination);
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
}
private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
throw new CoreException(new ErrorCode.ErrorCodeBuilder()
.withCategory(ErrorCategory.APPLICATION)
.withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
uniqueCombination[uniqueCombination.length - 1])).build());
}
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
}
private static Optional<String> formatValue(String[] uniqueCombination) {
+
+
+ mdcDataDebugMessage.debugEntryMessage(null, null);
+
if (uniqueCombination == null || uniqueCombination.length == 0
|| uniqueCombination[uniqueCombination.length - 1] == null) {
return Optional.empty();
uniqueCombination[uniqueCombination.length - 1] =
uniqueCombination[uniqueCombination.length - 1].toLowerCase();
+
+ mdcDataDebugMessage.debugExitMessage(null, null);
return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));
}
}