Search in sources :

Example 1 with Summary

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

the class StormUtil method buildStormTemplateSummary.

public static Summary buildStormTemplateSummary(Layer layer) {
    Summary summary = new Summary();
    Document cswDoc = getStormCswDocument(layer);
    if (cswDoc != null) {
        String title = MetadataUtil.extractFirstStringFromCswDoc(cswDoc, "/*/metadata/idinfo/citation/citeinfo/title");
        Map<String, String> titleParts = parseTitleParts(title);
        String cswAbstract = MetadataUtil.extractFirstStringFromCswDoc(cswDoc, "/*/metadata/idinfo/descript/abstract");
        List<String> dataSrcList = MetadataUtil.extractStringsFromCswDoc(cswDoc, "/*/metadata/dataqual/lineage/srcinfo/srccite/citeinfo/title");
        String surgeDescription = buildSurgeDescription(cswDoc);
        Tiny tiny = buildTinyText(titleParts);
        Medium medium = buildMediumText(titleParts);
        Full full = buildFullText(titleParts, title, surgeDescription);
        Legend legend = buildLegendText(titleParts);
        summary.setTiny(tiny);
        summary.setMedium(medium);
        summary.setFull(full);
        summary.setLegend(legend);
    }
    return summary;
}
Also used : Legend(gov.usgs.cida.coastalhazards.model.summary.Legend) Medium(gov.usgs.cida.coastalhazards.model.summary.Medium) Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) Document(org.w3c.dom.Document) Full(gov.usgs.cida.coastalhazards.model.summary.Full)

Example 2 with Summary

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

the class MetadataResource method getMetadataSummaryByAttribtueUsingItemID.

@GET
@Path("/summarize/itemid/{itemid}/attribute/{attr}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMetadataSummaryByAttribtueUsingItemID(@PathParam("itemid") String itemId, @PathParam("attr") String attr) throws URISyntaxException {
    Response response;
    try (ItemManager itemManager = new ItemManager()) {
        Item item = itemManager.load(itemId);
        String jsonSummary = MetadataUtil.getSummaryFromWPS(getMetadataUrl(item), attr);
        Summary summary = GsonUtil.getDefault().fromJson(jsonSummary, Summary.class);
        response = Response.ok(GsonUtil.getDefault().toJson(summary, Summary.class), MediaType.APPLICATION_JSON_TYPE).build();
    } catch (IOException | ParserConfigurationException | SAXException | JsonSyntaxException ex) {
        Map<String, String> err = new HashMap<>();
        err.put("message", ex.getMessage());
        response = Response.serverError().entity(GsonUtil.getDefault().toJson(err, HashMap.class)).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) SAXException(org.xml.sax.SAXException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with Summary

use of gov.usgs.cida.coastalhazards.model.summary.Summary 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 4 with Summary

use of gov.usgs.cida.coastalhazards.model.summary.Summary 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 5 with Summary

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

the class TemplateResource method gatherTemplateSummary.

protected Summary gatherTemplateSummary(Summary previousSummary, List<Item> children) {
    Summary newSummary = Summary.copyValues(previousSummary, null);
    String keywords = previousSummary.getKeywords();
    Set<String> keywordSet = keywordsFromString(keywords);
    Set<Publication> publicationSet = new LinkedHashSet<>();
    Full full = previousSummary.getFull();
    List<Publication> publications = full.getPublications();
    publicationSet.addAll(publications);
    if (children != null) {
        for (Item item : children) {
            Set<String> childKeywords = keywordsFromString(item.getSummary().getKeywords());
            keywordSet.addAll(childKeywords);
            List<Publication> childPubs = item.getSummary().getFull().getPublications();
            for (Publication pub : childPubs) {
                publicationSet.add(Publication.copyValues(pub, null));
            }
        }
    }
    String newKeywords = StringUtils.join(keywordSet, "|");
    newSummary.setKeywords(newKeywords);
    newSummary.getFull().setPublications(Lists.newArrayList(publicationSet));
    return newSummary;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) Publication(gov.usgs.cida.coastalhazards.model.summary.Publication) Full(gov.usgs.cida.coastalhazards.model.summary.Full)

Aggregations

Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)16 Item (gov.usgs.cida.coastalhazards.model.Item)12 LinkedList (java.util.LinkedList)9 Response (javax.ws.rs.core.Response)9 Service (gov.usgs.cida.coastalhazards.model.Service)7 Tiny (gov.usgs.cida.coastalhazards.model.summary.Tiny)7 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 Gson (com.google.gson.Gson)5 Map (java.util.Map)5 IOException (java.io.IOException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 SAXException (org.xml.sax.SAXException)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 Layer (gov.usgs.cida.coastalhazards.model.Layer)3 List (java.util.List)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Viewable (org.glassfish.jersey.server.mvc.Viewable)3