Introduce unique value RESTs
[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 org.openecomp.core.dao.UniqueValueDao;
20 import org.openecomp.core.dao.types.UniqueValueEntity;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCategory;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25
26 import java.util.Optional;
27
28 public class UniqueValueUtil {
29
30   private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
31   private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
32   private static final char FORMATTED_UNIQUE_VALUE_SEPARATOR = '_';
33
34   private final UniqueValueDao uniqueValueDao;
35
36   public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
37     this.uniqueValueDao = uniqueValueDao;
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       uniqueValueDao.create(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).ifPresent(
61         formattedValue -> uniqueValueDao.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,
74                                 String... uniqueContext) {
75     if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
76       createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
77       deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
78     }
79   }
80
81   /**
82    * Validate unique value.
83    *
84    * @param type              the type
85    * @param uniqueCombination the unique combination
86    */
87   public void validateUniqueValue(String type, String... uniqueCombination) {
88     formatValue(uniqueCombination)
89         .ifPresent(formattedValue -> validateUniqueValue(type, formattedValue, uniqueCombination));
90   }
91
92   /**
93    * Checks if a unique value is taken.
94    *
95    * @return true if the unique value is occupied, false otherwise
96    */
97   public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
98     return formatValue(uniqueCombination)
99         .map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
100         .orElse(false);
101   }
102
103   private void validateUniqueValue(String type, String formattedValue,
104                                    String... uniqueCombination) {
105     if (isUniqueValueOccupied(type, formattedValue)) {
106       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
107           .withCategory(ErrorCategory.APPLICATION)
108           .withId(UNIQUE_VALUE_VIOLATION)
109           .withMessage(String
110               .format(UNIQUE_VALUE_VIOLATION_MSG, type, getValueWithoutContext(uniqueCombination)))
111           .build());
112     }
113   }
114
115   private boolean isUniqueValueOccupied(String type, String formattedValue) {
116     return uniqueValueDao.get(new UniqueValueEntity(type, formattedValue)) != null;
117   }
118
119   private static Optional<String> formatValue(String[] uniqueCombination) {
120     if (uniqueCombination == null || uniqueCombination.length == 0
121         || getValueWithoutContext(uniqueCombination) == null) {
122       return Optional.empty();
123     }
124
125     uniqueCombination[uniqueCombination.length - 1] =
126         getValueWithoutContext(uniqueCombination).toLowerCase();
127     return Optional.of(CommonMethods
128         .arrayToSeparatedString(uniqueCombination, FORMATTED_UNIQUE_VALUE_SEPARATOR));
129   }
130
131   private static String getValueWithoutContext(String... uniqueCombination) {
132     return uniqueCombination[uniqueCombination.length - 1];
133   }
134 }