Search in sources :

Example 6 with Item

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

the class SLDResource method getSLDInfo.

/**
 * JSON representation of the contents of the SLD, this is primarily for
 * building a UI legend ;qs=0 is to make this a lower priority than the xml
 * document, must say accepts=application/json to get this document
 *
 * @param id item ID
 * @param ribbon Not used currently, but represents which ribbon to
 * represent
 * @return JSON document with SLD info
 */
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON + ";qs=0")
public Response getSLDInfo(@PathParam("id") String id, @QueryParam("ribbon") Integer ribbon, @QueryParam("selectedItem") String selectedId) {
    PerformanceProfiler.startTimer("SLDResource.getSLDInfo - " + id);
    Response response;
    try (ItemManager manager = new ItemManager()) {
        if (selectedId == null || selectedId.length() == 0) {
            selectedId = id;
        }
        PerformanceProfiler.startTimer("SLDResource.getSLD_ItemManager.load - " + id);
        Item item = manager.load(id);
        PerformanceProfiler.stopDebug("SLDResource.getSLD_ItemManager.load - " + id);
        if (item == null) {
            response = Response.status(Response.Status.NOT_FOUND).build();
        } else {
            PerformanceProfiler.startTimer("SLDResource.getSLDInfo_getGenerator - " + id);
            SLDGenerator generator = SLDGenerator.getGenerator(item, selectedId, ribbon);
            PerformanceProfiler.stopDebug("SLDResource.getSLDInfo_getGenerator - " + id);
            if (generator == null) {
                response = Response.status(Response.Status.NOT_FOUND).build();
            } else {
                PerformanceProfiler.startTimer("SLDResource.getSLDInfo_generateSLDInfo - " + id);
                response = generator.generateSLDInfo();
                PerformanceProfiler.stopDebug("SLDResource.getSLDInfo_generateSLDInfo - " + id);
            }
        }
    } catch (Exception e) {
        response = Response.status(500).build();
    }
    PerformanceProfiler.stopDebug("SLDResource.getSLDInfo - " + id);
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) SLDGenerator(gov.usgs.cida.coastalhazards.sld.SLDGenerator) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with Item

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

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

the class TemplateResource method baseTemplateItem.

private Item baseTemplateItem(boolean active, Bbox bbox, List<Service> serviceCopies, Summary summary) {
    Item baseTemplate = new Item();
    baseTemplate.setType(Type.storms);
    baseTemplate.setRibbonable(true);
    baseTemplate.setShowChildren(true);
    baseTemplate.setName("storm_" + (new SimpleDateFormat("yyyyMMddHHmm").format(Date.from(Instant.now()))));
    baseTemplate.setId(IdGenerator.generate());
    baseTemplate.setItemType(ItemType.template);
    baseTemplate.setActiveStorm(active);
    baseTemplate.setBbox(Bbox.copyValues(bbox, new Bbox()));
    baseTemplate.setServices(serviceCopies);
    baseTemplate.setSummary(summary);
    return baseTemplate;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with Item

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

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

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