Search in sources :

Example 46 with Item

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;
}
Also used : Response(javax.ws.rs.core.Response) Status(gov.usgs.cida.coastalhazards.model.util.Status) Item(gov.usgs.cida.coastalhazards.model.Item) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ThumbnailManager(gov.usgs.cida.coastalhazards.jpa.ThumbnailManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 47 with Item

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;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) SLDGenerator(gov.usgs.cida.coastalhazards.sld.SLDGenerator) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 48 with Item

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;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) LinkedList(java.util.LinkedList)

Example 49 with Item

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;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary)

Example 50 with Item

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;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) OGCService(gov.usgs.cida.coastalhazards.util.ogc.OGCService) Service(gov.usgs.cida.coastalhazards.model.Service) LinkedList(java.util.LinkedList)

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