4341899d8a6ba8e97f6f598136647b5d162088d6
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.util;
22
23 import org.openecomp.core.dao.UniqueValueDao;
24 import org.openecomp.core.dao.UniqueValueDaoFactory;
25 import org.openecomp.core.dao.types.UniqueValueEntity;
26 import org.openecomp.core.utilities.CommonMethods;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCategory;
29 import org.openecomp.sdc.common.errors.ErrorCode;
30
31 import java.util.Optional;
32
33 public class UniqueValueUtil {
34   private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
35   private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
36
37   private static final UniqueValueDao uniqueValueDao =
38       UniqueValueDaoFactory.getInstance().createInterface();
39   /**
40    * Create unique value.
41    *
42    * @param type              the type
43    * @param uniqueCombination the unique combination
44    */
45   public static 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 static 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 static 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 static 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 static 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 }