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