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