Add activity spec code
[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.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;
26
27 @Service("uniqueValueService")
28 public class UniqueValueService {
29
30     private static final String FORMATTED_UNIQUE_VALUE_SEPARATOR = "_";
31
32     private final UniqueValueRepository uniqueValueRepository;
33
34     @Autowired
35     public UniqueValueService(UniqueValueRepository uniqueValueRepository) {
36         this.uniqueValueRepository = uniqueValueRepository;
37     }
38
39     /**
40      * Create unique value.
41      *
42      * @param type              the type
43      * @param uniqueCombination the unique combination
44      */
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));
49         });
50     }
51
52     /**
53      * Delete unique value.
54      *
55      * @param type              the type
56      * @param uniqueCombination the unique combination
57      */
58     public void deleteUniqueValue(String type, String... uniqueCombination) {
59         formatValue(uniqueCombination)
60                 .ifPresent(formattedValue -> uniqueValueRepository.delete(new UniqueValueEntity(type, formattedValue)));
61
62     }
63
64     /**
65      * Update unique value.
66      *
67      * @param type          the type
68      * @param oldValue      the old value
69      * @param newValue      the new value
70      * @param uniqueContext the unique context
71      */
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));
76         }
77     }
78
79     /**
80      * Validate unique value.
81      *
82      * @param type              the type
83      * @param uniqueCombination the unique combination
84      */
85     public void validateUniqueValue(String type, String... uniqueCombination) {
86         formatValue(uniqueCombination)
87                 .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
88     }
89
90     private Optional<String> formatValue(String[] uniqueCombination) {
91         if (ArrayUtils.isEmpty(uniqueCombination) || getValueWithoutContext(uniqueCombination) == null) {
92             return Optional.empty();
93         }
94
95         uniqueCombination[uniqueCombination.length - 1] = getValueWithoutContext(uniqueCombination).toLowerCase();
96         return Optional.of(String.join(FORMATTED_UNIQUE_VALUE_SEPARATOR, uniqueCombination));
97     }
98
99     private void validateUniqueValue(String type, String formattedValue, String[] uniqueCombination) {
100         if (isUniqueValueOccupied(type, formattedValue)) {
101             throw new UniqueValueViolationException(type, getValueWithoutContext(uniqueCombination));
102         }
103     }
104
105     private boolean isUniqueValueOccupied(String type, String formattedValue) {
106         return uniqueValueRepository.findById(new UniqueValueEntity(type, formattedValue)).isPresent();
107     }
108
109     /**
110      * Checks if a unique value is taken.
111      *
112      * @return true if the unique value is occupied, false otherwise
113      */
114     public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
115         return formatValue(uniqueCombination).map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
116                                              .orElse(false);
117     }
118
119     private String getValueWithoutContext(String[] uniqueCombination) {
120         return uniqueCombination[uniqueCombination.length - 1];
121     }
122 }