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