Test coverage : openecomp-nosqldb-api
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-api / src / main / java / org / openecomp / core / util / UniqueValueUtil.java
1 /*
2  * Copyright © 2016-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.openecomp.core.util;
18
19 import java.util.Optional;
20
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.openecomp.core.dao.UniqueValueDao;
23 import org.openecomp.core.dao.types.UniqueValueEntity;
24 import org.openecomp.core.utilities.CommonMethods;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28
29 public class UniqueValueUtil {
30
31     private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
32     private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
33     private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
34
35     private final UniqueValueDao uniqueValueDao;
36
37     public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
38         this.uniqueValueDao = uniqueValueDao;
39     }
40
41     /**
42      * Create unique value.
43      *
44      * @param type              the type
45      * @param uniqueCombination the unique combination
46      */
47     public void createUniqueValue(String type, String... uniqueCombination) {
48         String originalEntityName = null;
49         if (ArrayUtils.isNotEmpty(uniqueCombination)) {
50             originalEntityName = uniqueCombination[uniqueCombination.length - 1];
51         }
52
53         Optional<String> formattedValue = formatValue(uniqueCombination);
54         if (formattedValue.isPresent()) {
55             validateUniqueValue(type, formattedValue.get(), originalEntityName);
56             uniqueValueDao.create(new UniqueValueEntity(type, formattedValue.get()));
57         }
58     }
59
60     /**
61      * Delete unique value.
62      *
63      * @param type              the type
64      * @param uniqueCombination the unique combination
65      */
66     public void deleteUniqueValue(String type, String... uniqueCombination) {
67         formatValue(uniqueCombination).ifPresent(
68                 formattedValue -> uniqueValueDao.delete(new UniqueValueEntity(type, formattedValue)));
69
70     }
71
72     /**
73      * Update unique value.
74      *
75      * @param type          the type
76      * @param oldValue      the old value
77      * @param newValue      the new value
78      * @param uniqueContext the unique context
79      */
80     public void updateUniqueValue(String type, String oldValue, String newValue,
81                                   String... uniqueContext) {
82         if (newValue == null || !newValue.equalsIgnoreCase(oldValue)) {
83             createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {newValue}));
84             deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[] {oldValue}));
85         }
86     }
87
88     /**
89      * Validate unique value.
90      *
91      * @param type              the type
92      * @param uniqueCombination the unique combination
93      */
94     public void validateUniqueValue(String type, String... uniqueCombination) {
95         String originalEntityName = null;
96         if (ArrayUtils.isNotEmpty(uniqueCombination)) {
97             originalEntityName = uniqueCombination[uniqueCombination.length - 1];
98         }
99
100         Optional<String> formattedValue = formatValue(uniqueCombination);
101         if (formattedValue.isPresent()) {
102             validateUniqueValue(type, formattedValue.get(), originalEntityName);
103         }
104     }
105
106     private void validateUniqueValue(String type, String formattedValue, String originalEntityName) {
107         if (isUniqueValueOccupied(type, formattedValue)) {
108             throw new CoreException(new ErrorCode.ErrorCodeBuilder()
109                     .withCategory(ErrorCategory.APPLICATION)
110                     .withId(UNIQUE_VALUE_VIOLATION)
111                     .withMessage(String
112                             .format(UNIQUE_VALUE_VIOLATION_MSG, type, originalEntityName))
113                     .build());
114         }
115     }
116
117     /**
118      * Checks if a unique value is taken.
119      *
120      * @return true if the unique value is occupied, false otherwise
121      */
122     public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
123         return formatValue(uniqueCombination)
124                 .map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
125                 .orElse(false);
126     }
127
128     private boolean isUniqueValueOccupied(String type, String formattedValue) {
129         return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null;
130     }
131
132     private static Optional<String> formatValue(String[] uniqueCombination) {
133         if (uniqueCombination == null || uniqueCombination.length == 0
134                 || getValueWithoutContext(uniqueCombination) == null) {
135             return Optional.empty();
136         }
137
138         uniqueCombination[uniqueCombination.length - 1] =
139                 getValueWithoutContext(uniqueCombination).toLowerCase();
140         return Optional.of(CommonMethods
141                 .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
142     }
143
144     private static String getValueWithoutContext(String... uniqueCombination) {
145         return uniqueCombination[uniqueCombination.length - 1];
146     }
147 }