Search in sources :

Example 16 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemAdapter method serialize.

@Override
public JsonElement serialize(Item src, Type typeOfSrc, JsonSerializationContext context) {
    JsonElement item = GsonUtil.getDefault().toJsonTree(src, typeOfSrc);
    if (item instanceof JsonObject) {
        JsonObject json = (JsonObject) item;
        List<Item> items = src.getChildren();
        if (items != null) {
            JsonArray children = new JsonArray();
            for (Item child : items) {
                children.add(context.serialize(child));
            }
            json.add("children", children);
        }
        item = json;
    }
    return item;
}
Also used : JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 17 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemAdapter method deserialize.

@Override
public Item deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Item result = null;
    Object defaultObj = GsonUtil.getDefault().fromJson(json, typeOfT);
    if (defaultObj instanceof Item) {
        result = (Item) defaultObj;
        if (json instanceof JsonObject) {
            JsonObject itemJson = (JsonObject) json;
            JsonElement children = itemJson.get("children");
            if (children instanceof JsonArray) {
                JsonArray childrenArray = (JsonArray) children;
                List<Item> childrenList = new ArrayList<>();
                Iterator<JsonElement> iterator = childrenArray.iterator();
                while (iterator.hasNext()) {
                    JsonElement childItem = iterator.next();
                    try {
                        Item childItemObj = null;
                        if (childItem.isJsonPrimitive()) {
                            childItemObj = new Item();
                            childItemObj.setId(childItem.getAsString());
                        } else if (childItem.isJsonObject()) {
                            childItemObj = (Item) context.deserialize(childItem, Item.class);
                        } else {
                            throw new JsonParseException("Need a list of primatives or objects");
                        }
                        childrenList.add(childItemObj);
                    } catch (JsonParseException ex) {
                        log.debug("Problem deserializing children", ex);
                    }
                }
                result.setChildren(childrenList);
            }
        }
    }
    return result;
}
Also used : JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 18 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemTreeAdapter method serialize.

@Override
public JsonElement serialize(Item src, Type typeOfSrc, JsonSerializationContext context) {
    JsonElement fullItem = GsonUtil.getDefault().toJsonTree(src, typeOfSrc);
    JsonObject simplified = new JsonObject();
    if (fullItem instanceof JsonObject) {
        JsonObject json = (JsonObject) fullItem;
        simplified.add("id", json.get("id"));
        simplified.add("itemType", json.get("itemType"));
        simplified.add("title", json.getAsJsonObject("summary").getAsJsonObject("full").get("title"));
        simplified.add("displayedChildren", json.getAsJsonArray("displayedChildren"));
        List<Item> items = src.getChildren();
        if (items != null) {
            JsonArray children = new JsonArray();
            for (Item child : items) {
                children.add(context.serialize(child));
            }
            simplified.add("children", children);
        }
    } else {
        throw new IllegalStateException("Item must be an object");
    }
    return simplified;
}
Also used : JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 19 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemManager method findAncestors.

private List<Item> findAncestors(Item item) {
    List<Item> items = new ArrayList<>();
    List<String> idList = findAncestorItemIds(item);
    for (String result : idList) {
        Item ancestor = load(result);
        if (ancestor != null) {
            items.add(ancestor);
        }
    }
    return items;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) ArrayList(java.util.ArrayList)

Example 20 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemManager method load.

public Item load(String itemId) {
    Item item = null;
    try {
        item = em.find(Item.class, itemId);
        if (item != null) {
            List<Item> children = item.getChildren();
            List<Item> replaceList = new LinkedList<>();
            if (children != null) {
                for (Item child : children) {
                    if (child != null) {
                        replaceList.add(load(child.getId()));
                    }
                }
                item.setChildren(replaceList);
            }
        }
    } catch (PersistenceException ex) {
        log.error("Unable to load item", ex);
    }
    return item;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) PersistenceException(javax.persistence.PersistenceException) LinkedList(java.util.LinkedList)

Aggregations

Item (gov.usgs.cida.coastalhazards.model.Item)60 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)26 Response (javax.ws.rs.core.Response)24 Path (javax.ws.rs.Path)22 LinkedList (java.util.LinkedList)21 GET (javax.ws.rs.GET)20 Produces (javax.ws.rs.Produces)20 HashMap (java.util.HashMap)16 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)12 Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)12 JsonObject (com.google.gson.JsonObject)10 ArrayList (java.util.ArrayList)10 Viewable (org.glassfish.jersey.server.mvc.Viewable)10 Gson (com.google.gson.Gson)9 Service (gov.usgs.cida.coastalhazards.model.Service)8 NotFoundException (javax.ws.rs.NotFoundException)8 Test (org.junit.Test)8 JsonElement (com.google.gson.JsonElement)7 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)7 Status (gov.usgs.cida.coastalhazards.model.util.Status)7