use of gov.usgs.cida.coastalhazards.model.Layer 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.Layer 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.Layer 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.Layer in project coastal-hazards by USGS-CIDA.
the class LayerManager method load.
public Layer load(String id) {
Layer layer = null;
layer = em.find(Layer.class, id);
return layer;
}
use of gov.usgs.cida.coastalhazards.model.Layer in project coastal-hazards by USGS-CIDA.
the class LayerResource method createRasterLayer.
@POST
@Path("/raster")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
public Response createRasterLayer(@Context HttpServletRequest req, @FormDataParam("metadata") String metadata, @FormDataParam("file") InputStream zipFileStream, @FormDataParam("file") FormDataContentDisposition fileDisposition) {
List<Service> services = new ArrayList<>();
try {
log.info("Raster layer upload - about to parseRequest");
String newId = IdGenerator.generate();
String metadataId;
if (metadata == null || metadata.isEmpty()) {
throw new ServerErrorException("Metadata file is missing or empty.", Status.INTERNAL_SERVER_ERROR);
}
try {
log.info("Raster layer create - about to doCSWInsertFromString");
metadataId = MetadataUtil.doCSWInsertFromString(metadata);
} catch (IOException | ParserConfigurationException | SAXException ex) {
throw new ServerErrorException("Error inserting metadata to the CSW server.", Status.INTERNAL_SERVER_ERROR, ex);
}
log.info("Raster layer create - about to makeCSWServiceForUrl with metadataId: " + metadataId);
services.add(MetadataUtil.makeCSWServiceForUrl(MetadataUtil.getMetadataByIdUrl(metadataId)));
Bbox bbox = MetadataUtil.getBoundingBoxFromFgdcMetadata(metadata);
log.info("Starting CRS Identifier Lookup");
long startTime = System.nanoTime();
String EPSGcode = CRS.lookupIdentifier(MetadataUtil.getCrsFromFgdcMetadata(metadata), true);
long endTime = System.nanoTime();
// divide by 1000000 to get milliseconds
long duration = (endTime - startTime) / 1000000;
log.info("Finished CRS Identifier Lookup. Took " + duration + "ms.");
if (bbox == null || EPSGcode == null) {
throw new ServerErrorException("Unable to identify bbox or epsg code from metadata.", Status.INTERNAL_SERVER_ERROR);
}
log.info("Raster layer create - about to addRasterLayer to geoserver with an id of: " + newId);
log.info("Raster layer create - about to addRasterLayer to geoserver with a bbox: " + bbox.getBbox());
log.info("Raster layer create - about to addRasterLayer to geoserver with an EPSG of: " + EPSGcode);
Service rasterService = GeoserverUtil.addRasterLayer(geoserverEndpoint, zipFileStream, newId, bbox, EPSGcode);
if (null == rasterService) {
throw new ServerErrorException("Unable to create a store and/or layer in GeoServer.", Status.INTERNAL_SERVER_ERROR);
} else {
services.add(rasterService);
}
if (!services.isEmpty()) {
Layer layer = new Layer();
layer.setId(newId);
layer.setServices(services);
layer.setBbox(bbox);
try (LayerManager manager = new LayerManager()) {
manager.save(layer);
}
return Response.created(layerURI(layer)).build();
} else {
throw new ServerErrorException("Unable to create layer", Status.INTERNAL_SERVER_ERROR);
}
} catch (JAXBException | FactoryException | IOException ex) {
throw new ServerErrorException("Error parsing upload request", Status.INTERNAL_SERVER_ERROR, ex);
}
}
Aggregations