2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.dao.types;
23 import com.datastax.driver.mapping.annotations.Transient;
24 import com.datastax.driver.mapping.annotations.UDT;
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 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
32 import org.openecomp.sdc.logging.types.LoggerConstants;
33 import org.openecomp.sdc.logging.types.LoggerErrorCode;
34 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
37 import java.util.HashSet;
40 @UDT(keyspace = "dox", name = "multi_choice_or_other")
41 public class MultiChoiceOrOther<E extends Enum<E>> {
42 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
43 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
44 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
45 "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
46 public static final String OTHER_ENUM_VALUE = "Other";
48 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
51 private Set<E> choices;
56 private Set<String> results;
58 public MultiChoiceOrOther() {
62 * Instantiates a new Multi choice or other.
64 * @param choices the choices
65 * @param other the other
67 public MultiChoiceOrOther(Set<E> choices, String other) {
68 this.choices = choices;
70 results = resolveResult();
73 public Set<E> getChoices() {
77 public void setChoices(Set<E> choices) {
78 this.choices = choices;
81 public String getOther() {
85 public void setOther(String other) {
89 public Set<String> getResults() {
96 * @param results the results
98 public void setResults(Set<String> results) {
99 if (choices != null) {
100 if (results == null) {
101 this.results = resolveResult();
104 this.results = results;
108 private Set<String> resolveResult() {
109 if (choices != null) {
110 results = new HashSet<>();
111 if(choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
114 for (E choice : choices) {
115 results.add(choice.name());
126 * @param enumClass the enum class
128 public void resolveEnum(Class<E> enumClass) {
129 if (choices != null || results == null || results.size() == 0) {
133 choices = new HashSet<>();
134 if (results.size() > 1) {
135 for (String result : results) {
136 choices.add(E.valueOf(enumClass, result));
139 String result = results.iterator().next();
141 choices.add(E.valueOf(enumClass, result));
142 } catch (IllegalArgumentException exception) {
143 log.debug("",exception);
145 choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
146 } catch (IllegalArgumentException ex) {
148 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
149 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
150 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
151 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
152 .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
153 .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
154 .withCategory(ErrorCategory.APPLICATION).build());
162 public int hashCode() {
163 int result = choices != null ? choices.hashCode() : 0;
164 result = 31 * result + (other != null ? other.hashCode() : 0);
165 result = 31 * result + (results != null ? results.hashCode() : 0);
170 public boolean equals(Object obj) {
174 if (obj == null || getClass() != obj.getClass()) {
178 MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
180 if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
183 if (other != null ? !other.equals(that.other) : that.other != null) {
186 return results != null ? results.equals(that.results) : that.results == null;