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