Search in sources :

Example 6 with Status

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

the class StatusManager method save.

public boolean save(Status status) {
    EntityTransaction transaction = em.getTransaction();
    boolean saved = false;
    try {
        Status fromDb = load(status.getStatusName());
        transaction.begin();
        if (fromDb != null) {
            // if there are more properties, copy those as well
            fromDb.setLastUpdate(status.getLastUpdate());
            em.merge(fromDb);
        } else {
            em.persist(status);
        }
        transaction.commit();
        saved = true;
    } catch (Exception ex) {
        log.error("unable to update status", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        saved = false;
    }
    return saved;
}
Also used : Status(gov.usgs.cida.coastalhazards.model.util.Status) EntityTransaction(javax.persistence.EntityTransaction)

Example 7 with Status

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

the class StatusManager method load.

public Status load(StatusName name) {
    Status status = null;
    Query selectQuery = em.createQuery(HQL_SELECT_BY_ID);
    selectQuery.setParameter("name", name);
    List<Status> resultList = selectQuery.getResultList();
    if (!resultList.isEmpty()) {
        status = resultList.get(0);
    }
    return status;
}
Also used : Status(gov.usgs.cida.coastalhazards.model.util.Status) Query(javax.persistence.Query)

Example 8 with Status

use of gov.usgs.cida.coastalhazards.model.util.Status 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;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) Item(gov.usgs.cida.coastalhazards.model.Item) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) 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 9 with Status

use of gov.usgs.cida.coastalhazards.model.util.Status 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;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) Item(gov.usgs.cida.coastalhazards.model.Item) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ThumbnailManager(gov.usgs.cida.coastalhazards.jpa.ThumbnailManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 10 with Status

use of gov.usgs.cida.coastalhazards.model.util.Status 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;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ThumbnailManager(gov.usgs.cida.coastalhazards.jpa.ThumbnailManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) DELETE(javax.ws.rs.DELETE)

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