Search in sources :

Example 11 with Item

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

the class TemplateResource method instantiateTemplate.

@POST
@Path("/item/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response instantiateTemplate(@Context HttpServletRequest request, @PathParam("id") String id, String content) {
    Response response = null;
    try (ItemManager itemMan = new ItemManager();
        LayerManager layerMan = new LayerManager()) {
        Item template = itemMan.load(id);
        if (template.getItemType() != Item.ItemType.template) {
            throw new UnsupportedOperationException("Only template items may be instantiated");
        }
        List<Item> childItems = template.getChildren();
        List<Item> newItemList = null;
        List<Item> newAndOldList = null;
        List<Item> retainedItems = new LinkedList<>();
        List<String> displayed = new LinkedList<>();
        JsonParser parser = new JsonParser();
        JsonObject parsed = parser.parse(content).getAsJsonObject();
        // TODO only supporting one level for now, bring in aggs later
        boolean allAttributes = parseAllAttribute(parsed);
        boolean retainAggregations = retainAggregations(parsed);
        if (allAttributes) {
            JsonElement layer = parsed.get("layerId");
            if (layer != null) {
                String layerId = layer.getAsString();
                try {
                    newItemList = makeItemsFromLayer(template, layerId, layerMan);
                    retainedItems = findItemsToRetain(template, retainAggregations);
                    newAndOldList = new LinkedList<>(retainedItems);
                    newAndOldList.addAll(newItemList);
                } catch (IOException ex) {
                    log.error("Cannot create items", ex);
                }
                for (Item retained : retainedItems) {
                    displayed.add(retained.getId());
                }
                List<String> displayedIdByAttr = makeDisplayedChildren(newItemList);
                displayed.addAll(displayedIdByAttr);
            }
        } else {
            Map<String, Item> childMap = makeChildItemMap(childItems);
            JsonArray children = parsed.get("children").getAsJsonArray();
            newItemList = makeItemsFromDocument(template, children, childMap, itemMan, layerMan);
            List<String> visibleItems = visibleItems(children, newItemList, childMap);
            displayed.addAll(visibleItems);
            newAndOldList = newItemList;
        }
        itemMan.persistAll(newItemList);
        template.setChildren(newAndOldList);
        template.setDisplayedChildren(displayed);
        template.setSummary(gatherTemplateSummary(template.getSummary(), newItemList));
        String mergeId = itemMan.merge(template);
        if (mergeId != null) {
            response = Response.ok().build();
            try (StatusManager statusMan = new StatusManager()) {
                Status status = new Status();
                status.setStatusName(Status.StatusName.ITEM_UPDATE);
                statusMan.save(status);
            }
        } else {
            response = Response.serverError().build();
        }
    }
    return response;
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(gov.usgs.cida.coastalhazards.model.util.Status) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) LayerManager(gov.usgs.cida.coastalhazards.jpa.LayerManager) JsonParser(com.google.gson.JsonParser) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 12 with Item

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

the class TemplateResource method makeDisplayedChildren.

// TODO make this more configurable, right now just using PCOI
private List<String> makeDisplayedChildren(List<Item> children) {
    List<String> displayed = new LinkedList<>();
    for (Item child : children) {
        String attr = child.getAttr();
        boolean isDisplayed = Attributes.getPCOIAttrs().contains(attr);
        if (isDisplayed) {
            displayed.add(child.getId());
        }
    }
    return displayed;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) LinkedList(java.util.LinkedList)

Example 13 with Item

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

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

the class DataDomainUtility method retrieveDomainFromWFS.

public static SortedSet<String> retrieveDomainFromWFS(Item item) {
    SortedSet<String> domain = new TreeSet<>();
    WFSGetDomain client = new WFSGetDomain();
    if (item == null) {
        throw new IllegalArgumentException("Item must be valid data item");
    } else if (item.getItemType() == Item.ItemType.aggregation || item.getItemType() == Item.ItemType.template) {
        List<String> displayedIds = item.getDisplayedChildren();
        for (Item child : item.getChildren()) {
            if (displayedIds.contains(child.getId())) {
                // recurse (again, avoiding cycles is important)
                Set<String> childDomain = retrieveDomainFromWFS(child);
                domain.addAll(childDomain);
            }
        }
    } else if (item.getItemType() == Item.ItemType.data) {
        WFSService service = item.fetchWfsService();
        try {
            Set<String> domainValuesAsStrings = client.getDomainValuesAsStrings(service, item.getAttr());
            domain.addAll(domainValuesAsStrings);
        } catch (IOException e) {
            log.error("unable to get domain from wfs", e);
        }
    }
    return domain;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) TreeSet(java.util.TreeSet) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) List(java.util.List) IOException(java.io.IOException)

Example 15 with Item

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

the class DownloadUtility method populateDownloadMap.

private static void populateDownloadMap(Map<WFSService, SingleDownload> downloadMap, Item item) {
    SingleDownload download;
    Queue<Item> itemQueue = new LinkedList<>();
    itemQueue.add(item);
    // (i.e. an item that sees itself in the subtree throws an exception)
    while (itemQueue.peek() != null) {
        Item currentItem = itemQueue.poll();
        WFSService wfs = getWfsService(currentItem);
        if (wfs != null && wfs.checkValidity()) {
            if (downloadMap.containsKey(wfs)) {
                download = downloadMap.get(wfs);
            } else {
                download = new SingleDownload();
                download.setWfs(wfs);
                download.setName(currentItem.getName());
                try {
                    URL cswUrl = currentItem.fetchCswService().fetchUrl();
                    download.setMetadata(cswUrl);
                } catch (MalformedURLException ex) {
                    LOG.info("Invalid csw url {}", currentItem.fetchCswService());
                } catch (NullPointerException ex) {
                    LOG.info("CSW service not set");
                }
                downloadMap.put(wfs, download);
            }
            String attr = currentItem.getAttr();
            if (StringUtils.isNotBlank(attr)) {
                download.addAttr(attr);
            }
        }
        List<Item> children = currentItem.getChildren();
        if (children != null) {
            itemQueue.addAll(children);
        }
    }
}
Also used : SessionItem(gov.usgs.cida.coastalhazards.model.SessionItem) Item(gov.usgs.cida.coastalhazards.model.Item) MalformedURLException(java.net.MalformedURLException) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) LinkedList(java.util.LinkedList) URL(java.net.URL)

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