Search in sources :

Example 1 with ItemTreeAdapter

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;
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ItemTreeAdapter(gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with ItemTreeAdapter

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;
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ItemTreeAdapter(gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with ItemTreeAdapter

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;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) GsonBuilder(com.google.gson.GsonBuilder) NotFoundException(javax.ws.rs.NotFoundException) Gson(com.google.gson.Gson) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ItemTreeAdapter(gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)3 ItemTreeAdapter (gov.usgs.cida.coastalhazards.gson.adapter.ItemTreeAdapter)3 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)3 Item (gov.usgs.cida.coastalhazards.model.Item)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Response (javax.ws.rs.core.Response)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2