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 @UDT(keyspace = "dox", name = "choice_or_other")
38 public class ChoiceOrOther<E extends Enum<E>> {
40 public static final String CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
41 "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
42 public static final String CHOICE_OR_OTHER_INVALID_ENUM_MSG =
43 "Enum used as part of ChoiceOrOther type must contain the value 'Other'";
44 public static final String OTHER_ENUM_VALUE = "Other";
46 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
54 private String result;
56 public ChoiceOrOther() {
60 * Instantiates a new Choice or other.
62 * @param choice the choice
63 * @param other the other
65 public ChoiceOrOther(E choice, String other) {
68 result = resolveResult();
71 public E getChoice() {
75 public void setChoice(E choice) {
80 public String getOther() {
84 public void setOther(String other) {
88 public String getResult() {
95 * @param result the result
97 public void setResult(String result) {
100 this.result = resolveResult();
103 this.result = result;
107 private String resolveResult() {
108 return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
114 * @param enumClass the enum class
116 public void resolveEnum(Class<E> enumClass) {
117 if (choice != null || result == null) {
122 choice = E.valueOf(enumClass, result);
123 } catch (IllegalArgumentException exception) {
124 log.debug("",exception);
126 choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
127 } catch (IllegalArgumentException ex) {
129 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
130 LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
131 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
132 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
133 .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
134 .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
135 .withCategory(ErrorCategory.APPLICATION).build());
142 public int hashCode() {
143 int result1 = choice != null ? choice.hashCode() : 0;
144 result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
145 result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
150 public boolean equals(Object obj) {
154 if (obj == null || getClass() != obj.getClass()) {
158 ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
160 if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
163 if (other != null ? !other.equals(that.other) : that.other != null) {
166 return result != null ? result.equals(that.result) : that.result == null;