dce34a1c961f6201030bfaa1d460a0be092212f9
[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 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31
32 import java.util.Optional;
33
34 public class UniqueValueUtil {
35   public 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
50
51     mdcDataDebugMessage.debugEntryMessage(null, null);
52
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     mdcDataDebugMessage.debugExitMessage(null, null);
61   }
62
63   /**
64    * Delete unique value.
65    *
66    * @param type              the type
67    * @param uniqueCombination the unique combination
68    */
69   public static void deleteUniqueValue(String type, String... uniqueCombination) {
70
71
72     mdcDataDebugMessage.debugEntryMessage(null, null);
73
74     Optional<String> value = formatValue(uniqueCombination);
75     if (!value.isPresent()) {
76       return;
77     }
78     uniqueValueDao.delete(new UniqueValueEntity(type, value.get()));
79
80     mdcDataDebugMessage.debugExitMessage(null, null);
81   }
82
83   /**
84    * Update unique value.
85    *
86    * @param type          the type
87    * @param oldValue      the old value
88    * @param newValue      the new value
89    * @param uniqueContext the unique context
90    */
91   public static void updateUniqueValue(String type, String oldValue, String newValue,
92                                        String... uniqueContext) {
93
94
95     mdcDataDebugMessage.debugEntryMessage(null, null);
96
97     boolean nonEqual = (newValue != null) && (oldValue != null)
98             && !newValue.toLowerCase().equals(oldValue.toLowerCase());
99
100     if (nonEqual || 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 }