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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations