36b76a0772313ed805396bee3f71edcbb7036d19
[sdc.git] /
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   private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
30   private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
31
32   private final UniqueValueDao uniqueValueDao;
33
34
35   public UniqueValueUtil(UniqueValueDao uniqueValueDao) {
36     this.uniqueValueDao = uniqueValueDao;
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     Optional<String> value = formatValue(uniqueCombination);
47     if (!value.isPresent()) {
48       return;
49     }
50     validateUniqueValue(type, value.get(), uniqueCombination);
51     uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
52   }
53
54   /**
55    * Delete unique value.
56    *
57    * @param type              the type
58    * @param uniqueCombination the unique combination
59    */
60   public void deleteUniqueValue(String type, String... uniqueCombination) {
61     Optional<String> value = formatValue(uniqueCombination);
62     if (!value.isPresent()) {
63       return;
64     }
65     uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
66   }
67
68   /**
69    * Update unique value.
70    *
71    * @param type          the type
72    * @param oldValue      the old value
73    * @param newValue      the new value
74    * @param uniqueContext the unique context
75    */
76   public void updateUniqueValue(String type, String oldValue, String newValue,
77       String... uniqueContext) {
78     if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
79       createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
80       deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
81     }
82   }
83
84   /**
85    * Validate unique value.
86    *
87    * @param type              the type
88    * @param uniqueCombination the unique combination
89    */
90   public void validateUniqueValue(String type, String... uniqueCombination) {
91     Optional<String> value = formatValue(uniqueCombination);
92     if (!value.isPresent()) {
93       return;
94     }
95     validateUniqueValue(type, value.get(), uniqueCombination);
96   }
97
98   private void validateUniqueValue(String type, String value, String... uniqueCombination) {
99     if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
100       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
101           .withCategory(ErrorCategory.APPLICATION)
102           .withId(UNIQUE_VALUE_VIOLATION)
103           .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
104               uniqueCombination[uniqueCombination.length - 1])).build());
105     }
106   }
107
108   private static Optional<String> formatValue(String[] uniqueCombination) {
109     if (uniqueCombination == null || uniqueCombination.length == 0
110         || uniqueCombination[uniqueCombination.length - 1] == null) {
111       return Optional.empty();
112     }
113
114     uniqueCombination[uniqueCombination.length - 1] =
115         uniqueCombination[uniqueCombination.length - 1].toLowerCase();
116     return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));
117   }
118 }