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