Search in sources :

Example 1 with AliasManager

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

the class TemplateResource method instantiateStormTemplate.

@GET
@Path("/storm")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response instantiateStormTemplate(@Context HttpServletRequest request, @QueryParam("layerId") String layerId, @QueryParam("activeStorm") String active, @QueryParam("alias") String alias, @QueryParam("copyType") String copyType, @QueryParam("copyVal") String copyVal, @QueryParam("trackId") String trackId) {
    Response response;
    if (layerId != null && active != null) {
        Gson gson = GsonUtil.getDefault();
        String childJson = null;
        childJson = gson.toJson(StormUtil.createStormChildMap(layerId, Boolean.parseBoolean(active), trackId));
        if (childJson != null && childJson.length() > 0) {
            try (ItemManager itemMan = new ItemManager();
                LayerManager layerMan = new LayerManager();
                AliasManager aliasMan = new AliasManager()) {
                Layer layer = layerMan.load(layerId);
                if (layer != null) {
                    Summary summary = null;
                    if (copyType.equalsIgnoreCase("item") || copyType.equalsIgnoreCase("alias")) {
                        summary = copyExistingSummary(copyType, copyVal, itemMan, aliasMan);
                    } else {
                        summary = StormUtil.buildStormTemplateSummary(layer);
                    }
                    if (summary != null) {
                        List<Service> services = layer.getServices();
                        List<Service> serviceCopies = new LinkedList<>();
                        for (Service service : services) {
                            serviceCopies.add(Service.copyValues(service, new Service()));
                        }
                        Item baseTemplate = baseTemplateItem(Boolean.parseBoolean(active), layer.getBbox(), serviceCopies, summary);
                        String templateId = itemMan.persist(baseTemplate);
                        if (templateId != null && templateId.length() > 0) {
                            response = instantiateTemplate(request, templateId, childJson);
                            if (response.getStatus() == HttpStatus.SC_OK) {
                                Map<String, Object> ok = new HashMap<String, Object>() {

                                    private static final long serialVersionUID = 2398472L;

                                    {
                                        put("id", templateId);
                                    }
                                };
                                if (alias != null && alias.length() > 0) {
                                    Alias fullAlias = aliasMan.load(alias);
                                    if (fullAlias != null) {
                                        fullAlias.setItemId(templateId);
                                        aliasMan.update(fullAlias);
                                    } else {
                                        fullAlias = new Alias();
                                        fullAlias.setId(alias);
                                        fullAlias.setItemId(templateId);
                                        aliasMan.save(fullAlias);
                                    }
                                }
                                response = Response.ok(GsonUtil.getDefault().toJson(ok, HashMap.class), MediaType.APPLICATION_JSON_TYPE).build();
                            }
                        } else {
                            response = Response.status(500).build();
                        }
                    } else {
                        response = Response.status(400).build();
                    }
                } else {
                    response = Response.status(400).build();
                }
            } catch (Exception e) {
                log.error(e.toString());
                response = Response.status(500).build();
            }
        } else {
            response = Response.status(400).build();
        }
    } else {
        response = Response.status(400).build();
    }
    return response;
}
Also used : ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Gson(com.google.gson.Gson) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) OGCService(gov.usgs.cida.coastalhazards.util.ogc.OGCService) Service(gov.usgs.cida.coastalhazards.model.Service) Layer(gov.usgs.cida.coastalhazards.model.Layer) LinkedList(java.util.LinkedList) URISyntaxException(java.net.URISyntaxException) BadRequestException(javax.ws.rs.BadRequestException) SAXException(org.xml.sax.SAXException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Response(javax.ws.rs.core.Response) AliasManager(gov.usgs.cida.coastalhazards.jpa.AliasManager) Item(gov.usgs.cida.coastalhazards.model.Item) Alias(gov.usgs.cida.coastalhazards.model.Alias) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) JsonObject(com.google.gson.JsonObject) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with AliasManager

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

the class AliasResource method getAliasById.

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getAliasById(@PathParam("id") String id) {
    Response response = null;
    try (AliasManager manager = new AliasManager()) {
        Alias alias = manager.load(id);
        Gson gson = GsonUtil.getDefault();
        if (alias != null) {
            response = Response.ok(gson.toJson(alias), 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) Gson(com.google.gson.Gson) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with AliasManager

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

the class InfoRouter method useAliasInfoPrintViewJsp.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/alias/print/{aliasId}")
public Response useAliasInfoPrintViewJsp(@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-print.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 4 with AliasManager

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

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

Aggregations

AliasManager (gov.usgs.cida.coastalhazards.jpa.AliasManager)10 Alias (gov.usgs.cida.coastalhazards.model.Alias)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 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 Layer (gov.usgs.cida.coastalhazards.model.Layer)1