Search in sources :

Example 1 with CycleIntroductionException

use of gov.usgs.cida.coastalhazards.exception.CycleIntroductionException in project coastal-hazards by USGS-CIDA.

the class ItemManager method persist.

public synchronized String persist(Item item) throws CycleIntroductionException {
    String id;
    EntityTransaction transaction = em.getTransaction();
    try {
        transaction.begin();
        persistItem(item);
        id = item.getId();
        transaction.commit();
    } catch (Exception ex) {
        log.debug("Exception during save", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        id = null;
        if (ex instanceof CycleIntroductionException) {
            throw ex;
        }
    }
    fixEnabledStatus();
    return id;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) PersistenceException(javax.persistence.PersistenceException)

Example 2 with CycleIntroductionException

use of gov.usgs.cida.coastalhazards.exception.CycleIntroductionException in project coastal-hazards by USGS-CIDA.

the class ItemManager method persistAll.

public synchronized boolean persistAll(List<Item> items) throws CycleIntroductionException {
    boolean worked = false;
    EntityTransaction transaction = em.getTransaction();
    try {
        transaction.begin();
        for (Item item : items) {
            persistItem(item);
        }
        transaction.commit();
        worked = true;
    } catch (Exception ex) {
        log.debug("Exception during save", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        if (ex instanceof CycleIntroductionException) {
            throw ex;
        }
    }
    fixEnabledStatus();
    return worked;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Item(gov.usgs.cida.coastalhazards.model.Item) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) PersistenceException(javax.persistence.PersistenceException)

Example 3 with CycleIntroductionException

use of gov.usgs.cida.coastalhazards.exception.CycleIntroductionException in project coastal-hazards by USGS-CIDA.

the class ItemManager method mergeAll.

public synchronized boolean mergeAll(List<Item> items) throws CycleIntroductionException {
    boolean worked = false;
    EntityTransaction transaction = em.getTransaction();
    try {
        transaction.begin();
        for (Item item : items) {
            mergeItem(item);
        }
        transaction.commit();
        worked = true;
    } catch (Exception ex) {
        log.debug("Exception during save", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        if (ex instanceof CycleIntroductionException) {
            throw ex;
        }
    }
    fixEnabledStatus();
    return worked;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Item(gov.usgs.cida.coastalhazards.model.Item) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) PersistenceException(javax.persistence.PersistenceException)

Example 4 with CycleIntroductionException

use of gov.usgs.cida.coastalhazards.exception.CycleIntroductionException in project coastal-hazards by USGS-CIDA.

the class ItemManager method merge.

public synchronized String merge(Item item) throws CycleIntroductionException {
    String id;
    EntityTransaction transaction = em.getTransaction();
    try {
        // Update old ancestor chain
        mergeAll(updateAncestors(item));
        transaction.begin();
        id = mergeItem(item);
        transaction.commit();
        fixEnabledStatus();
        // update new ancestor chain
        mergeAll(updateAncestors(item));
    } catch (Exception ex) {
        log.debug("Transaction failed on merge", ex);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        id = null;
    }
    return id;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) PersistenceException(javax.persistence.PersistenceException)

Example 5 with CycleIntroductionException

use of gov.usgs.cida.coastalhazards.exception.CycleIntroductionException in project coastal-hazards by USGS-CIDA.

the class ItemManager method mergeItem.

private synchronized String mergeItem(Item item) {
    String id;
    try (DataDomainManager dm = new DataDomainManager()) {
        if (anyCycles(item)) {
            throw new CycleIntroductionException();
        }
        List<Item> children = item.getChildren();
        List<Item> replaceList = new LinkedList<>();
        if (children != null) {
            for (Item child : children) {
                replaceList.add(load(child.getId()));
            }
            item.setChildren(replaceList);
        }
        em.merge(item);
        id = item.getId();
        // Clear the upstream data domains if this item has a domain
        if (dm.load(id) != null) {
            List<Item> domainsToDelete = findVisibleAncestors(item);
            for (Item toDelete : domainsToDelete) {
                dm.deleteDomainForItem(toDelete);
            }
        }
    } catch (Exception e) {
        log.error("Failed to save item: " + item.getId() + " | Error: " + e.getMessage());
        id = null;
    }
    return id;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) LinkedList(java.util.LinkedList) BadRequestException(gov.usgs.cida.coastalhazards.exception.BadRequestException) CycleIntroductionException(gov.usgs.cida.coastalhazards.exception.CycleIntroductionException) PersistenceException(javax.persistence.PersistenceException)

Aggregations

BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)5 CycleIntroductionException (gov.usgs.cida.coastalhazards.exception.CycleIntroductionException)5 PersistenceException (javax.persistence.PersistenceException)5 EntityTransaction (javax.persistence.EntityTransaction)4 Item (gov.usgs.cida.coastalhazards.model.Item)3 LinkedList (java.util.LinkedList)1