Example usage for javax.persistence EntityTransaction commit

List of usage examples for javax.persistence EntityTransaction commit

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction commit.

Prototype

public void commit();

Source Link

Document

Commit the current resource transaction, writing any unflushed changes to the database.

Usage

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves an AnnotationStatus in the database according to the specified label
 * @param label//from www . j  a  va2  s.  c  om
 * @return an AnnotationStatus that may be a new UriStatus with no value.
 */
public AnnotationStatus retrieveAnnotationStatus(String label) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        AnnotationStatus annotationstatus = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, label).getSingleResult();
        tx.commit();
        return annotationstatus;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println(
                "[RetrieveAnnotationStatus.retrieveAnnotationStatus] unable to retrieve AnnotationStatus"
                        + " label : " + label + " cause : " + e.getMessage());
        return new AnnotationStatus();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Computes the number of annotations for associated to a specified Resource through its url
 * @param _url//from ww  w .  j  a va2s  .c o  m
 * @return
 */
public long computeNbAnnotations(String _url) {
    long nb = 0;
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            System.out.println("[RetrieveAnnotation.computeNbAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return nb;
        }
        Object nb_annotations = em.createQuery(
                " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?")
                .setParameter(1, _uri).getSingleResult();
        //System.out.println("[RetrieveAnnotation.computeNbAnnotations] nb_annotations : " + nb_annotations + " classe : " + nb_annotations.getClass().getSimpleName());
        tx.commit();
        if (nb_annotations instanceof Long) {
            nb = ((Long) nb_annotations).longValue();
        }
        return nb;
    } catch (Exception e) {
        tx.rollback();
        return nb;
    }
}

From source file:org.apache.juddi.api.impl.UDDIInquiryImpl.java

public OperationalInfos getOperationalInfo(GetOperationalInfo body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {/*from   w  w w.j  av a2  s  . c o  m*/
        new ValidateInquiry(null).validateGetOperationalInfo(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_OPERATIONALINFO, QueryStatus.FAILED, procTime);
        throw drfm;
    }

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();

        if (isAuthenticated())
            this.getEntityPublisher(em, body.getAuthInfo());

        OperationalInfos result = new OperationalInfos();

        List<String> entityKeyList = body.getEntityKey();
        for (String entityKey : entityKeyList) {
            org.apache.juddi.model.UddiEntity modelUddiEntity = null;
            try {
                modelUddiEntity = em.find(org.apache.juddi.model.UddiEntity.class, entityKey);
            } catch (ClassCastException e) {
            }
            if (modelUddiEntity == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.EntityNotFound", entityKey));

            org.uddi.api_v3.OperationalInfo apiOperationalInfo = new org.uddi.api_v3.OperationalInfo();

            MappingModelToApi.mapOperationalInfo(modelUddiEntity, apiOperationalInfo);

            result.getOperationalInfo().add(apiOperationalInfo);
        }

        tx.commit();
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_OPERATIONALINFO, QueryStatus.SUCCESS, procTime);

        return result;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves all the annotations about a specified Resource, by ascendant or descendant chronological order
 * @param _url//from   w  w  w .  j a v a2 s.  c om
 * @param asc
 * @return
 */
public List<Annotation> retrieveAnnotations(String _url, boolean asc) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " order by annotation.id desc";
        if (asc)
            order_by_clause = " order by annotation.id asc";
        List<Annotation> annotations = ((List<Annotation>) em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?"
                        + order_by_clause)
                .setParameter(1, _uri).getResultList());
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:org.opencastproject.search.impl.persistence.SearchServiceDatabaseImpl.java

/**
 * {@inheritDoc}//from w w  w. j a  v  a2 s .co m
 *
 * @see org.opencastproject.search.impl.persistence.SearchServiceDatabase#deleteMediaPackage(String, Date)
 */
@Override
public void deleteMediaPackage(String mediaPackageId, Date deletionDate)
        throws SearchServiceDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();

        SearchEntity searchEntity = getSearchEntity(mediaPackageId, em);
        if (searchEntity == null)
            throw new NotFoundException("No media package with id=" + mediaPackageId + " exists");

        // Ensure this user is allowed to delete this episode
        String accessControlXml = searchEntity.getAccessControl();
        if (accessControlXml != null) {
            AccessControlList acl = AccessControlParser.parseAcl(accessControlXml);
            User currentUser = securityService.getUser();
            Organization currentOrg = securityService.getOrganization();
            if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, WRITE.toString()))
                throw new UnauthorizedException(
                        currentUser + " is not authorized to delete media package " + mediaPackageId);

            searchEntity.setDeletionDate(deletionDate);
            em.merge(searchEntity);
        }
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete episode {}: {}", mediaPackageId, e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SearchServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves all the annotations associated to a Resource specified by its URL, grouped by annotationStatus
 * @param _url/*from w w w  .  j  a  v  a  2 s  . co  m*/
 * @param asc
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByStatus(String _url, boolean asc) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        List<Annotation> annotations = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.status,"
                        + order_by_clause)
                .setParameter(1, _uri).getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves all the annotations associated to a specified Resource through its URL, ordered by FirstAuthor
 * @param _url//from  w w w  .  j a va  2s  . c o  m
 * @param asc
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByFirstAuthor(String _url, boolean asc) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        List<Annotation> annotations = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.creator,"
                        + order_by_clause)
                .setParameter(1, _uri).getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves a specified quantity of Annotations associated to a Resource specified by its URL
 * @param _url/*from   w  ww  . j  a  v  a 2s.  co m*/
 * @param asc
 * @param first_indice
 * @param max_results
 * @return
 */
public List<Annotation> retrieveAnnotations(String _url, boolean asc, int first_indice, int max_results) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " order by annotation.id desc";
        if (asc)
            order_by_clause = " order by annotation.id asc";
        Query _query = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?"
                        + order_by_clause)
                .setParameter(1, _uri);
        _query.setFirstResult(first_indice);
        _query.setMaxResults(max_results);
        List<Annotation> annotations = _query.getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves a specified quantity of annotations associated to a Resource specified by its URL and ordered by AnnotationStatus
 * @param _url//  ww  w .jav  a 2  s . co  m
 * @param asc
 * @param first_indice
 * @param max_results
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByStatus(String _url, boolean asc, int first_indice,
        int max_results) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        Query _query = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.status,"
                        + order_by_clause)
                .setParameter(1, _uri);
        _query.setFirstResult(first_indice);
        _query.setMaxResults(max_results);
        List<Annotation> annotations = _query.getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves a specified quantity of annotations associated to a specified Resource through its URL, ordered by FirstAuthor
 * @param _url/*from   w w  w.  ja v a  2  s .co m*/
 * @param asc
 * @param first_indice
 * @param max_results
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByFirstAuthor(String _url, boolean asc, int first_indice,
        int max_results) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        Query _query = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.creator,"
                        + order_by_clause)
                .setParameter(1, _uri);
        _query.setFirstResult(first_indice);
        _query.setMaxResults(max_results);
        List<Annotation> annotations = _query.getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}