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