/
ListTypeContract.java
64 lines (55 loc) · 2.27 KB
/
ListTypeContract.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.scalar.am.contract;
import com.scalar.ledger.asset.Asset;
import com.scalar.ledger.contract.Contract;
import com.scalar.ledger.database.AssetFilter;
import com.scalar.ledger.exception.ContractContextException;
import com.scalar.ledger.ledger.Ledger;
import java.util.List;
import java.util.Optional;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
public class ListTypeContract extends Contract {
public static final String TYPE = "type";
public static final String AGE = "age";
public static final String TYPES = "types";
public static final String NAME = "name";
public static final String HOLDER_ID = "holderId";
private static final String RESULT = "result";
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
private static final String MESSAGE = "message";
@Override
public JsonObject invoke(Ledger ledger, JsonObject argument, Optional<JsonObject> property) {
if (!property.isPresent() || !property.get().containsKey(HOLDER_ID)) {
throw new ContractContextException("property: `" + HOLDER_ID + "` is mandatory.");
}
String holderId = property.get().getString(HOLDER_ID);
/**
* Registered types will be put into key: type, thus the history of key: type is just the list
* of registered types E.g. history of type = [ { "name": "book" }, // age: 0 { "name": "phone"
* } // age: 1 ]
*/
AssetFilter filter = new AssetFilter(holderId + "-" + TYPE);
List<Asset> history = ledger.scan(filter);
if (history.isEmpty()) {
return Json.createObjectBuilder()
.add(RESULT, FAILURE)
.add(MESSAGE, "No types were registered. Use am add-type to create one.")
.build();
}
JsonArrayBuilder builder = Json.createArrayBuilder();
for (Asset h : history) {
JsonObject type =
Json.createObjectBuilder().add(TYPE, h.data().getString(NAME)).add(AGE, h.age()).build();
builder.add(type);
}
JsonArray types = builder.build();
return Json.createObjectBuilder()
.add(RESULT, SUCCESS)
.add(MESSAGE, "get list completed.")
.add(TYPES, types)
.build();
}
}