use of gov.usgs.cida.coastalhazards.jpa.ThumbnailManager in project coastal-hazards by USGS-CIDA.
the class TreeResource method updateItemChildren.
/**
* Updates one or more items for children and displayed children
*
* @return whether items were updated in the database or not
*/
private boolean updateItemChildren(Map<String, JsonObject> items) {
List<Item> itemList = new LinkedList<>();
boolean updated = false;
for (Entry<String, JsonObject> entry : items.entrySet()) {
String itemId = entry.getKey();
JsonObject updateData = entry.getValue();
if (updateData.has("children")) {
Item parentItem;
List<Item> children;
try (ItemManager manager = new ItemManager()) {
parentItem = manager.load(itemId);
children = new LinkedList<>();
log.info("Attempting to update item {}", parentItem.getId());
// Update the item's children
Iterator<JsonElement> iterator = updateData.get("children").getAsJsonArray().iterator();
while (iterator.hasNext()) {
String childId = iterator.next().getAsString();
Item child = manager.load(childId);
children.add(child);
}
parentItem.setChildren(children);
// Update the item's displayedChildren
if (updateData.has("displayedChildren")) {
Iterator<JsonElement> displayedIterator = updateData.get("displayedChildren").getAsJsonArray().iterator();
List<String> displayedChildren = new ArrayList<>();
while (displayedIterator.hasNext()) {
String childId = displayedIterator.next().getAsString();
displayedChildren.add(childId);
}
parentItem.setDisplayedChildren(displayedChildren);
}
}
itemList.add(parentItem);
} else {
log.error("Incoming JSON Object {} has no children");
throw new BadRequestException();
}
}
// in the database
if (!itemList.isEmpty()) {
// Update the children
try (ItemManager manager = new ItemManager()) {
updated = manager.mergeAll(itemList);
}
if (updated) {
log.info("Updated {} items", itemList.size());
// Update the thumbnails
try (ThumbnailManager thumbMan = new ThumbnailManager()) {
for (Item item : itemList) {
thumbMan.updateDirtyBits(item.getId());
}
log.debug("Updated thumbs for {} items", itemList.size());
}
// Update the status manager
try (StatusManager statusMan = new StatusManager()) {
Status status = new Status();
status.setStatusName(Status.StatusName.STRUCTURE_UPDATE);
if (statusMan.save(status)) {
log.debug("Status Manager updated structure status after items were updated.");
} else {
log.warn("Status Manager did not update the structure status after updating items. This could lead to inconsistencies in the data");
}
} catch (Exception e) {
log.error(e.toString());
}
} else {
log.warn("Could not update {} items.", itemList.size());
}
}
return updated;
}
use of gov.usgs.cida.coastalhazards.jpa.ThumbnailManager in project coastal-hazards by USGS-CIDA.
the class ItemResource method postItem.
/**
* Only allows one card to be posted at a time for now
*
* @param content Posted content as text string (should be JSON)
* @param request passed through context of request
* @return
*/
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postItem(String content, @Context HttpServletRequest request) {
Response response;
Item item = Item.fromJSON(content);
final String id;
try (ItemManager itemManager = new ItemManager()) {
id = itemManager.persist(item);
}
if (null == id) {
throw new BadRequestException();
} else {
Map<String, Object> ok = new HashMap<String, Object>() {
private static final long serialVersionUID = 2398472L;
{
put("id", id);
}
};
response = Response.ok(GsonUtil.getDefault().toJson(ok, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
}
try (StatusManager statusMan = new StatusManager();
ThumbnailManager thumbMan = new ThumbnailManager()) {
Status status = new Status();
status.setStatusName(Status.StatusName.ITEM_UPDATE);
statusMan.save(status);
thumbMan.updateDirtyBits(id);
}
return response;
}
use of gov.usgs.cida.coastalhazards.jpa.ThumbnailManager in project coastal-hazards by USGS-CIDA.
the class ThumbnailResource method getList.
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getList(@QueryParam("dirty") @DefaultValue("false") boolean dirty) {
Response response = null;
try (ThumbnailManager manager = new ThumbnailManager()) {
List<Thumbnail> thumbnails = manager.loadAll(dirty);
Gson gson = GsonUtil.getDefault();
response = Response.ok(gson.toJson(thumbnails, List.class), MediaType.APPLICATION_JSON_TYPE).build();
}
return response;
}
use of gov.usgs.cida.coastalhazards.jpa.ThumbnailManager in project coastal-hazards by USGS-CIDA.
the class ItemResource method updateItem.
/**
* @param request
* @param id
* @param content
* @return
*/
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateItem(@Context HttpServletRequest request, @PathParam("id") String id, String content) {
Response response = null;
try (ItemManager itemManager = new ItemManager()) {
Item dbItem = itemManager.load(id);
Item updatedItem = Item.fromJSON(content);
String trackId = null;
// If this is a storm going from active to inactive then remove the track item
if (dbItem.getType() == Item.Type.storms && !updatedItem.isActiveStorm() && dbItem.isActiveStorm()) {
Integer trackIndex = null;
// Find Track Child
for (Item child : updatedItem.getChildren()) {
if (child.getName().equals("track")) {
trackId = child.getId();
trackIndex = updatedItem.getChildren().indexOf(child);
break;
}
}
// Remove Track Child
if (trackId != null && trackIndex != null) {
updatedItem.getChildren().remove(trackIndex.intValue());
}
}
Item mergedItem = Item.copyValues(updatedItem, dbItem);
String mergedId = null;
if (dbItem == null) {
mergedId = itemManager.persist(mergedItem);
} else {
mergedId = itemManager.merge(mergedItem);
}
if (null != mergedId) {
// Delete the storm track item once the storm has been successfully saved
if (trackId != null) {
itemManager.delete(trackId, true);
}
response = Response.ok().build();
} else {
throw new BadRequestException();
}
try (StatusManager statusMan = new StatusManager();
ThumbnailManager thumbMan = new ThumbnailManager()) {
Status status = new Status();
status.setStatusName(Status.StatusName.ITEM_UPDATE);
statusMan.save(status);
thumbMan.updateDirtyBits(id);
}
}
return response;
}
use of gov.usgs.cida.coastalhazards.jpa.ThumbnailManager in project coastal-hazards by USGS-CIDA.
the class ItemResource method deleteItem.
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@DELETE
@Path("/{id}")
public Response deleteItem(@Context HttpServletRequest request, @PathParam("id") String id, @QueryParam("deleteChildren") boolean deleteChildren) {
Response response = null;
try (ItemManager itemManager = new ItemManager()) {
if (itemManager.isOrphan(id)) {
if (itemManager.delete(id, deleteChildren)) {
response = Response.ok().build();
} else {
throw new Error();
}
try (StatusManager statusMan = new StatusManager();
ThumbnailManager thumbMan = new ThumbnailManager()) {
Status status = new Status();
status.setStatusName(Status.StatusName.ITEM_UPDATE);
statusMan.save(status);
thumbMan.updateDirtyBits(id);
}
} else {
response = Response.status(400).build();
}
}
return response;
}
Aggregations