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 {
30 private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
31 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
32 private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
34 private final UniqueValueDao uniqueValueDao;
36 public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
37 this.uniqueValueDao = uniqueValueDao;
41 * Create unique value.
43 * @param type the type
44 * @param uniqueCombination the unique combination
46 public void createUniqueValue(String type, String... uniqueCombination) {
47 formatValue(uniqueCombination).ifPresent(formattedValue -> {
48 validateUniqueValue(type, formattedValue, uniqueCombination);
49 uniqueValueDao.create(new UniqueValueEntity(type, formattedValue));
54 * Delete unique value.
56 * @param type the type
57 * @param uniqueCombination the unique combination
59 public void deleteUniqueValue(String type, String... uniqueCombination) {
60 formatValue(uniqueCombination).ifPresent(
61 formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue)));
66 * Update unique value.
68 * @param type the type
69 * @param oldValue the old value
70 * @param newValue the new value
71 * @param uniqueContext the unique context
73 public void updateUniqueValue(String type, String oldValue, String newValue,
74 String... uniqueContext) {
75 if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
76 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
77 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
82 * Validate unique value.
84 * @param type the type
85 * @param uniqueCombination the unique combination
87 public void validateUniqueValue(String type, String... uniqueCombination) {
88 formatValue(uniqueCombination)
89 .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
93 * Checks if a unique value is taken.
95 * @return true if the unique value is occupied, false otherwise
97 public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
98 return formatValue(uniqueCombination)
99 .map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
103 private void validateUniqueValue(String type, String formattedValue,
104 String... uniqueCombination) {
105 if (isUniqueValueOccupied(type, formattedValue)) {
106 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
107 .withCategory(ErrorCategory.APPLICATION)
108 .withId(UNIQUE_VALUE_VIOLATION)
110 .format(UNIQUE_VALUE_VIOLATION_MSG, type, getValueWithoutContext(uniqueCombination)))
115 private boolean isUniqueValueOccupied(String type, String formattedValue) {
116 return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null;
119 private static Optional<String> formatValue(String[] uniqueCombination) {
120 if (uniqueCombination == null || uniqueCombination.length == 0
121 || getValueWithoutContext(uniqueCombination) == null) {
122 return Optional.empty();
125 uniqueCombination[uniqueCombination.length - 1] =
126 getValueWithoutContext(uniqueCombination).toLowerCase();
127 return Optional.of(CommonMethods
128 .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
131 private static String getValueWithoutContext(String... uniqueCombination) {
132 return uniqueCombination[uniqueCombination.length - 1];