165260f9c5c844bf78c1ef8cdc1f92128cd88652
[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     results = new HashSet<>();
106     if (choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
107       results.add(other);
108     } else {
109       for (E choice : choices) {
110         results.add(choice.name());
111       }
112     }
113
114     return results;
115   }
116
117   /**
118    * Resolve enum.
119    *
120    * @param enumClass the enum class
121    */
122   public void resolveEnum(Class<E> enumClass) {
123     if (choices != null || results == null || results.size() == 0) {
124       return;
125     }
126
127     choices = new HashSet<>();
128     if (results.size() > 1) {
129       for (String result : results) {
130         choices.add(E.valueOf(enumClass, result));
131       }
132     } else {
133       String result = results.iterator().next();
134       try {
135         choices.add(E.valueOf(enumClass, result));
136       } catch (IllegalArgumentException exception) {
137         try {
138           choices.add(E.valueOf(enumClass, OTHER_ENUM_VALUE));
139         } catch (IllegalArgumentException ex) {
140
141           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
142               LoggerTragetServiceName.VALIDATE_CHOICE_VALUE, ErrorLevel.ERROR.name(),
143               LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VALUE);
144           throw new CoreException(new ErrorCode.ErrorCodeBuilder()
145               .withId(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID)
146               .withMessage(MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG)
147               .withCategory(ErrorCategory.APPLICATION).build());
148         }
149         other = result;
150       }
151     }
152   }
153
154   @Override
155   public int hashCode() {
156     int result = choices != null ? choices.hashCode() : 0;
157     result = 31 * result + (other != null ? other.hashCode() : 0);
158     result = 31 * result + (results != null ? results.hashCode() : 0);
159     return result;
160   }
161
162   @Override
163   public boolean equals(Object obj) {
164     if (this == obj) {
165       return true;
166     }
167     if (obj == null || getClass() != obj.getClass()) {
168       return false;
169     }
170
171     MultiChoiceOrOther<?> that = (MultiChoiceOrOther<?>) obj;
172
173     if (choices != null ? !choices.equals(that.choices) : that.choices != null) {
174       return false;
175     }
176     if (other != null ? !other.equals(that.other) : that.other != null) {
177       return false;
178     }
179     return results != null ? results.equals(that.results) : that.results == null;
180
181   }
182 }