2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.util;
23 import org.openecomp.core.dao.UniqueValueDao;
24 import org.openecomp.core.dao.UniqueValueDaoFactory;
25 import org.openecomp.core.dao.types.UniqueValueEntity;
26 import org.openecomp.core.utilities.CommonMethods;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCategory;
29 import org.openecomp.sdc.common.errors.ErrorCode;
31 import java.util.Optional;
34 * The type Unique value util.
36 public class UniqueValueUtil {
38 * The constant UNIQUE_VALUE_VIOLATION.
40 public static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
41 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
43 private static final UniqueValueDao uniqueValueDao =
44 UniqueValueDaoFactory.getInstance().createInterface();
47 * Create unique value.
49 * @param type the type
50 * @param uniqueCombination the unique combination
52 public static void createUniqueValue(String type, String... uniqueCombination) {
53 Optional<String> value = formatValue(uniqueCombination);
54 if (!value.isPresent()) {
57 validateUniqueValue(type, value.get(), uniqueCombination);
58 uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
62 * Delete unique value.
64 * @param type the type
65 * @param uniqueCombination the unique combination
67 public static void deleteUniqueValue(String type, String... uniqueCombination) {
68 Optional<String> value = formatValue(uniqueCombination);
69 if (!value.isPresent()) {
72 uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
76 * Update unique value.
78 * @param type the type
79 * @param oldValue the old value
80 * @param newValue the new value
81 * @param uniqueContext the unique context
83 public static void updateUniqueValue(String type, String oldValue, String newValue,
84 String... uniqueContext) {
85 if ((newValue != null && oldValue != null
86 && !newValue.toLowerCase().equals(oldValue.toLowerCase()))
87 || newValue == null || oldValue == null) {
88 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
89 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
94 * Validate unique value.
96 * @param type the type
97 * @param uniqueCombination the unique combination
99 public static void validateUniqueValue(String type, String... uniqueCombination) {
100 Optional<String> value = formatValue(uniqueCombination);
101 if (!value.isPresent()) {
104 validateUniqueValue(type, value.get(), uniqueCombination);
107 private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
108 if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
109 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
110 .withCategory(ErrorCategory.APPLICATION)
111 .withId(UNIQUE_VALUE_VIOLATION)
112 .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
113 uniqueCombination[uniqueCombination.length - 1])).build());
117 private static Optional<String> formatValue(String[] uniqueCombination) {
118 if (uniqueCombination == null || uniqueCombination.length == 0
119 || uniqueCombination[uniqueCombination.length - 1] == null) {
120 return Optional.empty();
123 uniqueCombination[uniqueCombination.length - 1] =
124 uniqueCombination[uniqueCombination.length - 1].toLowerCase();
125 return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));