Sonar Fix: JsonResponse.java
[music.git] / src / main / java / org / onap / music / response / jsonobjects / JsonResponse.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *
7  *  Modifications Copyright (C) 2019 IBM.
8  * ===================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.response.jsonobjects;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
33 import org.onap.music.main.ResultType;
34
35
36 import io.swagger.annotations.ApiModel;
37 import io.swagger.annotations.ApiModelProperty;
38
39 @ApiModel(value = "JsonResponse", description = "General Response JSON")
40 public class JsonResponse {
41
42     /* Status is required */
43     private ResultType status;
44     
45     /* Standard informational fields */
46     private String error;
47     private String message;
48     
49     /* versioning */
50     private String musicVersion;
51     private String musicBuild;
52     
53     /* Data Fields */
54     private Map<String, HashMap<String, Object>> dataResult;
55     
56     /* Locking fields */
57     private String lock;
58     private LockStatus lockStatus;
59     private List<String> lockHolders;
60     private String lockLease;
61     private boolean isLockHolders=false;
62
63     /**
64      * Create a JSONLock Response
65      * Use setters to provide more information as in
66      * JsonLockResponse(ResultType.SUCCESS).setMessage("We did it").setLock(mylockname)
67      * @param status
68      */
69     public JsonResponse(ResultType status) {
70         this.status = status;
71     }
72
73     public boolean isLockHolders() {
74                 return isLockHolders;
75         }
76
77         public JsonResponse setisLockHolders(boolean isLockHolders) {
78                 this.isLockHolders = isLockHolders;
79                 return this;
80         }
81
82     /**
83      * 
84      * @return
85      */
86     @ApiModelProperty(value = "Overall status of the response.",
87                     allowableValues = "Success,Failure")
88     public ResultType getStatus() {
89         return status;
90     }
91
92     /**
93      * 
94      * @param status
95      */
96     public JsonResponse setStatus(ResultType status) {
97         this.status = status;
98         return this;
99     }
100
101     /**
102      * 
103      * @return the error
104      */
105     @ApiModelProperty(value = "Error value")
106     public String getError() {
107         return error;
108     }
109
110     /**
111      * 
112      * @param error
113      */
114     public JsonResponse setError(String error) {
115         this.error = error;
116         return this;
117     }
118     
119     /**
120      * 
121      * @return the message
122      */
123     @ApiModelProperty(value = "Message value")
124     public String getMessage() {
125         return message;
126     }
127
128     /**
129      * 
130      * @param message
131      */
132     public JsonResponse setMessage(String message) {
133         this.message = message;
134         return this;
135     }
136     
137     
138     /**
139      * .
140      * @return the music version
141      */
142     public String getMusicVersion() {
143         return this.musicVersion;
144     }
145     
146     /**
147      * .
148      * @param version of music
149      * @return
150      */
151     public JsonResponse setMusicVersion(String version) {
152         this.musicVersion = version;
153         return this;
154     }
155
156     /**
157      * .
158      * @return the music version
159      */
160     public String getMusicBuild() {
161         return this.musicBuild;
162     }
163     
164     /**
165      * .
166      * @param build of music
167      * @return
168      */
169     public JsonResponse setMusicBuild(String build) {
170         this.musicBuild = build;
171         return this;
172     }
173
174
175     public Map<String, HashMap<String, Object>> getDataResult() {
176         return this.dataResult;
177     }
178     
179     public JsonResponse setDataResult(Map<String, HashMap<String, Object>> map) {
180         this.dataResult = map;
181         return this;
182     }
183
184     /**
185      * 
186      * @return
187      */
188     public String getLock() {
189         return lock;
190     }
191
192     /**
193      * 
194      * @param lock
195      */
196     public JsonResponse setLock(String lock) {
197         this.lock = lock;
198         return this;
199     }
200     
201     /**
202      * 
203      * @return the lockStatus
204      */
205     @ApiModelProperty(value = "Status of the lock")
206     public LockStatus getLockStatus() {
207         return lockStatus;
208     }
209
210     /**
211      * 
212      * @param lockStatus
213      */
214     public JsonResponse setLockStatus(LockStatus lockStatus) {
215         this.lockStatus = lockStatus;
216         return this;
217     }
218
219     /**
220      * 
221      * 
222      * @return the lockHolder
223      */
224     @ApiModelProperty(value = "Holder of the Lock")
225     public List<String> getLockHolder() {
226         return lockHolders;
227     }
228
229     /**
230      * 
231      * @param lockHolder
232      */
233     public JsonResponse setLockHolder(String lockHolder) {
234         this.lockHolders = new ArrayList<String>();
235         this.lockHolders.add(lockHolder);
236         return this;
237     }
238
239     public JsonResponse setLockHolder(List<String> lockHolders) {
240         this.lockHolders = lockHolders;
241         return this;
242     }
243
244
245     /**
246      * @return the lockLease
247      */
248     public String getLockLease() {
249         return lockLease;
250     }
251
252     /**
253      * @param lockLease the lockLease to set
254      */
255     public JsonResponse setLockLease(String lockLease) {
256         this.lockLease = lockLease;
257         return this;
258     }
259
260     /**
261      * Convert to Map
262      * 
263      * @return
264      */
265     public Map<String, Object> toMap() {
266         Map<String, Object> fullMap = new HashMap<>();
267         fullMap.put("status", status);
268         if (error != null && !"".equals(error)) { 
269             fullMap.put("error", error); 
270         }
271         if (message != null) { 
272             fullMap.put("message", message); 
273         }
274         
275         if (musicVersion != null) {
276             fullMap.put("version", musicVersion);
277         }
278         
279         if (musicBuild != null) {
280             fullMap.put("build", musicBuild);
281         }
282     
283         if (dataResult != null) {
284             fullMap.put("result", dataResult);
285         }
286         
287         if (lock != null) {
288             Map<String, Object> lockMap = new HashMap<>();
289             if (lock != null) {
290                 lockMap.put("lock", lock);
291             }
292             if (lockStatus != null) {
293                 lockMap.put("lock-status", lockStatus);
294             }
295             if (lockHolders != null && !lockHolders.isEmpty()) {
296                 if (lockHolders.size()==1 && !isLockHolders) {
297                     //for backwards compatability
298                     lockMap.put("lock-holder", lockHolders.get(0));
299                 } else {
300                     lockMap.put("lock-holder", lockHolders);
301                 }
302             }
303             if (lockLease != null) {
304                 lockMap.put("lock-lease", lockLease);
305             }
306             fullMap.put("lock", lockMap);
307         }
308
309         return fullMap;
310     }
311
312     /**
313      * Convert to String
314      */
315     @Override
316     public String toString() {
317         return "JsonLockResponse [status=" + status + ", error=" + error + ", message=" + message
318                         + ", lock=" + lock + ", lockStatus=" + lockStatus + ", lockHolder="
319                         + lockHolders + "]";
320     }
321
322 }