Search in sources :

Example 1 with SessionItem

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

the class SessionManager method removeItemFromSessions.

public synchronized boolean removeItemFromSessions(String itemId) {
    EntityTransaction transaction = em.getTransaction();
    for (String sessionId : getSessionIdsByItemId(itemId)) {
        try {
            Session sessionObj = loadSession(sessionId);
            List<SessionItem> newItems = new ArrayList<>(sessionObj.getItems());
            for (SessionItem item : sessionObj.getItems()) {
                if (item.getItemId().equals(itemId)) {
                    newItems.remove(item);
                }
            }
            transaction.begin();
            em.remove(sessionObj);
            sessionObj.setItems(newItems);
            em.persist(sessionObj);
            transaction.commit();
        } catch (Exception ex) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            log.error("An error occurred while modifying the session. Error: " + ex.getMessage());
            return false;
        }
    }
    return true;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) SessionItem(gov.usgs.cida.coastalhazards.model.SessionItem) ArrayList(java.util.ArrayList) PersistenceException(javax.persistence.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(gov.usgs.cida.coastalhazards.model.Session)

Example 2 with SessionItem

use of gov.usgs.cida.coastalhazards.model.SessionItem 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)

Aggregations

SessionItem (gov.usgs.cida.coastalhazards.model.SessionItem)2 ArrayList (java.util.ArrayList)2 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)1 Item (gov.usgs.cida.coastalhazards.model.Item)1 Session (gov.usgs.cida.coastalhazards.model.Session)1 WFSService (gov.usgs.cida.coastalhazards.util.ogc.WFSService)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 EntityTransaction (javax.persistence.EntityTransaction)1 PersistenceException (javax.persistence.PersistenceException)1