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.lang.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.openecomp.core.utilities.CommonMethods; // todo get rid of
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Service;
28 @Service("uniqueValueService")
29 public class UniqueValueService {
31 private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
33 private final UniqueValueRepository uniqueValueRepository;
36 public UniqueValueService(UniqueValueRepository uniqueValueRepository) {
37 this.uniqueValueRepository = uniqueValueRepository;
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 uniqueValueRepository.insert(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)
61 .ifPresent(formattedValue -> uniqueValueRepository.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, String ... uniqueContext) {
74 if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) {
75 createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {newValue}));
76 deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {oldValue}));
81 * Validate unique value.
83 * @param type the type
84 * @param uniqueCombination the unique combination
86 public void validateUniqueValue(String type, String[] uniqueCombination) {
87 formatValue(uniqueCombination)
88 .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
92 * Checks if a unique value is taken.
94 * @return true if the unique value is occupied, false otherwise
96 public boolean isUniqueValueOccupied(String type, String[] uniqueCombination) {
97 return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
101 private void validateUniqueValue(String type, String formattedValue, String[] uniqueCombination) {
102 if (isUniqueValueOccupied(type, formattedValue)) {
103 throw new UniqueValueViolationException(type, getValueWithoutContext(uniqueCombination));
107 private boolean isUniqueValueOccupied(String type, String formattedValue) {
108 return uniqueValueRepository.findById(new UniqueValueEntity(type, formattedValue)).isPresent();
111 private Optional<String> formatValue(String[] uniqueCombination) {
112 if (ArrayUtils.isEmpty(uniqueCombination) || getValueWithoutContext(uniqueCombination) == null) {
113 return Optional.empty();
116 uniqueCombination[uniqueCombination.length - 1] = getValueWithoutContext(uniqueCombination).toLowerCase();
117 return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
120 private String getValueWithoutContext(String[] uniqueCombination) {
121 return uniqueCombination[uniqueCombination.length - 1];