7f5fc66ef455cd219d52ef4405210dafbc0550a1
[sdc.git] /
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.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;
36
37 @UDT(keyspace = "dox", name = "choice_or_other")
38 public class ChoiceOrOther<E extends Enum<E>> {
39
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";
45
46   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
47
48   @Transient
49   private E choice;
50
51   @Transient
52   private String other;
53
54   private String result;
55
56   public ChoiceOrOther() {
57   }
58
59   /**
60    * Instantiates a new Choice or other.
61    *
62    * @param choice the choice
63    * @param other  the other
64    */
65   public ChoiceOrOther(E choice, String other) {
66     this.choice = choice;
67     this.other = other;
68     result = resolveResult();
69   }
70
71   public E getChoice() {
72     return choice;
73   }
74
75   public void setChoice(E choice) {
76
77     this.choice = choice;
78   }
79
80   public String getOther() {
81     return other;
82   }
83
84   public void setOther(String other) {
85     this.other = other;
86   }
87
88   public String getResult() {
89     return result;
90   }
91
92   /**
93    * Sets result.
94    *
95    * @param result the result
96    */
97   public void setResult(String result) {
98     if (choice != null) {
99       if (result == null) {
100         this.result = resolveResult();
101       }
102     } else {
103       this.result = result;
104     }
105   }
106
107   private String resolveResult() {
108     return OTHER_ENUM_VALUE.equals(choice.name()) ? other : choice.name();
109   }
110
111   /**
112    * Resolve enum.
113    *
114    * @param enumClass the enum class
115    */
116   public void resolveEnum(Class<E> enumClass) {
117     if (choice != null || result == null) {
118       return;
119     }
120
121     try {
122       choice = E.valueOf(enumClass, result);
123     } catch (IllegalArgumentException exception) {
124       log.debug("",exception);
125       try {
126         choice = E.valueOf(enumClass, OTHER_ENUM_VALUE);
127       } catch (IllegalArgumentException ex) {
128         log.debug("",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());
136       }
137       other = result;
138     }
139   }
140
141   @Override
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);
146     return result1;
147   }
148
149   @Override
150   public boolean equals(Object obj) {
151     if (this == obj) {
152       return true;
153     }
154     if (obj == null || getClass() != obj.getClass()) {
155       return false;
156     }
157
158     ChoiceOrOther<?> that = (ChoiceOrOther<?>) obj;
159
160     if (choice != null ? !choice.equals(that.choice) : that.choice != null) {
161       return false;
162     }
163     if (other != null ? !other.equals(that.other) : that.other != null) {
164       return false;
165     }
166     return result != null ? result.equals(that.result) : that.result == null;
167
168   }
169 }