[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-api / src / main / java / org / openecomp / sdc / vendorlicense / dao / types / MultiChoiceOrOther.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 import java.util.HashSet;
36 import java.util.Set;
37
38 @UDT(keyspace = "dox", name = "multi_choice_or_other")
39 public class MultiChoiceOrOther<E extends Enum<E>> {
40   public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
41       "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
42   public static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
43       "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
44   public static final String OTHER_ENUM_VALUE = "Other";
45
46   @Transient
47   private Set<E> choices;
48   @Transient
49   private String other;
50
51
52   private Set<String> results;
53
54   public MultiChoiceOrOther() {
55   }
56
57   /**
58    * Instantiates a new Multi choice or other.
59    *
60    * @param choices the choices
61    * @param other   the other
62    */
63   public MultiChoiceOrOther(Set<E> choices, String other) {
64     this.choices = choices;
65     this.other = other;
66     results = resolveResult();
67   }
68
69   public Set<E> getChoices() {
70     return choices;
71   }
72
73   public void setChoices(Set<E> choices) {
74     this.choices = choices;
75   }
76
77   public String getOther() {
78     return other;
79   }
80
81   public void setOther(String other) {
82     this.other = other;
83   }
84
85   public Set<String> getResults() {
86     return results;
87   }
88
89   /**
90    * Sets results.
91    *
92    * @param results the results
93    */
94   public void setResults(Set<String> results) {
95     if (choices != null) {
96       if (results == null) {
97         this.results = resolveResult();
98       }
99     } else {
100       this.results = results;
101     }
102   }
103
104   private Set<String> resolveResult() {
105     if (choices != null) {
106         results = new HashSet<>();
107         if(choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
108             results.add(other);
109         } else {
110             for (E choice : choices) {
111                 results.add(choice.name());
112             }
113         }
114     }
115
116     return results;
117   }
118
119   /**
120    * Resolve enum.
121    *
122    * @param enumClass the enum class
123    */
124   public void resolveEnum(Class<E> enumClass) {
125     if (choices != null || results == null || results.size() == 0) {
126       return;
127     }
128
129     choices = new HashSet<>();
130     if (results.size() > 1) {
131       for (String result : results) {
132         choices.add(E.valueOf(enumClass, result));
133       }
134     } else {
135       String result = results.iterator().next();
136       try {
137         choices.add(E.valueOf(enumClass, result));
138       } catch (IllegalArgumentException exception) {
139         try {
140           choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
141         } catch (IllegalArgumentException ex) {
142
143           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
144               LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
145               LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
146           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
147               .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
148               .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
149               .withCategory(ErrorCategory.APPLICATION).build());
150         }
151         other = result;
152       }
153     }
154   }
155
156   @Override
157   public int hashCode() {
158     int result = choices != null ? choices.hashCode() : 0;
159     result = 31 * result + (other != null ? other.hashCode() : 0);
160     result = 31 * result + (results != null ? results.hashCode() : 0);
161     return result;
162   }
163
164   @Override
165   public boolean equals(Object obj) {
166     if (this == obj) {
167       return true;
168     }
169     if (obj == null || getClass() != obj.getClass()) {
170       return false;
171     }
172
173     MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
174
175     if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
176       return false;
177     }
178     if (other != null ? !other.equals(that.other) : that.other != null) {
179       return false;
180     }
181     return results != null ? results.equals(that.results) : that.results == null;
182
183   }
184 }