0ba7228cceb89190e489cb2e73cc10c725a7daf5
[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 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 || oldValue == 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   /**
107    * Checks if a unique value is taken.
108    *
109    * @return true if the unique value is occupied, false otherwise
110    */
111   public boolean isUniqueValueOccupied(String type, String... uniqueCombination) {
112     return formatValue(uniqueCombination)
113         .map(formattedValue -> isUniqueValueOccupied(type, formattedValue))
114         .orElse(false);
115   }
116
117   private void validateUniqueValue(String type, String formattedValue, String originalEntityName) {
118     if (isUniqueValueOccupied(type, formattedValue)) {
119       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
120           .withCategory(ErrorCategory.APPLICATION)
121           .withId(UNIQUE_VALUE_VIOLATION)
122           .withMessage(String
123               .format(UNIQUE_VALUE_VIOLATION_MSG, type, originalEntityName))
124           .build());
125     }
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 }