Search in sources :

Example 1 with Layer

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

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

the class TemplateResource method makeItemsFromDocument.

private List<Item> makeItemsFromDocument(Item template, JsonArray children, Map<String, Item> childMap, ItemManager itemMan, LayerManager layerMan) {
    Iterator<JsonElement> iterator = children.iterator();
    while (iterator.hasNext()) {
        String attr = "";
        Layer layer;
        JsonObject child = iterator.next().getAsJsonObject();
        JsonElement childId = child.get("id");
        JsonElement attrElement = child.get("attr");
        JsonElement layerId = child.get("layerId");
        String replaceId = null;
        // Generate item JSON from metadata
        if (layerId != null) {
            layer = layerMan.load(layerId.getAsString());
            if (childId != null) {
                // Replace the existing item in this place
                replaceId = childId.getAsString();
                if (childMap.containsKey(replaceId)) {
                    Item item = itemMan.load(replaceId);
                    attr = item.getAttr();
                } else {
                    throw new BadRequestException("Specified invalid child to replace");
                }
            } else if (attrElement != null) {
                attr = attrElement.getAsString();
            } else {
                throw new BadRequestException("Must specify child or attribute to replace/use");
            }
            Summary summary = makeSummary(layer, attr);
            Item newItem = templateItem(template, attr, layer, summary);
            if (replaceId == null) {
                replaceId = newItem.getId();
            }
            childMap.put(replaceId, newItem);
        } else if (childId != null) {
            String retainedChildId = childId.getAsString();
            Item retainedChild = itemMan.load(retainedChildId);
            if (retainedChild != null) {
                childMap.put(retainedChildId, retainedChild);
            }
        } else {
            throw new BadRequestException("Must specify childId if not including layerId");
        }
    }
    return new LinkedList<>(childMap.values());
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) BadRequestException(javax.ws.rs.BadRequestException) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) Layer(gov.usgs.cida.coastalhazards.model.Layer) LinkedList(java.util.LinkedList)

Example 3 with Layer

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

the class TemplateResource method makeItemsFromLayer.

private List<Item> makeItemsFromLayer(Item template, String layerId, LayerManager layerMan) throws IOException {
    List<Item> items = new LinkedList<>();
    Layer layer = layerMan.load(layerId);
    WFSService wfs = (WFSService) Service.ogcHelper(Service.ServiceType.proxy_wfs, layer.getServices());
    List<String> attrs = WFSIntrospector.getAttrs(wfs);
    for (String attr : attrs) {
        if (Attributes.contains(attr)) {
            Summary summary = makeSummary(layer, attr);
            Item item = templateItem(template, attr, layer, summary);
            items.add(item);
        }
    }
    return items;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) Layer(gov.usgs.cida.coastalhazards.model.Layer) LinkedList(java.util.LinkedList)

Example 4 with Layer

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

the class LayerManager method load.

public Layer load(String id) {
    Layer layer = null;
    layer = em.find(Layer.class, id);
    return layer;
}
Also used : Layer(gov.usgs.cida.coastalhazards.model.Layer)

Example 5 with Layer

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

the class LayerResource method createRasterLayer.

@POST
@Path("/raster")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response createRasterLayer(@Context HttpServletRequest req, @FormDataParam("metadata") String metadata, @FormDataParam("file") InputStream zipFileStream, @FormDataParam("file") FormDataContentDisposition fileDisposition) {
    List<Service> services = new ArrayList<>();
    try {
        log.info("Raster layer upload - about to parseRequest");
        String newId = IdGenerator.generate();
        String metadataId;
        if (metadata == null || metadata.isEmpty()) {
            throw new ServerErrorException("Metadata file is missing or empty.", Status.INTERNAL_SERVER_ERROR);
        }
        try {
            log.info("Raster layer create - about to doCSWInsertFromString");
            metadataId = MetadataUtil.doCSWInsertFromString(metadata);
        } catch (IOException | ParserConfigurationException | SAXException ex) {
            throw new ServerErrorException("Error inserting metadata to the CSW server.", Status.INTERNAL_SERVER_ERROR, ex);
        }
        log.info("Raster layer create - about to makeCSWServiceForUrl with metadataId: " + metadataId);
        services.add(MetadataUtil.makeCSWServiceForUrl(MetadataUtil.getMetadataByIdUrl(metadataId)));
        Bbox bbox = MetadataUtil.getBoundingBoxFromFgdcMetadata(metadata);
        log.info("Starting CRS Identifier Lookup");
        long startTime = System.nanoTime();
        String EPSGcode = CRS.lookupIdentifier(MetadataUtil.getCrsFromFgdcMetadata(metadata), true);
        long endTime = System.nanoTime();
        // divide by 1000000 to get milliseconds
        long duration = (endTime - startTime) / 1000000;
        log.info("Finished CRS Identifier Lookup. Took " + duration + "ms.");
        if (bbox == null || EPSGcode == null) {
            throw new ServerErrorException("Unable to identify bbox or epsg code from metadata.", Status.INTERNAL_SERVER_ERROR);
        }
        log.info("Raster layer create - about to addRasterLayer to geoserver with an id of: " + newId);
        log.info("Raster layer create - about to addRasterLayer to geoserver with a  bbox: " + bbox.getBbox());
        log.info("Raster layer create - about to addRasterLayer to geoserver with an EPSG of: " + EPSGcode);
        Service rasterService = GeoserverUtil.addRasterLayer(geoserverEndpoint, zipFileStream, newId, bbox, EPSGcode);
        if (null == rasterService) {
            throw new ServerErrorException("Unable to create a store and/or layer in GeoServer.", Status.INTERNAL_SERVER_ERROR);
        } else {
            services.add(rasterService);
        }
        if (!services.isEmpty()) {
            Layer layer = new Layer();
            layer.setId(newId);
            layer.setServices(services);
            layer.setBbox(bbox);
            try (LayerManager manager = new LayerManager()) {
                manager.save(layer);
            }
            return Response.created(layerURI(layer)).build();
        } else {
            throw new ServerErrorException("Unable to create layer", Status.INTERNAL_SERVER_ERROR);
        }
    } catch (JAXBException | FactoryException | IOException ex) {
        throw new ServerErrorException("Error parsing upload request", Status.INTERNAL_SERVER_ERROR, ex);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) Service(gov.usgs.cida.coastalhazards.model.Service) IOException(java.io.IOException) Layer(gov.usgs.cida.coastalhazards.model.Layer) SAXException(org.xml.sax.SAXException) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) ServerErrorException(javax.ws.rs.ServerErrorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

Layer (gov.usgs.cida.coastalhazards.model.Layer)7 LayerManager (gov.usgs.cida.coastalhazards.jpa.LayerManager)4 WFSService (gov.usgs.cida.coastalhazards.util.ogc.WFSService)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 Item (gov.usgs.cida.coastalhazards.model.Item)3 Service (gov.usgs.cida.coastalhazards.model.Service)3 Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)3 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 RolesAllowed (javax.annotation.security.RolesAllowed)3 Response (javax.ws.rs.core.Response)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXException (org.xml.sax.SAXException)3 JsonObject (com.google.gson.JsonObject)2 Bbox (gov.usgs.cida.coastalhazards.model.Bbox)2 BadRequestException (javax.ws.rs.BadRequestException)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2