|
1 | | -/* |
2 | | - * Copyright 2014, BlobCity iSolutions Pvt. Ltd. |
3 | | - */ |
4 | | -package com.blobcity.db; |
5 | | - |
6 | | -import com.blobcity.db.exceptions.DbOperationException; |
7 | | -import com.google.gson.Gson; |
8 | | -import com.google.gson.JsonElement; |
9 | | -import com.google.gson.JsonObject; |
10 | | -import com.google.gson.JsonParser; |
11 | | -import com.google.gson.reflect.TypeToken; |
12 | | -import java.util.List; |
13 | | - |
14 | | -/** |
15 | | - * Class to wrap responses from the database for internal representation before it gets returned to the client |
16 | | - * |
17 | | - * @author Karun AB |
18 | | - */ |
19 | | -class DbQueryResponse { |
20 | | - |
21 | | - // Ack code |
22 | | - private final int ackCode; |
23 | | - // Select keys |
24 | | - private final List keys; |
25 | | - // Contains |
26 | | - private final boolean contains; |
27 | | - // Error handling |
28 | | - private final String errorCode; |
29 | | - private final String errorCause; |
30 | | - // Response data |
31 | | - private final JsonElement payload; |
32 | | - |
33 | | - public DbQueryResponse(final String response) { |
34 | | - final JsonObject jsonObj = new JsonParser().parse(response).getAsJsonObject(); |
35 | | - |
36 | | - ackCode = jsonObj.get(QueryConstants.ACK).getAsInt(); |
37 | | - keys = new Gson().fromJson(jsonObj.get(QueryConstants.KEYS), new TypeToken<List>() { |
38 | | - }.getType()); |
39 | | - |
40 | | - final JsonElement containsElement = jsonObj.get(QueryConstants.CONTAINS); |
41 | | - contains = containsElement != null ? jsonObj.get(QueryConstants.CONTAINS).getAsBoolean() : false; |
42 | | - |
43 | | - final JsonElement codeElement = jsonObj.get(QueryConstants.CODE); |
44 | | - errorCode = codeElement != null ? jsonObj.get(QueryConstants.CODE).getAsString() : null; |
45 | | - final JsonElement causeElement = jsonObj.get(QueryConstants.CAUSE); |
46 | | - errorCause = causeElement != null ? jsonObj.get(QueryConstants.CAUSE).getAsString() : null; |
47 | | - |
48 | | - payload = jsonObj.get(QueryConstants.PAYLOAD); |
49 | | - } |
50 | | - |
51 | | - public int getAckCode() { |
52 | | - return ackCode; |
53 | | - } |
54 | | - |
55 | | - public boolean isSuccessful() { |
56 | | - return ackCode == 1; |
57 | | - } |
58 | | - |
59 | | - public List getKeys() { |
60 | | - return keys; |
61 | | - } |
62 | | - |
63 | | - public boolean contains() { |
64 | | - return contains; |
65 | | - } |
66 | | - |
67 | | - public String getErrorCode() { |
68 | | - return errorCode; |
69 | | - } |
70 | | - |
71 | | - public String getErrorCause() { |
72 | | - return errorCause; |
73 | | - } |
74 | | - |
75 | | - public DbOperationException createException() { |
76 | | - return new DbOperationException(errorCode, errorCause); |
77 | | - } |
78 | | - |
79 | | - public JsonElement getPayload() { |
80 | | - return payload; |
81 | | - } |
82 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2014, BlobCity iSolutions Pvt. Ltd. |
| 3 | + */ |
| 4 | +package com.blobcity.db; |
| 5 | + |
| 6 | +import com.blobcity.db.exceptions.DbOperationException; |
| 7 | +import com.google.gson.Gson; |
| 8 | +import com.google.gson.JsonElement; |
| 9 | +import com.google.gson.JsonObject; |
| 10 | +import com.google.gson.JsonParser; |
| 11 | +import com.google.gson.reflect.TypeToken; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class to wrap responses from the database for internal representation before it gets returned to the client |
| 16 | + * |
| 17 | + * @author Karun AB |
| 18 | + */ |
| 19 | +public class DbQueryResponse { |
| 20 | + |
| 21 | + // Ack code |
| 22 | + private final int ackCode; |
| 23 | + // Select keys |
| 24 | + private final List keys; |
| 25 | + // Contains |
| 26 | + private final boolean contains; |
| 27 | + // Error handling |
| 28 | + private final String errorCode; |
| 29 | + private final String errorCause; |
| 30 | + // Response data |
| 31 | + private final JsonElement payload; |
| 32 | + |
| 33 | + public DbQueryResponse(final String response) { |
| 34 | + final JsonObject jsonObj = new JsonParser().parse(response).getAsJsonObject(); |
| 35 | + |
| 36 | + ackCode = jsonObj.get(QueryConstants.ACK).getAsInt(); |
| 37 | + keys = new Gson().fromJson(jsonObj.get(QueryConstants.KEYS), new TypeToken<List>() { |
| 38 | + }.getType()); |
| 39 | + |
| 40 | + final JsonElement containsElement = jsonObj.get(QueryConstants.CONTAINS); |
| 41 | + contains = containsElement != null ? jsonObj.get(QueryConstants.CONTAINS).getAsBoolean() : false; |
| 42 | + |
| 43 | + final JsonElement codeElement = jsonObj.get(QueryConstants.CODE); |
| 44 | + errorCode = codeElement != null ? jsonObj.get(QueryConstants.CODE).getAsString() : null; |
| 45 | + final JsonElement causeElement = jsonObj.get(QueryConstants.CAUSE); |
| 46 | + errorCause = causeElement != null ? jsonObj.get(QueryConstants.CAUSE).getAsString() : null; |
| 47 | + |
| 48 | + payload = jsonObj.get(QueryConstants.PAYLOAD); |
| 49 | + } |
| 50 | + |
| 51 | + public int getAckCode() { |
| 52 | + return ackCode; |
| 53 | + } |
| 54 | + |
| 55 | + public boolean isSuccessful() { |
| 56 | + return ackCode == 1; |
| 57 | + } |
| 58 | + |
| 59 | + public List getKeys() { |
| 60 | + return keys; |
| 61 | + } |
| 62 | + |
| 63 | + public boolean contains() { |
| 64 | + return contains; |
| 65 | + } |
| 66 | + |
| 67 | + public String getErrorCode() { |
| 68 | + return errorCode; |
| 69 | + } |
| 70 | + |
| 71 | + public String getErrorCause() { |
| 72 | + return errorCause; |
| 73 | + } |
| 74 | + |
| 75 | + public DbOperationException createException() { |
| 76 | + return new DbOperationException(errorCode, errorCause); |
| 77 | + } |
| 78 | + |
| 79 | + public JsonElement getPayload() { |
| 80 | + return payload; |
| 81 | + } |
| 82 | +} |
0 commit comments