Search in sources :

Example 41 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class DownloadUtility method stageSessionDownload.

/**
 * TODO stage all the items with some smarts about naming and such
 *
 * @param stageThis
 * @param stagingDir
 * @return
 * @throws java.io.IOException
 */
public static boolean stageSessionDownload(Session stageThis, File stagingDir) throws IOException, ConcurrentModificationException {
    boolean success = false;
    lock(stagingDir);
    List<String> missing = new LinkedList<>();
    try {
        Map<WFSService, SingleDownload> downloadMap = new HashMap<>(stageThis.getItems().size());
        for (SessionItem sessionItem : stageThis.getItems()) {
            Item item;
            try (ItemManager itemManager = new ItemManager()) {
                item = itemManager.load(sessionItem.getItemId());
            }
            populateDownloadMap(downloadMap, item);
        }
        List<String> namesUsed = new ArrayList<>(downloadMap.size());
        for (SingleDownload stagedDownload : downloadMap.values()) {
            while (namesUsed.contains(stagedDownload.getName())) {
                stagedDownload.incrementName();
            }
            namesUsed.add(stagedDownload.getName());
            // TODO try/catch this to isolate/retry problem downloads?
            boolean stage = stagedDownload.stage(stagingDir, missing);
            success = success || stage;
        }
    } finally {
        writeMissingFile(stagingDir, missing);
        writeReadmeFile(stagingDir);
        unlock(stagingDir);
    }
    return success;
}
Also used : SessionItem(gov.usgs.cida.coastalhazards.model.SessionItem) Item(gov.usgs.cida.coastalhazards.model.Item) SessionItem(gov.usgs.cida.coastalhazards.model.SessionItem) HashMap(java.util.HashMap) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) WFSService(gov.usgs.cida.coastalhazards.util.ogc.WFSService) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 42 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ShorelineTest method testGenerateSLDInfoSelected.

/**
 * Test of generateSLDInfo method, of class Shoreline with an overriding
 * selected item.
 */
@Test
public void testGenerateSLDInfoSelected() {
    Item item = new Item();
    Item selectedItem = new Item();
    item.setAttr("Date_");
    item.setId("abcd");
    item.setType(Item.Type.historical);
    selectedItem.setAttr("Date_");
    selectedItem.setId("efg");
    selectedItem.setType(Item.Type.historical);
    selectedItem.setItemType(Item.ItemType.aggregation);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    selectedItem.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Shoreline");
    summary.setTiny(tiny);
    item.setSummary(summary);
    selectedItem.setSummary(summary);
    SLDGenerator shoreline = new SLDGenerator(item, selectedItem.getId(), null, Shorelines.shorelines);
    Response response = shoreline.generateSLDInfo();
    String json = (String) response.getEntity();
    Map<String, Object> sldInfo = new Gson().fromJson(json, HashMap.class);
    List<Object> bins = (List) sldInfo.get("bins");
    Map<String, Object> bin = (Map) bins.get(0);
    Double year0 = ((List<Double>) bin.get("years")).get(0);
    String color = (String) bin.get("color");
    assertEquals(year0, 0.0f, 0.01f);
    assertEquals(color, "#ff0000");
}
Also used : Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Service(gov.usgs.cida.coastalhazards.model.Service) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 43 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ShorelineTest method testGenerateSLD.

@Test
public void testGenerateSLD() {
    Item item = new Item();
    item.setAttr("DATE_");
    item.setId("abcde");
    item.setType(Item.Type.historical);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Shoreline");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator shoreline = new SLDGenerator(item, item.getId(), null, Shorelines.shorelines);
    Response response = shoreline.generateSLD();
    Viewable sld = (Viewable) response.getEntity();
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Viewable(org.glassfish.jersey.server.mvc.Viewable) Service(gov.usgs.cida.coastalhazards.model.Service) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 44 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemLastUpdateComparatorTest method testMin.

@Test
public void testMin() {
    ItemLastUpdateComparator comparator = new ItemLastUpdateComparator();
    List<Item> items = new ArrayList<>();
    items.add(o1);
    items.add(o2);
    Item min = Collections.min(items, comparator);
    assertThat(o1.getId(), is(equalTo(min.getId())));
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 45 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemResource method getItem.

/**
 * Retrieves representation of an instance of
 * gov.usgs.cida.coastalhazards.model.Item
 *
 * @param id identifier of requested item
 * @param subtree whether to return all items below this as a subtree
 * @param request request object
 * @return JSON representation of the item(s)
 */
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getItem(@PathParam("id") String id, @DefaultValue("false") @QueryParam("subtree") boolean subtree, @Context Request request) {
    Response response = null;
    Item item = null;
    try (StatusManager statusMan = new StatusManager()) {
        try (ItemManager itemManager = new ItemManager()) {
            item = itemManager.load(id);
        }
        if (item == null) {
            throw new NotFoundException();
        } else {
            // Check when the item and/or structure was last modified, if at all.
            // - If both are null, use today's date.
            // - If one of the two is not null, use that.
            // - Else, if both are not null, use the latest between them.
            Status lastItemUpdate = statusMan.load(Status.StatusName.ITEM_UPDATE);
            Status lastStructureUpdate = statusMan.load(Status.StatusName.STRUCTURE_UPDATE);
            Date modified = new Date();
            if (lastItemUpdate != null && lastStructureUpdate != null) {
                // Both updates exist, so compare between them and choose the latest
                Date lastItemUpdateDate = lastItemUpdate.getLastUpdate();
                Date lastStructureUpdateDate = lastStructureUpdate.getLastUpdate();
                modified = lastItemUpdateDate.after(lastStructureUpdateDate) ? lastItemUpdateDate : lastStructureUpdateDate;
            } else {
                // least one exists and use that.
                if (lastItemUpdate != null) {
                    modified = lastItemUpdate.getLastUpdate();
                }
                if (lastStructureUpdate != null) {
                    modified = lastStructureUpdate.getLastUpdate();
                }
            }
            Response unmodified = HTTPCachingUtil.checkModified(request, modified);
            if (unmodified != null) {
                response = unmodified;
            } else {
                String jsonResult = item.toJSON(subtree);
                response = Response.ok(jsonResult, MediaType.APPLICATION_JSON_TYPE).lastModified(modified).build();
            }
        }
    }
    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) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) NotFoundException(javax.ws.rs.NotFoundException) Date(java.util.Date) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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