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