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