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;
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations