Search in sources :

Example 6 with Alias

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

the class InfoRouter method useAliasInfoJsp.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/alias/{aliasId}")
public Response useAliasInfoJsp(@PathParam("aliasId") String aliasId) {
    Map<String, Object> map = new HashMap<>();
    try (ItemManager mgr = new ItemManager();
        AliasManager amgr = new AliasManager()) {
        Alias alias = amgr.load(aliasId);
        if (alias != null) {
            Item item = mgr.load(alias.getItemId());
            if (item == null) {
                return Response.status(Response.Status.NOT_FOUND).build();
            }
            map.put("item", item);
            map.put("alias", alias);
            return Response.ok(new Viewable("/WEB-INF/jsp/ui/back/index.jsp", map)).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
}
Also used : AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Item(gov.usgs.cida.coastalhazards.model.Item) HashMap(java.util.HashMap) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Alias(gov.usgs.cida.coastalhazards.model.Alias) Viewable(org.glassfish.jersey.server.mvc.Viewable) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with Alias

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

the class AliasManager method loadAll.

public List<Alias> loadAll() {
    Query selectAllQuery = em.createQuery(HQL_SELECT_ALL);
    List<Alias> resultList = selectAllQuery.getResultList();
    return (resultList);
}
Also used : Query(javax.persistence.Query) Alias(gov.usgs.cida.coastalhazards.model.Alias)

Example 8 with Alias

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

the class AliasResource method getAliasList.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAliasList(@PathParam("id") String id) {
    Response response = null;
    try (AliasManager manager = new AliasManager()) {
        List<Alias> aliases = manager.loadAll();
        Gson gson = GsonUtil.getDefault();
        if (aliases != null) {
            response = Response.ok(gson.toJson(aliases), MediaType.APPLICATION_JSON_TYPE).build();
        } else {
            throw new Error();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Alias(gov.usgs.cida.coastalhazards.model.Alias) Gson(com.google.gson.Gson) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with Alias

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

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

the class AliasResource method updateAlias.

@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateAlias(@PathParam("id") String id, String content, @Context HttpServletRequest request) {
    Response response = null;
    Alias newAlias = Alias.fromJSON(content);
    try (AliasManager manager = new AliasManager()) {
        Alias alias = manager.load(id);
        if (alias != null) {
            alias.setItemId(newAlias.getItemId());
            manager.update(alias);
            response = Response.ok(GsonUtil.getDefault().toJson(alias, Alias.class), MediaType.APPLICATION_JSON_TYPE).build();
        } else {
            throw new NotFoundException();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Alias(gov.usgs.cida.coastalhazards.model.Alias) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Aggregations

Alias (gov.usgs.cida.coastalhazards.model.Alias)13 AliasManager (gov.usgs.cida.coastalhazards.jpa.AliasManager)10 Produces (javax.ws.rs.Produces)10 GET (javax.ws.rs.GET)8 Path (javax.ws.rs.Path)8 Response (javax.ws.rs.core.Response)7 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)5 Item (gov.usgs.cida.coastalhazards.model.Item)5 Gson (com.google.gson.Gson)4 HashMap (java.util.HashMap)4 NotFoundException (javax.ws.rs.NotFoundException)4 RolesAllowed (javax.annotation.security.RolesAllowed)3 Query (javax.persistence.Query)3 Viewable (org.glassfish.jersey.server.mvc.Viewable)3 Consumes (javax.ws.rs.Consumes)2 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)1 LayerManager (gov.usgs.cida.coastalhazards.jpa.LayerManager)1 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)1