Search in sources :

Example 1 with DataDomain

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

the class DataDomainManager method load.

public DataDomain load(String id) {
    DataDomain domain = null;
    Query selectQuery = em.createQuery(HQL_SELECT_BY_ID);
    selectQuery.setParameter("id", id);
    List<DataDomain> resultList = selectQuery.getResultList();
    if (!resultList.isEmpty()) {
        domain = resultList.get(0);
    }
    return domain;
}
Also used : DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) Query(javax.persistence.Query)

Example 2 with DataDomain

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

the class DataDomainManager method regenerateDomainForItem.

/**
 * Delete the data domain associated with the given item and then rebuild
 * it from the WFS.
 * @param item The item to delete the associated data domain for
 * @return Boolean Representing whether or not the delete executed successfully.
 */
public boolean regenerateDomainForItem(Item item) {
    boolean didRegen = false;
    if (item == null) {
        throw new IllegalArgumentException("Item must be valid data item");
    }
    if (deleteDomainForItem(item)) {
        log.debug("Domain deleted. Regenerating...");
        SortedSet<String> domainVals = DataDomainUtility.retrieveDomainFromWFS(item);
        SortedSet<String> domainAsYears = DataDomainUtility.getDomainAsYears(domainVals);
        DataDomain domain = new DataDomain();
        domain.setItemId(item.getId());
        domain.setDomainValues(domainAsYears);
        save(domain);
        didRegen = true;
    }
    return didRegen;
}
Also used : DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain)

Example 3 with DataDomain

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

the class DataDomainManager method isPersisted.

public boolean isPersisted(String id) {
    boolean isPersisted = false;
    Query selectQuery = em.createQuery(HQL_SELECT_BY_ID);
    selectQuery.setParameter("id", id);
    List<DataDomain> resultList = selectQuery.getResultList();
    if (!resultList.isEmpty()) {
        isPersisted = true;
    }
    return isPersisted;
}
Also used : DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) Query(javax.persistence.Query)

Example 4 with DataDomain

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

the class DataDomainManager method getDomainForItem.

/**
 * Grab the domain of the item data and store it for later
 * TODO if Item has been modified, invalidate DB persisted entity
 * @param item Item for which domain to gather
 * @return DataDomain object making up the domain
 */
public DataDomain getDomainForItem(Item item) {
    DataDomain domain = new DataDomain();
    if (item == null) {
        throw new IllegalArgumentException("Item must be valid data item");
    }
    Lock domainLock = getOrCreateLock(item.getId());
    try {
        domainLock.lock();
        if (isPersisted(item.getId())) {
            log.debug("Found domain in database, return it");
            domain = load(item.getId());
        } else {
            log.debug("No domain found in database, get it from WFS");
            SortedSet<String> domainVals = DataDomainUtility.retrieveDomainFromWFS(item);
            // only using this for years for now, separated out for future endeavors though
            SortedSet<String> domainAsYears = DataDomainUtility.getDomainAsYears(domainVals);
            domain.setItemId(item.getId());
            domain.setDomainValues(domainAsYears);
            save(domain);
        }
    } finally {
        domainLock.unlock();
    }
    return domain;
}
Also used : DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 5 with DataDomain

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

the class DataDomainResource method getDataDomain.

@GET
@Path("/item/{id}")
public Response getDataDomain(@PathParam("id") String id, @Context Request request) {
    Response response = null;
    try (ItemManager itemManager = new ItemManager();
        DataDomainManager domainManager = new DataDomainManager()) {
        Item item = itemManager.load(id);
        if (item == null || item.getType() != Item.Type.historical) {
            throw new NotFoundException("Only historical is supported at this time");
        }
        DataDomain domain = domainManager.getDomainForItem(item);
        Response checkModified = HTTPCachingUtil.checkModified(request, domain);
        if (checkModified != null) {
            response = checkModified;
        } else {
            Gson serializer = GsonUtil.getDefault();
            String domainJson = serializer.toJson(domain);
            response = Response.ok(domainJson, MediaType.APPLICATION_JSON_TYPE).lastModified(domain.getLastModified()).build();
        }
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) DataDomain(gov.usgs.cida.coastalhazards.model.util.DataDomain) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) DataDomainManager(gov.usgs.cida.coastalhazards.jpa.DataDomainManager) NotFoundException(javax.ws.rs.NotFoundException) Gson(com.google.gson.Gson) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

DataDomain (gov.usgs.cida.coastalhazards.model.util.DataDomain)5 Query (javax.persistence.Query)2 Gson (com.google.gson.Gson)1 DataDomainManager (gov.usgs.cida.coastalhazards.jpa.DataDomainManager)1 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)1 Item (gov.usgs.cida.coastalhazards.model.Item)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 Path (javax.ws.rs.Path)1 Response (javax.ws.rs.core.Response)1