0acd051004d839fb3e8d57c2f78a1c763ec09b29
[sdc.git] /
1 /*
2 * Copyright © 2016-2018 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
18 package org.openecomp.sdc.action.types;
19
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Set;
26
27 import org.openecomp.sdc.action.ActionConstants;
28 import org.openecomp.sdc.action.dao.types.ActionEntity;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30
31 public class Action implements Comparable {
32   private String actionUuId;
33   private String actionInvariantUuId;
34   private String version;
35   private ActionStatus status;
36   private String name;
37   private String displayName;
38   private List<String> vendorList;
39   private List<String> categoryList;
40   private Date timestamp;
41   private String user;
42   private List<Map<String, String>> supportedModels;
43   private List<Map<String, String>> supportedComponents;
44   //private List<HashMap<String,String>> artifacts;
45   private List<ActionArtifact> artifacts;
46   private String data;
47
48   public Action() {
49   }
50
51   /**
52    * Instantiates a new Action.
53    *
54    * @param action the action
55    */
56   public Action(Action action) {
57     this.actionUuId = action.getActionUuId();
58     this.actionInvariantUuId = action.getActionInvariantUuId();
59     this.name = action.getName();
60     this.setDisplayName(action.getDisplayName());
61     this.setVendorList(action.getVendorList());
62     this.setCategoryList(action.getCategoryList());
63     this.setTimestamp(action.getTimestamp());
64     this.setUser(action.getUser());
65     this.version = action.getVersion();
66     this.status = action.getStatus();
67     this.data = action.getData();
68     this.supportedComponents = action.getSupportedComponents();
69     this.supportedModels = action.getSupportedModels();
70     this.artifacts = action.getArtifacts();
71   }
72
73   public String getActionUuId() {
74     return actionUuId;
75   }
76
77   public void setActionUuId(String actionUuId) {
78     this.actionUuId = actionUuId;
79   }
80
81   public String getActionInvariantUuId() {
82     return actionInvariantUuId;
83   }
84
85   public void setActionInvariantUuId(String actionInvariantUuId) {
86     this.actionInvariantUuId = actionInvariantUuId;
87   }
88
89   public String getVersion() {
90     return version;
91   }
92
93   public void setVersion(String version) {
94     this.version = version;
95   }
96
97   public ActionStatus getStatus() {
98     return status;
99   }
100
101   public void setStatus(ActionStatus status) {
102     this.status = status;
103   }
104
105   public String getName() {
106     return name;
107   }
108
109   public void setName(String name) {
110     this.name = name;
111   }
112
113   public String getDisplayName() {
114     return displayName;
115   }
116
117   public void setDisplayName(String displayName) {
118     this.displayName = displayName;
119   }
120
121   public List<String> getVendorList() {
122     return vendorList;
123   }
124
125   public void setVendorList(List<String> vendorList) {
126     this.vendorList = vendorList;
127   }
128
129   public List<String> getCategoryList() {
130     return categoryList;
131   }
132
133   public void setCategoryList(List<String> categoryList) {
134     this.categoryList = categoryList;
135   }
136
137   public Date getTimestamp() {
138     return timestamp;
139   }
140
141   public void setTimestamp(Date timestamp) {
142     this.timestamp = timestamp;
143   }
144
145   public String getUser() {
146     return user;
147   }
148
149   public void setUser(String user) {
150     this.user = user;
151   }
152
153   public List<Map<String, String>> getSupportedModels() {
154     return supportedModels;
155   }
156
157   public void setSupportedModels(List<Map<String, String>> supportedModels) {
158     this.supportedModels = supportedModels;
159   }
160
161   public List<Map<String, String>> getSupportedComponents() {
162     return supportedComponents;
163   }
164
165   public void setSupportedComponents(List<Map<String, String>> supportedComponents) {
166     this.supportedComponents = supportedComponents;
167   }
168
169
170   public List<ActionArtifact> getArtifacts() {
171     return artifacts;
172   }
173
174   public void setArtifacts(List<ActionArtifact> artifacts) {
175     this.artifacts = artifacts;
176   }
177
178   public String getData() {
179     return data;
180   }
181
182   public void setData(String data) {
183     this.data = data;
184   }
185
186   /**
187    * To entity action entity.
188    *
189    * @return the action entity
190    */
191   public ActionEntity toEntity() {
192     ActionEntity destination = new ActionEntity();
193
194     destination
195         .setActionUuId(this.getActionUuId() != null ? this.getActionUuId().toUpperCase() : null);
196     destination.setActionInvariantUuId(
197         this.getActionInvariantUuId() != null ? this.getActionInvariantUuId().toUpperCase() : null);
198     destination.setName(this.getName() != null ? this.getName().toLowerCase() : null);
199     if (Objects.nonNull(this.getVendorList())) {
200       destination.setVendorList(new HashSet<>(this.getVendorList()));
201     }
202     if (Objects.nonNull(this.getCategoryList())) {
203       destination.setCategoryList(new HashSet<>(this.getCategoryList()));
204     }
205     destination.setTimestamp(this.getTimestamp());
206     destination.setUser(this.getUser());
207     destination.setVersion(Version.valueOf(this.getVersion()));
208     if (this.getStatus() != null) {
209       destination.setStatus(this.getStatus().name());
210     }
211     destination.setSupportedComponents(getIdFromMap(this.getSupportedComponents(),
212         ActionConstants.SUPPORTED_COMPONENTS_ID));
213     destination.setSupportedModels(
214         getIdFromMap(this.getSupportedModels(), ActionConstants.SUPPORTED_MODELS_VERSION_ID));
215     destination.setData(this.getData());
216     return destination;
217   }
218
219   private Set<String> getIdFromMap(List<Map<String, String>> map, String idName) {
220     Set<String> list = new HashSet<>();
221     if (map != null && !map.isEmpty()) {
222       for (Map<String, String> entry : map) {
223         if (entry.containsKey(idName)) {
224           list.add(entry.get(idName) != null ? entry.get(idName).toLowerCase() : null);
225         }
226       }
227       return list;
228     }
229     return null;
230   }
231
232   @Override
233   public int compareTo(Object object) {
234     Action obj = (Action) object;
235     Version thisVersion = Version.valueOf(this.version);
236     Version objVersion = Version.valueOf(obj.getVersion());
237     if (obj.getName().compareTo(this.getName()) == 0) {
238       return compareVersions(objVersion, thisVersion);
239     }
240     return obj.getName().compareTo(this.getName());
241   }
242
243   private int compareVersions(Version objVersion, Version thisVersion) {
244     if (objVersion.getMajor() == thisVersion.getMajor()) {
245       return Integer.compare(objVersion.getMinor(), thisVersion.getMinor());
246     }
247     return Integer.compare(objVersion.getMajor(), thisVersion.getMajor());
248   }
249
250   @Override
251   public boolean equals(Object o) {
252     if (this == o) {
253       return true;
254     }
255     if (o == null || getClass() != o.getClass()) {
256       return false;
257     }
258
259     Action action = (Action) o;
260
261     if (!version.equals(action.version)) {
262       return false;
263     }
264     if (!name.equals(action.name)) {
265       return false;
266     }
267
268     return true;
269   }
270
271   @Override
272   public int hashCode() {
273     int result = version.hashCode();
274     result = 31 * result + name.hashCode();
275     return result;
276   }
277 }