ce39ac62c97557e92ae30f8c722de0d5dfbfb8b2
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.types;
18
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;
30
31 @UDT(keyspace = "dox", name = "choice_or_other")
32 public class ChoiceOrOther<E extends Enum<E>> {
33
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";
39
40   @Transient
41   private E choice;
42
43   @Transient
44   private String other;
45
46   private String result;
47
48   public ChoiceOrOther() {
49     // the default constructor is used to instantiate by reflection
50   }
51
52   /**
53    * Instantiates a new Choice or other.
54    *
55    * @param choice the choice
56    * @param other  the other
57    */
58   public ChoiceOrOther(E choice, String other) {
59     this.choice = choice;
60     this.other = other;
61     result = resolveResult();
62   }
63
64   public E getChoice() {
65     return choice;
66   }
67
68   public void setChoice(E choice) {
69
70     this.choice = choice;
71   }
72
73   public String getOther() {
74     return other;
75   }
76
77   public void setOther(String other) {
78     this.other = other;
79   }
80
81   public String getResult() {
82     return result;
83   }
84
85   /**
86    * Sets result.
87    *
88    * @param result the result
89    */
90   public void setResult(String result) {
91     if (choice != null) {
92       if (result == null) {
93         this.result = resolveResult();
94       }
95     } else {
96       this.result = result;
97     }
98   }
99
100   private String resolveResult() {
101     return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
102   }
103
104   /**
105    * Resolve enum.
106    *
107    * @param enumClass the enum class
108    */
109   public void resolveEnum(Class<E> enumClass) {
110     if (choice != null || result == null) {
111       return;
112     }
113
114     try {
115       choice = E.valueOf(enumClass, result);
116     } catch (IllegalArgumentException exception) {
117       try {
118         choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
119       } catch (IllegalArgumentException ex) {
120         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
121             LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
122             LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
123         throw new CoreException(new ErrorCode.ErrorCodeBuilder()
124             .withId(CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
125             .withMessage(CHOICE_OR_OTHER_INVALID_ENUM_MSG)
126             .withCategory(ErrorCategory.APPLICATION).build());
127       }
128       other = result;
129     }
130   }
131
132   @Override
133   public int hashCode() {
134     int result1 = choice != null ? choice.hashCode() : 0;
135     result1 = 31 * result1 + (other != null ? other.hashCode() : 0);
136     result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
137     return result1;
138   }
139
140   @Override
141   public boolean equals(Object obj) {
142     if (this == obj) {
143       return true;
144     }
145     if (obj == null || getClass() != obj.getClass()) {
146       return false;
147     }
148
149     ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
150
151     if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
152       return false;
153     }
154     if (other != null ? !other.equals(that.other) : that.other != null) {
155       return false;
156     }
157     return result != null ? result.equals(that.result) : that.result == null;
158
159   }
160 }