Search in sources :

Example 6 with SchedulerServiceDatabaseException

use of org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method hasTransaction.

@Override
public boolean hasTransaction(String source) throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        Query q = em.createNamedQuery("Transaction.findBySource").setParameter("source", source).setParameter("org", orgId);
        q.getSingleResult();
        return true;
    } catch (NoResultException e) {
        return false;
    } catch (Exception e) {
        logger.error("Could not retrieve transaction with source '{}': {}", source, getStackTrace(e));
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 7 with SchedulerServiceDatabaseException

use of org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method touchLastEntry.

@Override
public void touchLastEntry(String agentId) throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        LastModifiedDto entity = em.find(LastModifiedDto.class, agentId);
        if (entity == null) {
            entity = new LastModifiedDto();
            entity.setCaptureAgentId(agentId);
            entity.setLastModifiedDate(new Date());
            em.persist(entity);
        } else {
            entity.setLastModifiedDate(new Date());
            em.merge(entity);
        }
        tx.commit();
    } catch (Exception e) {
        if (tx.isActive())
            tx.rollback();
        logger.error("Could not updated last modifed date of agent {} status: {}", agentId, getStackTrace(e));
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 8 with SchedulerServiceDatabaseException

use of org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method getTransactions.

@Override
@SuppressWarnings("unchecked")
public List<String> getTransactions() throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        Query q = em.createNamedQuery("Transaction.findAll").setParameter("org", orgId);
        List<TransactionDto> resultList = q.getResultList();
        return $(resultList).map(new Fn<TransactionDto, String>() {

            @Override
            public String apply(TransactionDto trx) {
                return trx.getId();
            }
        }).toList();
    } catch (Exception e) {
        logger.error("Could not retrieve transactions: {}", getStackTrace(e));
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Fn(com.entwinemedia.fn.Fn) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 9 with SchedulerServiceDatabaseException

use of org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method countQuarterConfirmedResponses.

public long countQuarterConfirmedResponses() throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("ExtendedEvent.countConfirmedByDateRange");
        setDateForQuarterQuery(q);
        Number countResult = (Number) q.getSingleResult();
        return countResult.longValue();
    } catch (Exception e) {
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 10 with SchedulerServiceDatabaseException

use of org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method storeTransaction.

@Override
public void storeTransaction(String id, String source) throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        Opt<TransactionDto> entityOpt = getTransactionDto(id, em);
        TransactionDto entity = entityOpt.getOr(new TransactionDto());
        if (entityOpt.isNone()) {
            entity.setId(id);
            entity.setOrganization(securityService.getOrganization().getId());
            entity.setSource(source);
            entity.setLastModifiedDate(new Date());
            em.persist(entity);
        } else {
            entity.setLastModifiedDate(new Date());
            em.merge(entity);
        }
        tx.commit();
    } catch (Exception e) {
        logger.error("Could not store transaction: {}", getStackTrace(e));
        if (tx.isActive())
            tx.rollback();
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Aggregations

EntityManager (javax.persistence.EntityManager)17 NoResultException (javax.persistence.NoResultException)17 SchedulerServiceDatabaseException (org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException)17 NotFoundException (org.opencastproject.util.NotFoundException)17 Query (javax.persistence.Query)10 EntityTransaction (javax.persistence.EntityTransaction)4 Date (java.util.Date)3 Fn (com.entwinemedia.fn.Fn)1 HashMap (java.util.HashMap)1