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 @UDT(keyspace = "dox", name = "choice_or_other")
36 public class ChoiceOrOther<E extends Enum<E>> {
38 public static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
39 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
40 public static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
41 "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
42 public static final String OTHER_ENUM_VALUE = "Other";
50 private String result;
52 public ChoiceOrOther() {
56 * Instantiates a new Choice or other.
58 * @param choice the choice
59 * @param other the other
61 public ChoiceOrOther(E choice, String other) {
64 result = resolveResult();
67 public E getChoice() {
71 public void setChoice(E choice) {
76 public String getOther() {
80 public void setOther(String other) {
84 public String getResult() {
91 * @param result the result
93 public void setResult(String result) {
96 this.result = resolveResult();
103 private String resolveResult() {
104 return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
110 * @param enumClass the enum class
112 public void resolveEnum(Class<E> enumClass) {
113 if (choice != null || result == null) {
118 choice = E.valueOf(enumClass, result);
119 } catch (IllegalArgumentException exception) {
121 choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
122 } catch (IllegalArgumentException ex) {
123 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
124 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
125 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
126 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
127 .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
128 .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
129 .withCategory(ErrorCategory.APPLICATION).build());
136 public int hashCode() {
137 int result1 = choice != null ? choice.hashCode() : 0;
138 result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
139 result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
144 public boolean equals(Object obj) {
148 if (obj == null || getClass() != obj.getClass()) {
152 ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
154 if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
157 if (other != null ? !other.equals(that.other) : that.other != null) {
160 return result != null ? result.equals(that.result) : that.result == null;