Search in sources :

Example 61 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class OrganizationDatabaseImpl method deleteOrganization.

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#deleteOrganization(java.lang.String)
 */
@Override
public void deleteOrganization(String orgId) throws OrganizationDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaOrganization organization = getOrganizationEntity(orgId, em);
        if (organization == null)
            throw new NotFoundException("Organization " + orgId + " does not exist");
        em.remove(organization);
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete organization: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) NotFoundException(org.opencastproject.util.NotFoundException) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException)

Example 62 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class MailService method updateEmailConfiguration.

public EmailConfiguration updateEmailConfiguration(EmailConfiguration emailConfiguration) throws MailServiceException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        String orgId = securityService.getOrganization().getId();
        EmailConfigurationDto emailConfig = mergeEmailConfiguration(emailConfiguration, orgId, em);
        tx.commit();
        EmailConfiguration updatedEmailConfiguration = emailConfig.toEmailConfiguration();
        updateSmtpConfiguration(updatedEmailConfiguration);
        return updatedEmailConfiguration;
    } catch (Exception e) {
        logger.error("Could not update email configuration '{}': {}", emailConfiguration, e.getMessage());
        if (tx.isActive())
            tx.rollback();
        throw new MailServiceException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EmailConfigurationDto(org.opencastproject.messages.persistence.EmailConfigurationDto) MessagingException(javax.mail.MessagingException) NoResultException(javax.persistence.NoResultException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 63 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class TranscriptionJobControlDto method store.

/**
 * Store new job control
 */
public static TranscriptionJobControlDto store(EntityManager em, String mediaPackageId, String trackId, String transcriptionJobId, String jobStatus, long trackDuration) throws TranscriptionDatabaseException {
    TranscriptionJobControlDto dto = new TranscriptionJobControlDto(mediaPackageId, trackId, transcriptionJobId, new Date(), null, jobStatus, trackDuration);
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        em.persist(dto);
        tx.commit();
        return dto;
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new TranscriptionDatabaseException(e);
    } finally {
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Date(java.util.Date) NoResultException(javax.persistence.NoResultException)

Example 64 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class ThemesServiceDatabaseImpl method deleteTheme.

@Override
public void deleteTheme(long id) throws ThemesServiceDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        ThemeDto themeDto = getThemeDto(id, em);
        if (themeDto == null)
            throw new NotFoundException("No theme with id=" + id + " exists");
        tx = em.getTransaction();
        tx.begin();
        em.remove(themeDto);
        tx.commit();
        messageSender.sendObjectMessage(ThemeItem.THEME_QUEUE, MessageSender.DestinationType.Queue, ThemeItem.delete(id));
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete theme '{}': {}", id, ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();
        throw new ThemesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 65 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class CaptureAgentStateServiceImpl method deleteAgentFromDatabase.

/**
 * Removes an agent from the database.
 *
 * @param agentName
 *          The name of the agent you wish to remove.
 */
private void deleteAgentFromDatabase(String agentName) throws NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        String org = securityService.getOrganization().getId();
        Agent existing = getAgentEntity(agentName, org, em);
        if (existing == null)
            throw new NotFoundException();
        em.remove(existing);
        tx.commit();
        agentCache.invalidate(agentName.concat(DELIMITER).concat(org));
    } catch (RollbackException e) {
        logger.warn("Unable to commit to DB in deleteAgent.");
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Agent(org.opencastproject.capture.admin.api.Agent) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) RollbackException(javax.persistence.RollbackException)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)540 EntityManager (javax.persistence.EntityManager)447 Query (javax.persistence.Query)143 Person (org.datanucleus.samples.annotations.models.company.Person)101 List (java.util.List)90 TypedQuery (javax.persistence.TypedQuery)82 NoResultException (javax.persistence.NoResultException)77 ArrayList (java.util.ArrayList)69 NotFoundException (org.opencastproject.util.NotFoundException)61 PersistenceException (javax.persistence.PersistenceException)59 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)55 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)50 JPAQuery (org.datanucleus.api.jpa.JPAQuery)32 EntityManagerFactory (javax.persistence.EntityManagerFactory)29 Predicate (javax.persistence.criteria.Predicate)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)21 Connection (java.sql.Connection)20 RollbackException (javax.persistence.RollbackException)20 HashSet (java.util.HashSet)19 Employee (org.datanucleus.samples.annotations.models.company.Employee)19