2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.util;
19 import org.openecomp.core.dao.UniqueValueDao;
20 import org.openecomp.core.dao.types.UniqueValueEntity;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCategory;
24 import org.openecomp.sdc.common.errors.ErrorCode;
26 import java.util.Optional;
28 public class UniqueValueUtil {
29 private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
30 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
32 private final UniqueValueDao uniqueValueDao;
35 public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
36 this.uniqueValueDao = uniqueValueDao;
40 * Create unique value.
42 * @param type the type
43 * @param uniqueCombination the unique combination
45 public 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 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 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 void validateUniqueValue(String type, String... uniqueCombination) {
91 Optional<String> value = formatValue(uniqueCombination);
92 if (!value.isPresent()) {
95 validateUniqueValue(type, value.get(), uniqueCombination);
98 private 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, '_'));