8adfecf44e62e73ae911d4d7cd0270a6ae744012
[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.apache.commons.collections4.CollectionUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCategory;
24 import org.openecomp.sdc.common.errors.ErrorCode;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31
32 import java.util.HashSet;
33 import java.util.Set;
34
35 @UDT(keyspace = "dox", name = "multi_choice_or_other")
36 public class MultiChoiceOrOther<E extends Enum<E>> {
37   private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID =
38       "MULTI_CHOICE_OR_OTHER_INVALID_ENUM_ERR_ID";
39   private static final String MULTI_CHOICE_OR_OTHER_INVALID_ENUM_MSG =
40       "Enum used as part of MultiChoiceOrOther type must contain the value 'Other'";
41   public static final String OTHER_ENUM_VALUE = "Other";
42
43   @Transient
44   private Set<E> choices;
45   @Transient
46   private String other;
47
48
49   private Set<String> results;
50
51   public MultiChoiceOrOther() {
52     // Default constructor
53   }
54
55   /**
56    * Instantiates a new Multi choice or other.
57    *
58    * @param choices the choices
59    * @param other   the other
60    */
61   public MultiChoiceOrOther(Set<E> choices, String other) {
62     this.choices = choices;
63     this.other = other;
64     results = resolveResult();
65   }
66
67   public Set<E> getChoices() {
68     return choices;
69   }
70
71   public void setChoices(Set<E> choices) {
72     this.choices = choices;
73   }
74
75   public String getOther() {
76     return other;
77   }
78
79   public void setOther(String other) {
80     this.other = other;
81   }
82
83   public Set<String> getResults() {
84     return results;
85   }
86
87   /**
88    * Sets results.
89    *
90    * @param results the results
91    */
92   public void setResults(Set<String> results) {
93     if (choices != null) {
94       if (results == null) {
95         this.results = resolveResult();
96       }
97     } else {
98       this.results = results;
99     }
100   }
101
102   private Set<String> resolveResult() {
103     if (choices != null) {
104         results = new HashSet<>();
105         if (choices.size() == 1 && OTHER_ENUM_VALUE.equals(choices.iterator().next().name())) {
106             results.add(other);
107         } else {
108             for (E choice : choices) {
109                 results.add(choice.name());
110             }
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 || CollectionUtils.isEmpty(results)) {
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 }