Search in sources :

Example 11 with Status

use of gov.usgs.cida.coastalhazards.model.util.Status 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)

Example 12 with Status

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

the class StatusManager method loadAll.

public Map<StatusName, Status> loadAll() {
    Map<StatusName, Status> result = new TreeMap<>();
    Query selectQuery = em.createQuery(HQL_SELECT_ALL);
    List<Status> resultList = selectQuery.getResultList();
    for (Status status : resultList) {
        result.put(status.getStatusName(), status);
    }
    return result;
}
Also used : Status(gov.usgs.cida.coastalhazards.model.util.Status) Query(javax.persistence.Query) StatusName(gov.usgs.cida.coastalhazards.model.util.Status.StatusName) TreeMap(java.util.TreeMap)

Aggregations

Status (gov.usgs.cida.coastalhazards.model.util.Status)12 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)9 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)8 Response (javax.ws.rs.core.Response)8 Item (gov.usgs.cida.coastalhazards.model.Item)7 Path (javax.ws.rs.Path)6 RolesAllowed (javax.annotation.security.RolesAllowed)5 ThumbnailManager (gov.usgs.cida.coastalhazards.jpa.ThumbnailManager)4 Produces (javax.ws.rs.Produces)4 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)3 Date (java.util.Date)3 Consumes (javax.ws.rs.Consumes)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 StatusName (gov.usgs.cida.coastalhazards.model.util.Status.StatusName)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2