[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-api / src / main / java / org / openecomp / sdc / vendorlicense / dao / types / ChoiceOrOther.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.dao.types;
22
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;
34
35 @UDT(keyspace = "dox", name = "choice_or_other")
36 public class ChoiceOrOther<E extends Enum<E>> {
37
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";
43
44   @Transient
45   private E choice;
46
47   @Transient
48   private String other;
49
50   private String result;
51
52   public ChoiceOrOther() {
53   }
54
55   /**
56    * Instantiates a new Choice or other.
57    *
58    * @param choice the choice
59    * @param other  the other
60    */
61   public ChoiceOrOther(E choice, String other) {
62     this.choice = choice;
63     this.other = other;
64     result = resolveResult();
65   }
66
67   public E getChoice() {
68     return choice;
69   }
70
71   public void setChoice(E choice) {
72
73     this.choice = choice;
74   }
75
76   public String getOther() {
77     return other;
78   }
79
80   public void setOther(String other) {
81     this.other = other;
82   }
83
84   public String getResult() {
85     return result;
86   }
87
88   /**
89    * Sets result.
90    *
91    * @param result the result
92    */
93   public void setResult(String result) {
94     if (choice != null) {
95       if (result == null) {
96         this.result = resolveResult();
97       }
98     } else {
99       this.result = result;
100     }
101   }
102
103   private String resolveResult() {
104     return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
105   }
106
107   /**
108    * Resolve enum.
109    *
110    * @param enumClass the enum class
111    */
112   public void resolveEnum(Class<E> enumClass) {
113     if (choice != null || result == null) {
114       return;
115     }
116
117     try {
118       choice = E.valueOf(enumClass, result);
119     } catch (IllegalArgumentException exception) {
120       try {
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());
130       }
131       other = result;
132     }
133   }
134
135   @Override
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);
140     return result1;
141   }
142
143   @Override
144   public boolean equals(Object obj) {
145     if (this == obj) {
146       return true;
147     }
148     if (obj == null || getClass() != obj.getClass()) {
149       return false;
150     }
151
152     ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
153
154     if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
155       return false;
156     }
157     if (other != null ? !other.equals(that.other) : that.other != null) {
158       return false;
159     }
160     return result != null ? result.equals(that.result) : that.result == null;
161
162   }
163 }