Search in sources :

Example 51 with Item

use of gov.usgs.cida.coastalhazards.model.Item 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 52 with Item

use of gov.usgs.cida.coastalhazards.model.Item 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 53 with Item

use of gov.usgs.cida.coastalhazards.model.Item 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 54 with Item

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

the class DataDomainResource method getDataDomain.

@GET
@Path("/item/{id}")
public Response getDataDomain(@PathParam("id") String id, @Context Request request) {
    Response response = null;
    try (ItemManager itemManager = new ItemManager();
        DataDomainManager domainManager = new DataDomainManager()) {
        Item item = itemManager.load(id);
        if (item == null || item.getType() != Item.Type.historical) {
            throw new NotFoundException("Only historical is supported at this time");
        }
        DataDomain domain = domainManager.getDomainForItem(item);
        Response checkModified = HTTPCachingUtil.checkModified(request, domain);
        if (checkModified != null) {
            response = checkModified;
        } else {
            Gson serializer = GsonUtil.getDefault();
            String domainJson = serializer.toJson(domain);
            response = Response.ok(domainJson, MediaType.APPLICATION_JSON_TYPE).lastModified(domain.getLastModified()).build();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) DataDomainManager(gov.usgs.cida.coastalhazards.jpa.DataDomainManager) NotFoundException(javax.ws.rs.NotFoundException) Gson(com.google.gson.Gson) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 55 with Item

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

the class DownloadResource method downloadItem.

/**
 * Downloads a zip file containing the contents of
 * gov.usgs.cida.coastalhazards.model.Item
 *
 * @param id identifier of requested item
 * @return JSON representation of the item(s)
 * @throws java.io.IOException
 * @throws java.lang.InterruptedException
 * @throws java.util.concurrent.ExecutionException
 */
@GET
@Path("/item/{id}")
@Produces("application/zip")
public Response downloadItem(@PathParam("id") String id) throws IOException, InterruptedException, ExecutionException {
    Response response;
    Item item;
    try (ItemManager itemManager = new ItemManager()) {
        item = itemManager.load(id);
    }
    if (item == null) {
        response = Response.status(NOT_FOUND).build();
    } else {
        Download download;
        try (DownloadManager downloadManager = new DownloadManager()) {
            download = downloadManager.load(id);
        }
        if (download == null) {
            // Download was null, so we it was not previously staged. Do so now.
            LOG.debug("Download manager could not find download for item {}. A download will be staged for this item.", id);
            DownloadUtility.stageAsyncItemDownload(id);
            response = Response.status(ACCEPTED).build();
        } else {
            LOG.debug("Download manager found a download for item id {}", id);
            if (download.isProblem()) {
                LOG.debug("Download manager found a problem with download for item id {}", id);
                response = Response.status(NOT_IMPLEMENTED).build();
            } else if (StringUtils.isBlank(download.getPersistanceURI())) {
                LOG.debug("Download manager found a download with no path id {}, Item is still being created", id);
                response = Response.status(ACCEPTED).build();
            } else {
                boolean downloadFileExists;
                try (DownloadManager downloadManager = new DownloadManager()) {
                    downloadFileExists = downloadManager.downloadFileExistsOnFilesystem(download);
                }
                // Download should be on the file system. Check that it does exist
                if (!downloadFileExists) {
                    LOG.debug("Download manager found a download path that doesn't exist for id {}. Will delete and re-stage", id);
                    try (DownloadService svc = new DownloadService()) {
                        svc.delete(id);
                    }
                    DownloadUtility.stageAsyncItemDownload(id);
                    response = Response.status(ACCEPTED).build();
                } else {
                    // File was found. Load the zip file
                    File zipFile = download.fetchZipFile();
                    if (zipFile == null) {
                        LOG.debug("Download manager found could not find the zip file that was indicated in the database for item {}. Will attempt to re-stage", id);
                        try (DownloadService svc = new DownloadService()) {
                            svc.delete(id);
                        }
                        DownloadUtility.stageAsyncItemDownload(id);
                        response = Response.status(ACCEPTED).build();
                    } else {
                        String contentDisposition = "attachment; filename=\"" + id + ".zip\"";
                        response = Response.status(OK).entity(zipFile).type("application/zip").header("Content-Disposition", contentDisposition).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) Download(gov.usgs.cida.coastalhazards.model.util.Download) DownloadManager(gov.usgs.cida.coastalhazards.jpa.DownloadManager) File(java.io.File) DownloadService(gov.usgs.cida.coastalhazards.service.data.DownloadService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Item (gov.usgs.cida.coastalhazards.model.Item)60 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)26 Response (javax.ws.rs.core.Response)24 Path (javax.ws.rs.Path)22 LinkedList (java.util.LinkedList)21 GET (javax.ws.rs.GET)20 Produces (javax.ws.rs.Produces)20 HashMap (java.util.HashMap)16 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)12 Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)12 JsonObject (com.google.gson.JsonObject)10 ArrayList (java.util.ArrayList)10 Viewable (org.glassfish.jersey.server.mvc.Viewable)10 Gson (com.google.gson.Gson)9 Service (gov.usgs.cida.coastalhazards.model.Service)8 NotFoundException (javax.ws.rs.NotFoundException)8 Test (org.junit.Test)8 JsonElement (com.google.gson.JsonElement)7 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)7 Status (gov.usgs.cida.coastalhazards.model.util.Status)7