use of gov.usgs.cida.coastalhazards.jpa.StatusManager in project coastal-hazards by USGS-CIDA.
the class ItemResource method getItem.
/**
* Retrieves representation of an instance of
* gov.usgs.cida.coastalhazards.model.Item
*
* @param id identifier of requested item
* @param subtree whether to return all items below this as a subtree
* @param request request object
* @return JSON representation of the item(s)
*/
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getItem(@PathParam("id") String id, @DefaultValue("false") @QueryParam("subtree") boolean subtree, @Context Request request) {
Response response = null;
Item item = null;
try (StatusManager statusMan = new StatusManager()) {
try (ItemManager itemManager = new ItemManager()) {
item = itemManager.load(id);
}
if (item == null) {
throw new NotFoundException();
} else {
// Check when the item and/or structure was last modified, if at all.
// - If both are null, use today's date.
// - If one of the two is not null, use that.
// - Else, if both are not null, use the latest between them.
Status lastItemUpdate = statusMan.load(Status.StatusName.ITEM_UPDATE);
Status lastStructureUpdate = statusMan.load(Status.StatusName.STRUCTURE_UPDATE);
Date modified = new Date();
if (lastItemUpdate != null && lastStructureUpdate != null) {
// Both updates exist, so compare between them and choose the latest
Date lastItemUpdateDate = lastItemUpdate.getLastUpdate();
Date lastStructureUpdateDate = lastStructureUpdate.getLastUpdate();
modified = lastItemUpdateDate.after(lastStructureUpdateDate) ? lastItemUpdateDate : lastStructureUpdateDate;
} else {
// least one exists and use that.
if (lastItemUpdate != null) {
modified = lastItemUpdate.getLastUpdate();
}
if (lastStructureUpdate != null) {
modified = lastStructureUpdate.getLastUpdate();
}
}
Response unmodified = HTTPCachingUtil.checkModified(request, modified);
if (unmodified != null) {
response = unmodified;
} else {
String jsonResult = item.toJSON(subtree);
response = Response.ok(jsonResult, MediaType.APPLICATION_JSON_TYPE).lastModified(modified).build();
}
}
}
return response;
}
use of gov.usgs.cida.coastalhazards.jpa.StatusManager 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.StatusManager 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;
}
use of gov.usgs.cida.coastalhazards.jpa.StatusManager in project coastal-hazards by USGS-CIDA.
the class AliasResource method getItemForAlias.
@GET
@Path("/{id}/item")
@Produces(MediaType.APPLICATION_JSON)
public Response getItemForAlias(@PathParam("id") String id, @DefaultValue("false") @QueryParam("subtree") boolean subtree, @Context Request request) {
Response response = null;
try (AliasManager aliasManager = new AliasManager()) {
Alias alias = aliasManager.load(id);
Item item = null;
try (StatusManager statusMan = new StatusManager()) {
try (ItemManager itemManager = new ItemManager()) {
item = itemManager.load(alias.getItemId());
}
if (item == null) {
throw new NotFoundException();
} else {
// Check when the item and/or structure was last modified, if at all.
// - If both are null, use today's date.
// - If one of the two is not null, use that.
// - Else, if both are not null, use the latest between them.
Status lastItemUpdate = statusMan.load(Status.StatusName.ITEM_UPDATE);
Status lastStructureUpdate = statusMan.load(Status.StatusName.STRUCTURE_UPDATE);
Date modified = new Date();
if (lastItemUpdate != null && lastStructureUpdate != null) {
// Both updates exist, so compare between them and choose the latest
Date lastItemUpdateDate = lastItemUpdate.getLastUpdate();
Date lastStructureUpdateDate = lastStructureUpdate.getLastUpdate();
modified = lastItemUpdateDate.after(lastStructureUpdateDate) ? lastItemUpdateDate : lastStructureUpdateDate;
} else {
// least one exists and use that.
if (lastItemUpdate != null) {
modified = lastItemUpdate.getLastUpdate();
}
if (lastStructureUpdate != null) {
modified = lastStructureUpdate.getLastUpdate();
}
}
Response unmodified = HTTPCachingUtil.checkModified(request, modified);
if (unmodified != null) {
response = unmodified;
} else {
String jsonResult = item.toJSON(subtree);
response = Response.ok(jsonResult, MediaType.APPLICATION_JSON_TYPE).lastModified(modified).build();
}
}
}
}
return response;
}
Aggregations