use of gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter in project coastal-hazards by USGS-CIDA.
the class TreeResource method getRootTrees.
@GET
@Path("/item")
@Produces(MediaType.APPLICATION_JSON)
public Response getRootTrees(@Context Request request) {
Response response = null;
try (ItemManager itemManager = new ItemManager()) {
List<Item> items = itemManager.loadRootItems();
Gson treeGson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemTreeAdapter()).create();
JsonObject root = new JsonObject();
JsonArray rootItems = new JsonArray();
for (Item item : items) {
rootItems.add(treeGson.toJsonTree(item));
}
root.add("items", rootItems);
response = Response.ok(root.toString(), MediaType.APPLICATION_JSON_TYPE).build();
} catch (Exception e) {
log.error(e.toString());
}
return response;
}
use of gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter in project coastal-hazards by USGS-CIDA.
the class TreeResource method getOrphans.
@GET
@Path("/item/orphans")
@Produces(MediaType.APPLICATION_JSON)
public Response getOrphans(@Context Request request) {
Response response = null;
Item curItem = null;
try (ItemManager itemManager = new ItemManager()) {
List<Item> items = itemManager.loadRootItems();
Gson treeGson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemTreeAdapter()).create();
JsonObject root = new JsonObject();
JsonArray orphans = new JsonArray();
for (Item item : items) {
if (item.getId() == null) {
log.error("Item has null id!!" + item.toString());
break;
}
if (!item.getId().equals(Item.UBER_ID)) {
curItem = item;
orphans.add(treeGson.toJsonTree(item));
}
}
root.add("items", orphans);
response = Response.ok(root.toString(), MediaType.APPLICATION_JSON_TYPE).build();
} catch (Exception e) {
log.error(e.toString() + curItem.getId());
}
return response;
}
use of gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter in project coastal-hazards by USGS-CIDA.
the class TreeResource method getTree.
@GET
@Path("/item/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getTree(@PathParam("id") String id, @Context Request request) {
Response response = null;
Item item = null;
try (ItemManager itemManager = new ItemManager()) {
item = itemManager.load(id);
} catch (Exception e) {
log.error(e.toString());
}
if (item == null) {
throw new NotFoundException();
} else {
Gson treeGson = new GsonBuilder().registerTypeAdapter(Item.class, new ItemTreeAdapter()).create();
String jsonResult = treeGson.toJson(item);
response = Response.ok(jsonResult, MediaType.APPLICATION_JSON_TYPE).build();
}
return response;
}
Aggregations