Search in sources :

Example 21 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class SLDResource method getSLD.

/**
 * XML representation of the SLD document related to a specific item ;qs=1
 * is required to make this the default response when no accepts header is
 * given
 *
 * @param id item ID
 * @param ribbon which ribbon to represent (not required)
 * @return response with SLD XML representation
 */
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_XML + ";qs=1")
public Response getSLD(@PathParam("id") String id, @QueryParam("ribbon") Integer ribbon, @QueryParam("selectedItem") String selectedId) {
    PerformanceProfiler.startTimer("SLDResource.getSLD - " + id);
    Response response = null;
    try (ItemManager manager = new ItemManager()) {
        if (selectedId == null || selectedId.length() == 0) {
            selectedId = id;
        }
        PerformanceProfiler.startTimer("SLDResource.getSLD_ItemManager.load - " + id);
        Item item = manager.load(id);
        PerformanceProfiler.stopDebug("SLDResource.getSLD_ItemManager.load - " + id);
        if (item == null) {
            response = Response.status(Response.Status.NOT_FOUND).build();
        } else {
            PerformanceProfiler.startTimer("SLDResource.getSLD_getGenerator - " + id);
            SLDGenerator generator = SLDGenerator.getGenerator(item, selectedId, ribbon);
            PerformanceProfiler.stopDebug("SLDResource.getSLD_getGenerator - " + id);
            if (generator != null) {
                PerformanceProfiler.startTimer("SLDResource.getSLD_generateSLD - " + id);
                response = generator.generateSLD();
                PerformanceProfiler.stopDebug("SLDResource.getSLD_generateSLD - " + id);
            }
        }
    } catch (Exception e) {
        response = Response.status(500).build();
    }
    PerformanceProfiler.stopDebug("SLDResource.getSLD - " + id);
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) SLDGenerator(gov.usgs.cida.coastalhazards.sld.SLDGenerator) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager 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 23 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager 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)

Example 24 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager in project coastal-hazards by USGS-CIDA.

the class TreeResource method deleteItems.

private boolean deleteItems(JsonArray toDelete, boolean deleteChildren) {
    boolean returnStatus = true;
    for (JsonElement item : toDelete) {
        String itemId = item.getAsString();
        try (ItemManager itemManager = new ItemManager()) {
            if (itemManager.isOrphan(itemId)) {
                if (!itemManager.delete(itemId, deleteChildren)) {
                    log.error("Failed to delete some items marked for delete [item: " + itemId + "] [delete children: " + (deleteChildren ? "yes" : "no") + "].");
                    returnStatus = false;
                } else {
                    log.info("Successfully deleted item " + itemId + " [delete children: " + (deleteChildren ? "yes" : "no") + "].");
                }
            } else {
                log.info("Item " + itemId + " skipped because it is not an orphan.");
            }
        } catch (Exception e) {
            log.error("Failed to delete some items marked for delete [delete children: " + (deleteChildren ? "yes" : "no") + "]. Error: " + e.getMessage());
            returnStatus = false;
        }
        if (!returnStatus) {
            break;
        }
    }
    return returnStatus;
}
Also used : ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) JsonElement(com.google.gson.JsonElement) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException)

Example 25 with ItemManager

use of gov.usgs.cida.coastalhazards.jpa.ItemManager 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;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Item(gov.usgs.cida.coastalhazards.model.Item) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Alias(gov.usgs.cida.coastalhazards.model.Alias) NotFoundException(javax.ws.rs.NotFoundException) Date(java.util.Date) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)31 Item (gov.usgs.cida.coastalhazards.model.Item)26 Path (javax.ws.rs.Path)24 GET (javax.ws.rs.GET)22 Produces (javax.ws.rs.Produces)21 Response (javax.ws.rs.core.Response)21 HashMap (java.util.HashMap)11 NotFoundException (javax.ws.rs.NotFoundException)9 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)8 Status (gov.usgs.cida.coastalhazards.model.util.Status)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)7 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)7 Viewable (org.glassfish.jersey.server.mvc.Viewable)7 Gson (com.google.gson.Gson)6 JsonObject (com.google.gson.JsonObject)5 AliasManager (gov.usgs.cida.coastalhazards.jpa.AliasManager)5 Alias (gov.usgs.cida.coastalhazards.model.Alias)5 RolesAllowed (javax.annotation.security.RolesAllowed)5 ThumbnailManager (gov.usgs.cida.coastalhazards.jpa.ThumbnailManager)4 IOException (java.io.IOException)4