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;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
32 import java.util.Optional;
34 public class UniqueValueUtil {
35 private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
36 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
38 private static final UniqueValueDao uniqueValueDao =
39 UniqueValueDaoFactory.getInstance().createInterface();
40 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
43 * Create unique value.
45 * @param type the type
46 * @param uniqueCombination the unique combination
48 public static void createUniqueValue(String type, String... uniqueCombination) {
49 mdcDataDebugMessage.debugEntryMessage(null);
51 Optional<String> value = formatValue(uniqueCombination);
52 if (!value.isPresent()) {
55 validateUniqueValue(type, value.get(), uniqueCombination);
56 uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
58 mdcDataDebugMessage.debugExitMessage(null);
62 * Delete unique value.
64 * @param type the type
65 * @param uniqueCombination the unique combination
67 public static void deleteUniqueValue(String type, String... uniqueCombination) {
70 mdcDataDebugMessage.debugEntryMessage(null);
72 Optional<String> value = formatValue(uniqueCombination);
73 if (!value.isPresent()) {
76 uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
78 mdcDataDebugMessage.debugExitMessage(null);
82 * Update unique value.
84 * @param type the type
85 * @param oldValue the old value
86 * @param newValue the new value
87 * @param uniqueContext the unique context
89 public static void updateUniqueValue(String type, String oldValue, String newValue,
90 String... uniqueContext) {
93 mdcDataDebugMessage.debugEntryMessage(null);
95 if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
96 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
97 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
100 mdcDataDebugMessage.debugExitMessage(null);
104 * Validate unique value.
106 * @param type the type
107 * @param uniqueCombination the unique combination
109 public static void validateUniqueValue(String type, String... uniqueCombination) {
110 mdcDataDebugMessage.debugEntryMessage(null);
112 Optional<String> value = formatValue(uniqueCombination);
113 if (!value.isPresent()) {
116 validateUniqueValue(type, value.get(), uniqueCombination);
118 mdcDataDebugMessage.debugExitMessage(null);
121 private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
122 mdcDataDebugMessage.debugEntryMessage(null);
124 if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
125 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
126 .withCategory(ErrorCategory.APPLICATION)
127 .withId(UNIQUE_VALUE_VIOLATION)
128 .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
129 uniqueCombination[uniqueCombination.length - 1])).build());
132 mdcDataDebugMessage.debugExitMessage(null);
135 private static Optional<String> formatValue(String[] uniqueCombination) {
138 mdcDataDebugMessage.debugEntryMessage(null);
140 if (uniqueCombination == null || uniqueCombination.length == 0
141 || uniqueCombination[uniqueCombination.length - 1] == null) {
142 return Optional.empty();
145 uniqueCombination[uniqueCombination.length - 1] =
146 uniqueCombination[uniqueCombination.length - 1].toLowerCase();
148 mdcDataDebugMessage.debugExitMessage(null);
149 return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));