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 java.util.Optional;
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.openecomp.core.dao.UniqueValueDao;
23 import org.openecomp.core.dao.types.UniqueValueEntity;
24 import org.openecomp.core.utilities.CommonMethods;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.common.errors.ErrorCode;
29 public class UniqueValueUtil {
31 private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
32 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
33 private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
35 private final UniqueValueDao uniqueValueDao;
37 public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
38 this.uniqueValueDao = uniqueValueDao;
42 * Create unique value.
44 * @param type the type
45 * @param uniqueCombination the unique combination
47 public void createUniqueValue(String type, String... uniqueCombination) {
48 String originalEntityName = null;
49 if (ArrayUtils.isNotEmpty(uniqueCombination)) {
50 originalEntityName = uniqueCombination[uniqueCombination.length - 1];
53 Optional<String> formattedValue = formatValue(uniqueCombination);
54 if (formattedValue.isPresent()) {
55 validateUniqueValue(type, formattedValue.get(), originalEntityName);
56 uniqueValueDao.create(new UniqueValueEntity(type, formattedValue.get()));
61 * Delete unique value.
63 * @param type the type
64 * @param uniqueCombination the unique combination
66 public void deleteUniqueValue(String type, String... uniqueCombination) {
67 formatValue(uniqueCombination).ifPresent(
68 formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue)));
73 * Update unique value.
75 * @param type the type
76 * @param oldValue the old value
77 * @param newValue the new value
78 * @param uniqueContext the unique context
80 public void updateUniqueValue(String type, String oldValue, String newValue,
81 String... uniqueContext) {
82 if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) {
83 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {newValue}));
84 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {oldValue}));
89 * Validate unique value.
91 * @param type the type
92 * @param uniqueCombination the unique combination
94 public void validateUniqueValue(String type, String... uniqueCombination) {
95 String originalEntityName = null;
96 if (ArrayUtils.isNotEmpty(uniqueCombination)) {
97 originalEntityName = uniqueCombination[uniqueCombination.length - 1];
100 Optional<String> formattedValue = formatValue(uniqueCombination);
101 if (formattedValue.isPresent()) {
102 validateUniqueValue(type, formattedValue.get(), originalEntityName);
106 private void validateUniqueValue(String type, String formattedValue, String originalEntityName) {
107 if (isUniqueValueOccupied(type, formattedValue)) {
108 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
109 .withCategory(ErrorCategory.APPLICATION)
110 .withId(UNIQUE_VALUE_VIOLATION)
112 .format(UNIQUE_VALUE_VIOLATION_MSG, type, originalEntityName))
118 * Checks if a unique value is taken.
120 * @return true if the unique value is occupied, false otherwise
122 public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
123 return formatValue(uniqueCombination)
124 .map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
128 private boolean isUniqueValueOccupied(String type, String formattedValue) {
129 return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null;
132 private static Optional<String> formatValue(String[] uniqueCombination) {
133 if (uniqueCombination == null || uniqueCombination.length == 0
134 || getValueWithoutContext(uniqueCombination) == null) {
135 return Optional.empty();
138 uniqueCombination[uniqueCombination.length - 1] =
139 getValueWithoutContext(uniqueCombination).toLowerCase();
140 return Optional.of(CommonMethods
141 .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
144 private static String getValueWithoutContext(String... uniqueCombination) {
145 return uniqueCombination[uniqueCombination.length - 1];