0a8b640a2f92d42926eba56bbeec73af7b012a35
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / services / UniqueValueService.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.services;
18
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;
27
28 @Service("uniqueValueService")
29 public class UniqueValueService {
30
31     private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
32
33     private final UniqueValueRepository uniqueValueRepository;
34
35     @Autowired
36     public UniqueValueService(UniqueValueRepository uniqueValueRepository) {
37         this.uniqueValueRepository = uniqueValueRepository;
38     }
39
40     /**
41      * Create unique value.
42      *
43      * @param type              the type
44      * @param uniqueCombination the unique combination
45      */
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));
50         });
51     }
52
53     /**
54      * Delete unique value.
55      *
56      * @param type              the type
57      * @param uniqueCombination the unique combination
58      */
59     public void deleteUniqueValue(String type, String[] uniqueCombination) {
60         formatValue(uniqueCombination)
61                 .ifPresent(formattedValue -> uniqueValueRepository.delete(new UniqueValueEntity(type, formattedValue)));
62
63     }
64
65     /**
66      * Update unique value.
67      *
68      * @param type          the type
69      * @param oldValue      the old value
70      * @param newValue      the new value
71      * @param uniqueContext the unique context
72      */
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}));
77         }
78     }
79
80     /**
81      * Validate unique value.
82      *
83      * @param type              the type
84      * @param uniqueCombination the unique combination
85      */
86     public void validateUniqueValue(String type, String[] uniqueCombination) {
87         formatValue(uniqueCombination)
88                 .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
89     }
90
91     /**
92      * Checks if a unique value is taken.
93      *
94      * @return true if the unique value is occupied, false otherwise
95      */
96     public boolean isUniqueValueOccupied(String type, String[] uniqueCombination) {
97         return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
98                                              .orElse(false);
99     }
100
101     private void validateUniqueValue(String type, String formattedValue, String[] uniqueCombination) {
102         if (isUniqueValueOccupied(type, formattedValue)) {
103             throw new UniqueValueViolationException(type, getValueWithoutContext(uniqueCombination));
104         }
105     }
106
107     private boolean isUniqueValueOccupied(String type, String formattedValue) {
108         return uniqueValueRepository.findById(new UniqueValueEntity(type, formattedValue)).isPresent();
109     }
110
111     private Optional<String> formatValue(String[] uniqueCombination) {
112         if (ArrayUtils.isEmpty(uniqueCombination) || getValueWithoutContext(uniqueCombination) == null) {
113             return Optional.empty();
114         }
115
116         uniqueCombination[uniqueCombination.length - 1] = getValueWithoutContext(uniqueCombination).toLowerCase();
117         return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
118     }
119
120     private String getValueWithoutContext(String[] uniqueCombination) {
121         return uniqueCombination[uniqueCombination.length - 1];
122     }
123 }