use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.
the class ItemResource method updateItem.
/**
* @param request
* @param id
* @param content
* @return
*/
@RolesAllowed({ CoastalHazardsTokenBasedSecurityFilter.CCH_ADMIN_ROLE })
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateItem(@Context HttpServletRequest request, @PathParam("id") String id, String content) {
Response response = null;
try (ItemManager itemManager = new ItemManager()) {
Item dbItem = itemManager.load(id);
Item updatedItem = Item.fromJSON(content);
String trackId = null;
// If this is a storm going from active to inactive then remove the track item
if (dbItem.getType() == Item.Type.storms && !updatedItem.isActiveStorm() && dbItem.isActiveStorm()) {
Integer trackIndex = null;
// Find Track Child
for (Item child : updatedItem.getChildren()) {
if (child.getName().equals("track")) {
trackId = child.getId();
trackIndex = updatedItem.getChildren().indexOf(child);
break;
}
}
// Remove Track Child
if (trackId != null && trackIndex != null) {
updatedItem.getChildren().remove(trackIndex.intValue());
}
}
Item mergedItem = Item.copyValues(updatedItem, dbItem);
String mergedId = null;
if (dbItem == null) {
mergedId = itemManager.persist(mergedItem);
} else {
mergedId = itemManager.merge(mergedItem);
}
if (null != mergedId) {
// Delete the storm track item once the storm has been successfully saved
if (trackId != null) {
itemManager.delete(trackId, true);
}
response = Response.ok().build();
} else {
throw new BadRequestException();
}
try (StatusManager statusMan = new StatusManager();
ThumbnailManager thumbMan = new ThumbnailManager()) {
Status status = new Status();
status.setStatusName(Status.StatusName.ITEM_UPDATE);
statusMan.save(status);
thumbMan.updateDirtyBits(id);
}
}
return response;
}
use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.
the class SLDResource method getSLD.
/**
* XML representation of the SLD document related to a specific item ;qs=1
* is required to make this the default response when no accepts header is
* given
*
* @param id item ID
* @param ribbon which ribbon to represent (not required)
* @return response with SLD XML representation
*/
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_XML + ";qs=1")
public Response getSLD(@PathParam("id") String id, @QueryParam("ribbon") Integer ribbon, @QueryParam("selectedItem") String selectedId) {
PerformanceProfiler.startTimer("SLDResource.getSLD - " + id);
Response response = null;
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.getSLD_getGenerator - " + id);
SLDGenerator generator = SLDGenerator.getGenerator(item, selectedId, ribbon);
PerformanceProfiler.stopDebug("SLDResource.getSLD_getGenerator - " + id);
if (generator != null) {
PerformanceProfiler.startTimer("SLDResource.getSLD_generateSLD - " + id);
response = generator.generateSLD();
PerformanceProfiler.stopDebug("SLDResource.getSLD_generateSLD - " + id);
}
}
} catch (Exception e) {
response = Response.status(500).build();
}
PerformanceProfiler.stopDebug("SLDResource.getSLD - " + id);
return response;
}
use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.
the class TemplateResource method visibleItems.
private List<String> visibleItems(JsonArray children, List<Item> items, Map<String, Item> existing) {
List<String> visibleChildren = new LinkedList<>();
Iterator<JsonElement> iterator = children.iterator();
while (iterator.hasNext()) {
JsonObject child = iterator.next().getAsJsonObject();
JsonElement childId = child.get("id");
JsonElement attrElement = child.get("attr");
JsonElement visibleElement = child.get("visible");
String visibleId = "";
if (childId != null) {
String specified = childId.getAsString();
if (existing.containsKey(specified)) {
visibleId = existing.get(specified).getId();
} else {
visibleId = specified;
}
} else if (attrElement != null) {
String attr = attrElement.getAsString();
for (Item item : items) {
String existingAttr = item.getAttr();
if (null != existingAttr && attr.equals(existingAttr)) {
visibleId = item.getId();
}
}
}
if (visibleElement != null && StringUtils.isNotBlank(visibleId)) {
boolean visible = visibleElement.getAsBoolean();
if (visible) {
visibleChildren.add(visibleId);
}
}
}
return visibleChildren;
}
use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.
the class TemplateResource method copyExistingSummary.
private Summary copyExistingSummary(String copyType, String copyVal, ItemManager itemMan, AliasManager aliasMan) {
Summary newSummary = null;
Item summaryItem = null;
if (copyType.equalsIgnoreCase("item")) {
summaryItem = itemMan.load(copyVal);
} else if (copyType.equalsIgnoreCase("alias")) {
summaryItem = itemMan.load(aliasMan.load(copyVal).getItemId());
} else {
log.error("Attempted to copy existing summary from invalid copy type: " + copyType);
}
if (summaryItem != null) {
newSummary = Summary.copyValues(summaryItem.getSummary(), new Summary());
} else {
log.error("Item provided to copy summary from (" + copyType + " | " + copyVal + ") could not be loaded.");
}
return newSummary;
}
use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.
the class TemplateResource method templateItem.
private Item templateItem(Item template, String attr, Layer layer, Summary summary) {
String newId = IdGenerator.generate();
Item newItem = new Item();
newItem.setAttr(attr);
boolean isRibbonable = Attributes.getRibbonableAttrs().contains(attr);
List<Service> services = layer.getServices();
Bbox bbox = layer.getBbox();
List<Service> serviceCopies = new LinkedList<>();
for (Service service : services) {
serviceCopies.add(Service.copyValues(service, null));
}
newItem.setServices(serviceCopies);
newItem.setItemType(Item.ItemType.data);
newItem.setSummary(summary);
newItem.setId(newId);
newItem.setBbox(Bbox.copyValues(bbox, new Bbox()));
newItem.setActiveStorm(template.isActiveStorm());
newItem.setRibbonable(isRibbonable);
newItem.setType(template.getType());
newItem.setName(template.getName());
return newItem;
}
Aggregations