push addional code
[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  * ============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 /**
34  * The type Unique value util.
35  */
36 public class UniqueValueUtil {
37   /**
38    * The constant UNIQUE_VALUE_VIOLATION.
39    */
40   public static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
41   private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
42
43   private static final UniqueValueDao uniqueValueDao =
44       UniqueValueDaoFactory.getInstance().createInterface();
45
46   /**
47    * Create unique value.
48    *
49    * @param type              the type
50    * @param uniqueCombination the unique combination
51    */
52   public static void createUniqueValue(String type, String... uniqueCombination) {
53     Optional<String> value = formatValue(uniqueCombination);
54     if (!value.isPresent()) {
55       return;
56     }
57     validateUniqueValue(type, value.get(), uniqueCombination);
58     uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
59   }
60
61   /**
62    * Delete unique value.
63    *
64    * @param type              the type
65    * @param uniqueCombination the unique combination
66    */
67   public static void deleteUniqueValue(String type, String... uniqueCombination) {
68     Optional<String> value = formatValue(uniqueCombination);
69     if (!value.isPresent()) {
70       return;
71     }
72     uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
73   }
74
75   /**
76    * Update unique value.
77    *
78    * @param type          the type
79    * @param oldValue      the old value
80    * @param newValue      the new value
81    * @param uniqueContext the unique context
82    */
83   public static void updateUniqueValue(String type, String oldValue, String newValue,
84                                        String... uniqueContext) {
85     if ((newValue != null && oldValue != null
86         && !newValue.toLowerCase().equals(oldValue.toLowerCase()))
87         || newValue == null || oldValue == null) {
88       createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
89       deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
90     }
91   }
92
93   /**
94    * Validate unique value.
95    *
96    * @param type              the type
97    * @param uniqueCombination the unique combination
98    */
99   public static void validateUniqueValue(String type, String... uniqueCombination) {
100     Optional<String> value = formatValue(uniqueCombination);
101     if (!value.isPresent()) {
102       return;
103     }
104     validateUniqueValue(type, value.get(), uniqueCombination);
105   }
106
107   private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
108     if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
109       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
110           .withCategory(ErrorCategory.APPLICATION)
111           .withId(UNIQUE_VALUE_VIOLATION)
112           .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
113               uniqueCombination[uniqueCombination.length - 1])).build());
114     }
115   }
116
117   private static Optional<String> formatValue(String[] uniqueCombination) {
118     if (uniqueCombination == null || uniqueCombination.length == 0
119         || uniqueCombination[uniqueCombination.length - 1] == null) {
120       return Optional.empty();
121     }
122
123     uniqueCombination[uniqueCombination.length - 1] =
124         uniqueCombination[uniqueCombination.length - 1].toLowerCase();
125     return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));
126   }
127 }