Add collaboration feature
[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 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31
32 import java.util.Optional;
33
34 public class UniqueValueUtil {
35   private static final String UNIQUE_VALUE_VIOLATION = "UNIQUE_VALUE_VIOLATION";
36   private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
37
38   private static final UniqueValueDao uniqueValueDao =
39       UniqueValueDaoFactory.getInstance().createInterface();
40   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
41
42   /**
43    * Create unique value.
44    *
45    * @param type              the type
46    * @param uniqueCombination the unique combination
47    */
48   public static void createUniqueValue(String type, String... uniqueCombination) {
49     mdcDataDebugMessage.debugEntryMessage(null);
50
51     Optional<String> value = formatValue(uniqueCombination);
52     if (!value.isPresent()) {
53       return;
54     }
55     validateUniqueValue(type, value.get(), uniqueCombination);
56     uniqueValueDao.create(new UniqueValueEntity(type, value.get()));
57
58     mdcDataDebugMessage.debugExitMessage(null);
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
69
70     mdcDataDebugMessage.debugEntryMessage(null);
71
72     Optional<String> value = formatValue(uniqueCombination);
73     if (!value.isPresent()) {
74       return;
75     }
76     uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
77
78     mdcDataDebugMessage.debugExitMessage(null);
79   }
80
81   /**
82    * Update unique value.
83    *
84    * @param type          the type
85    * @param oldValue      the old value
86    * @param newValue      the new value
87    * @param uniqueContext the unique context
88    */
89   public static void updateUniqueValue(String type, String oldValue, String newValue,
90                                        String... uniqueContext) {
91
92
93     mdcDataDebugMessage.debugEntryMessage(null);
94
95     if (newValue == null || oldValue == null || !newValue.equalsIgnoreCase(oldValue)) {
96       createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue}));
97       deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue}));
98     }
99
100     mdcDataDebugMessage.debugExitMessage(null);
101   }
102
103   /**
104    * Validate unique value.
105    *
106    * @param type              the type
107    * @param uniqueCombination the unique combination
108    */
109   public static void validateUniqueValue(String type, String... uniqueCombination) {
110     mdcDataDebugMessage.debugEntryMessage(null);
111
112     Optional<String> value = formatValue(uniqueCombination);
113     if (!value.isPresent()) {
114       return;
115     }
116     validateUniqueValue(type, value.get(), uniqueCombination);
117
118     mdcDataDebugMessage.debugExitMessage(null);
119   }
120
121   private static void validateUniqueValue(String type, String value, String... uniqueCombination) {
122     mdcDataDebugMessage.debugEntryMessage(null);
123
124     if (uniqueValueDao.get(new UniqueValueEntity(type, value)) != null) {
125       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
126           .withCategory(ErrorCategory.APPLICATION)
127           .withId(UNIQUE_VALUE_VIOLATION)
128           .withMessage(String.format(UNIQUE_VALUE_VIOLATION_MSG, type,
129               uniqueCombination[uniqueCombination.length - 1])).build());
130     }
131
132     mdcDataDebugMessage.debugExitMessage(null);
133   }
134
135   private static Optional<String> formatValue(String[] uniqueCombination) {
136
137
138     mdcDataDebugMessage.debugEntryMessage(null);
139
140     if (uniqueCombination == null || uniqueCombination.length == 0
141         || uniqueCombination[uniqueCombination.length - 1] == null) {
142       return Optional.empty();
143     }
144
145     uniqueCombination[uniqueCombination.length - 1] =
146         uniqueCombination[uniqueCombination.length - 1].toLowerCase();
147
148     mdcDataDebugMessage.debugExitMessage(null);
149     return Optional.of(CommonMethods.arrayToSeparatedString(uniqueCombination, '_'));
150   }
151 }