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.context.impl.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import java.util.HashSet;
38 @UDT(keyspace = "dox", name = "multi_choice_or_other")
39 public class MultiChoiceOrOther<E extends Enum<E>> {
40 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
41 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
42 public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
43 "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
44 public static final String OTHER_ENUM_VALUE = "Other";
47 private Set<E> choices;
52 private Set<String> results;
54 public MultiChoiceOrOther() {
58 * Instantiates a new Multi choice or other.
60 * @param choices the choices
61 * @param other the other
63 public MultiChoiceOrOther(Set<E> choices, String other) {
64 this.choices = choices;
66 results = resolveResult();
69 public Set<E> getChoices() {
73 public void setChoices(Set<E> choices) {
74 this.choices = choices;
77 public String getOther() {
81 public void setOther(String other) {
85 public Set<String> getResults() {
92 * @param results the results
94 public void setResults(Set<String> results) {
95 if (choices != null) {
96 if (results == null) {
97 this.results = resolveResult();
100 this.results = results;
104 private Set<String> resolveResult() {
105 if (choices != null) {
106 results = new HashSet<>();
107 if(choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
110 for (E choice : choices) {
111 results.add(choice.name());
122 * @param enumClass the enum class
124 public void resolveEnum(Class<E> enumClass) {
125 if (choices != null || results == null || results.size() == 0) {
129 choices = new HashSet<>();
130 if (results.size() > 1) {
131 for (String result : results) {
132 choices.add(E.valueOf(enumClass, result));
135 String result = results.iterator().next();
137 choices.add(E.valueOf(enumClass, result));
138 } catch (IllegalArgumentException exception) {
140 choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
141 } catch (IllegalArgumentException ex) {
143 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
144 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
145 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
146 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
147 .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
148 .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
149 .withCategory(ErrorCategory.APPLICATION).build());
157 public int hashCode() {
158 int result = choices != null ? choices.hashCode() : 0;
159 result = 31 * result + (other != null ? other.hashCode() : 0);
160 result = 31 * result + (results != null ? results.hashCode() : 0);
165 public boolean equals(Object obj) {
169 if (obj == null || getClass() != obj.getClass()) {
173 MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
175 if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
178 if (other != null ? !other.equals(that.other) : that.other != null) {
181 return results != null ? results.equals(that.results) : that.results == null;