2 * Copyright © 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.onap.sdc.workflow.services;
19 import java.util.Optional;
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.onap.sdc.workflow.persistence.UniqueValueRepository;
22 import org.onap.sdc.workflow.persistence.types.UniqueValueEntity;
23 import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Service;
27 @Service("uniqueValueService")
28 public class UniqueValueService {
30 private static final String FORMATTED_UNIQUE_VALUE_SEPARATOR = "_";
32 private final UniqueValueRepository uniqueValueRepository;
35 public UniqueValueService(UniqueValueRepository uniqueValueRepository) {
36 this.uniqueValueRepository = uniqueValueRepository;
40 * Create unique value.
42 * @param type the type
43 * @param uniqueCombination the unique combination
45 public void createUniqueValue(String type, String... uniqueCombination) {
46 formatValue(uniqueCombination).ifPresent(formattedValue -> {
47 validateUniqueValue(type, formattedValue, uniqueCombination);
48 uniqueValueRepository.insert(new UniqueValueEntity(type, formattedValue));
53 * Delete unique value.
55 * @param type the type
56 * @param uniqueCombination the unique combination
58 public void deleteUniqueValue(String type, String... uniqueCombination) {
59 formatValue(uniqueCombination)
60 .ifPresent(formattedValue -> uniqueValueRepository.delete(new UniqueValueEntity(type, formattedValue)));
65 * Update unique value.
67 * @param type the type
68 * @param oldValue the old value
69 * @param newValue the new value
70 * @param uniqueContext the unique context
72 public void updateUniqueValue(String type, String oldValue, String newValue, String... uniqueContext) {
73 if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) {
74 createUniqueValue(type, ArrayUtils.addAll(uniqueContext, newValue));
75 deleteUniqueValue(type, ArrayUtils.addAll(uniqueContext, oldValue));
80 * Validate unique value.
82 * @param type the type
83 * @param uniqueCombination the unique combination
85 public void validateUniqueValue(String type, String... uniqueCombination) {
86 formatValue(uniqueCombination)
87 .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
90 private Optional<String> formatValue(String[] uniqueCombination) {
91 if (ArrayUtils.isEmpty(uniqueCombination) || getValueWithoutContext(uniqueCombination) == null) {
92 return Optional.empty();
95 uniqueCombination[uniqueCombination.length - 1] = getValueWithoutContext(uniqueCombination).toLowerCase();
96 return Optional.of(String.join(FORMATTED_UNIQUE_VALUE_SEPARATOR, uniqueCombination));
99 private void validateUniqueValue(String type, String formattedValue, String[] uniqueCombination) {
100 if (isUniqueValueOccupied(type, formattedValue)) {
101 throw new UniqueValueViolationException(type, getValueWithoutContext(uniqueCombination));
105 private boolean isUniqueValueOccupied(String type, String formattedValue) {
106 return uniqueValueRepository.findById(new UniqueValueEntity(type, formattedValue)).isPresent();
110 * Checks if a unique value is taken.
112 * @return true if the unique value is occupied, false otherwise
114 public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
115 return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
119 private String getValueWithoutContext(String[] uniqueCombination) {
120 return uniqueCombination[uniqueCombination.length - 1];