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.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.common.errors.ErrorCategory;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 @UDT(keyspace = "dox", name = "choice_or_other")
32 public class ChoiceOrOther<E extends Enum<E>> {
34 private static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
35 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
36 private static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
37 "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
38 public static final String OTHER_ENUM_VALUE = "Other";
46 private String result;
49 * Instantiates a new Choice or other.
51 * @param choice the choice
52 * @param other the other
54 public ChoiceOrOther(E choice, String other) {
57 result = resolveResult();
60 public E getChoice() {
64 public void setChoice(E choice) {
69 public String getOther() {
73 public void setOther(String other) {
77 public String getResult() {
84 * @param result the result
86 public void setResult(String result) {
89 this.result = resolveResult();
96 private String resolveResult() {
97 return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
103 * @param enumClass the enum class
105 public void resolveEnum(Class<E> enumClass) {
106 if (choice != null || result == null) {
111 choice = E.valueOf(enumClass, result);
112 } catch (IllegalArgumentException exception) {
114 choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
115 } catch (IllegalArgumentException ex) {
116 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
117 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
118 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
119 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
120 .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
121 .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
122 .withCategory(ErrorCategory.APPLICATION).build());
129 public int hashCode() {
130 int result1 = choice != null ? choice.hashCode() : 0;
131 result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
132 result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
137 public boolean equals(Object obj) {
141 if (obj == null || getClass() != obj.getClass()) {
145 ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
147 if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
150 if (other != null ? !other.equals(that.other) : that.other != null) {
153 return result != null ? result.equals(that.result) : that.result == null;