Search in sources :

Example 16 with ItemManager

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

the class ItemTest method testDB.

@Test
// need to figure out how to do local db test
@Ignore
public void testDB() throws SessionIOException, CycleIntroductionException {
    ItemManager manager = new ItemManager();
    String id;
    Item fromJSON = Item.fromJSON(itemJSON);
    id = manager.persist(fromJSON);
    assertNotNull(id);
    Item item = manager.load(id);
    assertNotNull(item);
}
Also used : ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with ItemManager

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

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

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

Example 20 with ItemManager

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

the class ItemResource method searchItems.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response searchItems(@DefaultValue("") @QueryParam("query") List<String> query, @DefaultValue("") @QueryParam("type") List<String> type, @DefaultValue("popularity") @QueryParam("sortBy") String sortBy, @DefaultValue("-1") @QueryParam("count") int count, @DefaultValue("") @QueryParam("bbox") String bbox, @DefaultValue("false") @QueryParam("subtree") boolean subtree, @DefaultValue("false") @QueryParam("showDisabled") boolean showDisabled) {
    // need to figure out how to search popularity and bbox yet
    Response response = null;
    try (ItemManager itemManager = new ItemManager()) {
        String jsonResult = itemManager.query(query, type, sortBy, count, bbox, subtree, showDisabled);
        response = Response.ok(jsonResult, MediaType.APPLICATION_JSON_TYPE).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) 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