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.common.utils.CommonUtil;
31 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
33 import java.util.Optional;
35 public class UniqueValueUtil {
36 public static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
37 private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
39 private static final UniqueValueDao uniqueValueDao =
40 UniqueValueDaoFactory.getInstance().createInterface();
41 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
44 * Create unique value.
46 * @param type the type
47 * @param uniqueCombination the unique combination
49 public static void createUniqueValue(String type, String... uniqueCombination) {
52 mdcDataDebugMessage.debugEntryMessage(null, null);
54 Optional<String> value = formatValue(uniqueCombination);
55 if (!value.isPresent()) {
58 validateUniqueValue(type, value.get(), uniqueCombination);
59 uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
61 mdcDataDebugMessage.debugExitMessage(null, null);
65 * Delete unique value.
67 * @param type the type
68 * @param uniqueCombination the unique combination
70 public static void deleteUniqueValue(String type, String... uniqueCombination) {
73 mdcDataDebugMessage.debugEntryMessage(null, null);
75 Optional<String> value = formatValue(uniqueCombination);
76 if (!value.isPresent()) {
79 uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
81 mdcDataDebugMessage.debugExitMessage(null, null);
85 * Update unique value.
87 * @param type the type
88 * @param oldValue the old value
89 * @param newValue the new value
90 * @param uniqueContext the unique context
92 public static void updateUniqueValue(String type, String oldValue, String newValue,
93 String... uniqueContext) {
96 mdcDataDebugMessage.debugEntryMessage(null, null);
98 if ((newValue != null && oldValue != null
99 && !newValue.toLowerCase().equals(oldValue.toLowerCase()))
100 || newValue == null || oldValue == null) {
101 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
102 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
105 mdcDataDebugMessage.debugExitMessage(null, null);
109 * Validate unique value.
111 * @param type the type
112 * @param uniqueCombination the unique combination
114 public static void validateUniqueValue(String type, String... uniqueCombination) {
115 mdcDataDebugMessage.debugEntryMessage(null, null);
117 Optional<String> value = formatValue(uniqueCombination);
118 if (!value.isPresent()) {
121 validateUniqueValue(type, value.get(), uniqueCombination);
123 mdcDataDebugMessage.debugExitMessage(null, null);
126 private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
127 mdcDataDebugMessage.debugEntryMessage(null, null);
129 if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
130 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
131 .withCategory(ErrorCategory.APPLICATION)
132 .withId(UNIQUE_VALUE_VIOLATION)
133 .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
134 uniqueCombination[uniqueCombination.length - 1])).build());
137 mdcDataDebugMessage.debugExitMessage(null, null);
140 private static Optional<String> formatValue(String[] uniqueCombination) {
143 mdcDataDebugMessage.debugEntryMessage(null, null);
145 if (uniqueCombination == null || uniqueCombination.length == 0
146 || uniqueCombination[uniqueCombination.length - 1] == null) {
147 return Optional.empty();
150 uniqueCombination[uniqueCombination.length - 1] =
151 uniqueCombination[uniqueCombination.length - 1].toLowerCase();
153 mdcDataDebugMessage.debugExitMessage(null, null);
154 return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));