2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorlicense.dao.types;
19 import com.datastax.driver.mapping.annotations.Transient;
20 import com.datastax.driver.mapping.annotations.UDT;
21 import org.apache.commons.collections4.CollectionUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCategory;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import java.util.HashSet;
35 @UDT(keyspace = "dox", name = "multi_choice_or_other")
36 public class MultiChoiceOrOther<E extends Enum<E>> {
37 private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
38 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
39 private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
40 "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
41 public static final String OTHER_ENUM_VALUE = "Other";
44 private Set<E> choices;
49 private Set<String> results;
51 public MultiChoiceOrOther() {
52 // Default constructor
56 * Instantiates a new Multi choice or other.
58 * @param choices the choices
59 * @param other the other
61 public MultiChoiceOrOther(Set<E> choices, String other) {
62 this.choices = choices;
64 results = resolveResult();
67 public Set<E> getChoices() {
71 public void setChoices(Set<E> choices) {
72 this.choices = choices;
75 public String getOther() {
79 public void setOther(String other) {
83 public Set<String> getResults() {
90 * @param results the results
92 public void setResults(Set<String> results) {
93 if (choices != null) {
94 if (results == null) {
95 this.results = resolveResult();
98 this.results = results;
102 private Set<String> resolveResult() {
103 if (choices != null) {
104 results = new HashSet<>();
105 if (choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
108 for (E choice : choices) {
109 results.add(choice.name());
120 * @param enumClass the enum class
122 public void resolveEnum(Class<E> enumClass) {
123 if (choices != null || CollectionUtils.isEmpty(results)) {
127 choices = new HashSet<>();
128 if (results.size() > 1) {
129 for (String result : results) {
130 choices.add(E.valueOf(enumClass, result));
133 String result = results.iterator().next();
135 choices.add(E.valueOf(enumClass, result));
136 } catch (IllegalArgumentException exception) {
138 choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
139 } catch (IllegalArgumentException ex) {
141 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
142 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
143 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
144 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
145 .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
146 .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
147 .withCategory(ErrorCategory.APPLICATION).build());
155 public int hashCode() {
156 int result = choices != null ? choices.hashCode() : 0;
157 result = 31 * result + (other != null ? other.hashCode() : 0);
158 result = 31 * result + (results != null ? results.hashCode() : 0);
163 public boolean equals(Object obj) {
167 if (obj == null || getClass() != obj.getClass()) {
171 MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
173 if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
176 if (other != null ? !other.equals(that.other) : that.other != null) {
179 return results != null ? results.equals(that.results) : that.results == null;