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;
33 public class UniqueValueUtil {
34 private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
35 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
37 private static final UniqueValueDao uniqueValueDao =
38 UniqueValueDaoFactory.getInstance().createInterface();
40 * Create unique value.
42 * @param type the type
43 * @param uniqueCombination the unique combination
45 public static void createUniqueValue(String type, String... uniqueCombination) {
46 Optional<String> value = formatValue(uniqueCombination);
47 if (!value.isPresent()) {
50 validateUniqueValue(type, value.get(), uniqueCombination);
51 uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
55 * Delete unique value.
57 * @param type the type
58 * @param uniqueCombination the unique combination
60 public static void deleteUniqueValue(String type, String... uniqueCombination) {
61 Optional<String> value = formatValue(uniqueCombination);
62 if (!value.isPresent()) {
65 uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
69 * Update unique value.
71 * @param type the type
72 * @param oldValue the old value
73 * @param newValue the new value
74 * @param uniqueContext the unique context
76 public static void updateUniqueValue(String type, String oldValue, String newValue,
77 String... uniqueContext) {
78 if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
79 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
80 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
85 * Validate unique value.
87 * @param type the type
88 * @param uniqueCombination the unique combination
90 public static void validateUniqueValue(String type, String... uniqueCombination) {
91 Optional<String> value = formatValue(uniqueCombination);
92 if (!value.isPresent()) {
95 validateUniqueValue(type, value.get(), uniqueCombination);
98 private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
99 if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
100 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
101 .withCategory(ErrorCategory.APPLICATION)
102 .withId(UNIQUE_VALUE_VIOLATION)
103 .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
104 uniqueCombination[uniqueCombination.length - 1])).build());
108 private static Optional<String> formatValue(String[] uniqueCombination) {
109 if (uniqueCombination == null || uniqueCombination.length == 0
110 || uniqueCombination[uniqueCombination.length - 1] == null) {
111 return Optional.empty();
114 uniqueCombination[uniqueCombination.length - 1] =
115 uniqueCombination[uniqueCombination.length - 1].toLowerCase();
116 return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));