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

/**
 * Tests if an agent has expressed a Spaming on a resource
 * @param _creator//w  w w.j  av  a2s .co  m
 * @param _url_to_test
 * @return
 */
public boolean spamExpressed(Agent _creator, String _url_to_test) {
    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_to_test)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            System.out.println("[RetrieveAnnotation.spamExpressed] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url_to_test);
            return false;
        }
        AnnotationStatus status_spam = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Spam").getSingleResult();
        if (status_spam != null) {
            Object nb_annotations_troll = em.createQuery(
                    " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? and annotation.creator=? and annotation.status=?")
                    .setParameter(1, _uri).setParameter(2, _creator).setParameter(3, status_spam)
                    .getSingleResult();
            if (nb_annotations_troll instanceof Long) {
                nb = ((Long) nb_annotations_troll).longValue();
            }
        }
        tx.commit();
        if (nb > 0)
            return true;
        else
            return false;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.spamExpressed] unable to retrieve Annotations" + " on url : "
                + _url_to_test + " cause : " + e.getMessage());
        return false; //to prevent to express its opinion if the system fails
    }
}

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

/**
 * Tests if an agent has expressed a Flame on a resource
 * @param _creator/*from  w  w  w.  ja va2s  . c  o m*/
 * @param _url_to_test
 * @return
 */
public boolean flameExpressed(Agent _creator, String _url_to_test) {
    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_to_test)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            System.out.println("[RetrieveAnnotation.flameExpressed] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url_to_test);
            return false;
        }
        AnnotationStatus status_flame = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Flame")
                .getSingleResult();
        if (status_flame != null) {
            Object nb_annotations_flame = em.createQuery(
                    " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? and annotation.creator=? and annotation.status=?")
                    .setParameter(1, _uri).setParameter(2, _creator).setParameter(3, status_flame)
                    .getSingleResult();
            if (nb_annotations_flame instanceof Long) {
                nb = ((Long) nb_annotations_flame).longValue();
            }
        }
        tx.commit();
        if (nb > 0)
            return true;
        else
            return false;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.flameExpressed] unable to retrieve Annotations" + " on url : "
                + _url_to_test + " cause : " + e.getMessage());
        return false; //to prevent to express its opinion if the system fails
    }
}

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

/**
 * Tests if an agent has expressed a Trolling on a resource
 * @param _creator//from  w w  w. ja  v a2 s  .  c  o  m
 * @param _url_to_test
 * @return
 */
public boolean trollExpressed(Agent _creator, String _url_to_test) {
    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_to_test)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            System.out.println("[RetrieveAnnotation.trollExpressed] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url_to_test);
            return false;
        }
        AnnotationStatus status_troll = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Troll")
                .getSingleResult();
        if (status_troll != null) {
            Object nb_annotations_troll = em.createQuery(
                    " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? and annotation.creator=? and annotation.status=?")
                    .setParameter(1, _uri).setParameter(2, _creator).setParameter(3, status_troll)
                    .getSingleResult();
            if (nb_annotations_troll instanceof Long) {
                nb = ((Long) nb_annotations_troll).longValue();
            }
        }
        tx.commit();
        if (nb > 0)
            return true;
        else
            return false;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.trollExpressed] unable to retrieve Annotations" + " on url : "
                + _url_to_test + " cause : " + e.getMessage());
        return false; //to prevent to express its opinion if the system fails
    }
}

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

/**
 * Saves clerk(s) to the persistence layer. This method is specific to
 * jUDDI. This is used for server to server subscriptions and for future use
 * with replication. Administrative privilege required.
 * @param body//from  w  w  w. ja  va  2 s .c o  m
 * @return ClerkDetail
 * @throws DispositionReportFaultMessage 
 */
public ClerkDetail saveClerk(SaveClerk body) throws DispositionReportFaultMessage {

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

        UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());

        new ValidateClerk(publisher).validateSaveClerk(em, body);

        ClerkDetail result = new ClerkDetail();

        List<org.apache.juddi.api_v3.Clerk> apiClerkList = body.getClerk();
        ;
        for (org.apache.juddi.api_v3.Clerk apiClerk : apiClerkList) {

            org.apache.juddi.model.Clerk modelClerk = new org.apache.juddi.model.Clerk();

            MappingApiToModel.mapClerk(apiClerk, modelClerk);

            Object existingUddiEntity = em.find(modelClerk.getClass(), modelClerk.getClerkName());
            if (existingUddiEntity != null) {
                em.merge(modelClerk);
            } else {
                em.persist(modelClerk);
            }

            result.getClerk().add(apiClerk);
        }

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

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

/**
 * Tests if an agent has expressed an opinion (agree or disagree) on a resource
 * @param _creator//from  w  ww . j av  a 2 s . c om
 * @param _url_to_test
 * @return
 */
public boolean agreementExpressed(Agent _creator, String _url_to_test) {
    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_to_test)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            System.out.println("[RetrieveAnnotation.agreementExpressed] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url_to_test);
            return false;
        }
        AnnotationStatus status_agree = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Accord")
                .getSingleResult();
        AnnotationStatus status_disagree = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Dsaccord")
                .getSingleResult();
        AnnotationStatus status_clarify = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, "A clarifier")
                .getSingleResult();
        //if(status_agree != null)
        if (status_agree != null || status_disagree != null || status_clarify != null) {
            Object nb_annotations_agree = em.createQuery(
                    " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? and annotation.creator=? and (annotation.status=? or annotation.status=? or annotation.status=?)")
                    .setParameter(1, _uri).setParameter(2, _creator).setParameter(3, status_agree)
                    .setParameter(4, status_disagree).setParameter(5, status_clarify).getSingleResult();
            if (nb_annotations_agree instanceof Long) {
                nb = ((Long) nb_annotations_agree).longValue();
            }
        }
        tx.commit();
        if (nb > 0)
            return true;
        else
            return false;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.agreementExpressed] unable to retrieve Annotations"
                + " on url : " + _url_to_test + " cause : " + e.getMessage());
        return false; //to prevent to express its opinion if the system fails
    }
}

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

public BusinessDetail getBusinessDetail(GetBusinessDetail body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {//from w  w  w .  j  a v a  2 s  .c o m
        new ValidateInquiry(null).validateGetBusinessDetail(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_BUSINESSDETAIL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

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

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

        BusinessDetail result = new BusinessDetail();

        List<String> businessKeyList = body.getBusinessKey();
        for (String businessKey : businessKeyList) {
            org.apache.juddi.model.BusinessEntity modelBusinessEntity = null;
            try {
                modelBusinessEntity = em.find(org.apache.juddi.model.BusinessEntity.class, businessKey);
            } catch (ClassCastException e) {
            }
            if (modelBusinessEntity == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.BusinessNotFound", businessKey));

            org.uddi.api_v3.BusinessEntity apiBusinessEntity = new org.uddi.api_v3.BusinessEntity();

            MappingModelToApi.mapBusinessEntity(modelBusinessEntity, apiBusinessEntity);

            result.getBusinessEntity().add(apiBusinessEntity);
        }

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

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

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

public TModelDetail saveTModel(SaveTModel body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {//from ww  w  .j  a  va 2s.c om
        tx.begin();

        UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());

        new ValidatePublish(publisher).validateSaveTModel(em, body, null);

        TModelDetail result = new TModelDetail();

        List<org.uddi.api_v3.TModel> apiTModelList = body.getTModel();
        for (org.uddi.api_v3.TModel apiTModel : apiTModelList) {

            org.apache.juddi.model.Tmodel modelTModel = new org.apache.juddi.model.Tmodel();

            MappingApiToModel.mapTModel(apiTModel, modelTModel);

            setOperationalInfo(em, modelTModel, publisher);

            em.persist(modelTModel);

            result.getTModel().add(apiTModel);

        }

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

        return result;
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(PublicationQuery.SAVE_TMODEL, QueryStatus.FAILED, procTime);
        throw drfm;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

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

public ServiceDetail getServiceDetail(GetServiceDetail body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {// w  w w  . ja  va2  s.com
        new ValidateInquiry(null).validateGetServiceDetail(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.GET_SERVICEDETAIL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

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

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

        ServiceDetail result = new ServiceDetail();

        List<String> serviceKeyList = body.getServiceKey();
        for (String serviceKey : serviceKeyList) {
            org.apache.juddi.model.BusinessService modelBusinessService = null;
            try {
                modelBusinessService = em.find(org.apache.juddi.model.BusinessService.class, serviceKey);
            } catch (ClassCastException e) {
            }
            if (modelBusinessService == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.ServiceNotFound", serviceKey));

            org.uddi.api_v3.BusinessService apiBusinessService = new org.uddi.api_v3.BusinessService();

            MappingModelToApi.mapBusinessService(modelBusinessService, apiBusinessService);

            result.getBusinessService().add(apiBusinessService);
        }

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

        return result;

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

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

public BindingDetail getBindingDetail(GetBindingDetail body) throws DispositionReportFaultMessage {
    long startTime = System.currentTimeMillis();
    try {/*from   ww w .ja va 2s .c  o  m*/
        new ValidateInquiry(null).validateGetBindingDetail(body);
    } catch (DispositionReportFaultMessage drfm) {
        long procTime = System.currentTimeMillis() - startTime;
        serviceCounter.update(InquiryQuery.FIND_TMODEL, QueryStatus.FAILED, procTime);
        throw drfm;
    }

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

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

        BindingDetail result = new BindingDetail();

        List<String> bindingKeyList = body.getBindingKey();
        for (String bindingKey : bindingKeyList) {
            org.apache.juddi.model.BindingTemplate modelBindingTemplate = null;
            try {
                modelBindingTemplate = em.find(org.apache.juddi.model.BindingTemplate.class, bindingKey);
            } catch (ClassCastException e) {
            }
            if (modelBindingTemplate == null)
                throw new InvalidKeyPassedException(
                        new ErrorMessage("errors.invalidkey.BindingTemplateNotFound", bindingKey));

            org.uddi.api_v3.BindingTemplate apiBindingTemplate = new org.uddi.api_v3.BindingTemplate();

            MappingModelToApi.mapBindingTemplate(modelBindingTemplate, apiBindingTemplate);

            result.getBindingTemplate().add(apiBindingTemplate);
        }

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

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

From source file:org.opencastproject.series.impl.persistence.SeriesServiceDatabaseImpl.java

@Override
public boolean storeSeriesAccessControl(String seriesId, AccessControlList accessControl)
        throws NotFoundException, SeriesServiceDatabaseException {
    if (accessControl == null) {
        logger.error("Access control parameter is <null> for series '{}'", seriesId);
        throw new IllegalArgumentException("Argument for updating ACL for series " + seriesId + " is null");
    }/*from www . ja  v a 2s  .c  om*/

    String serializedAC;
    try {
        serializedAC = AccessControlParser.toXml(accessControl);
    } catch (Exception e) {
        logger.error("Could not serialize access control parameter: {}", e.getMessage());
        throw new SeriesServiceDatabaseException(e);
    }
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    boolean updated = false;
    try {
        tx.begin();
        SeriesEntity entity = getSeriesEntity(seriesId, em);
        if (entity == null) {
            throw new NotFoundException("Series with ID " + seriesId + " does not exist.");
        }
        if (entity.getAccessControl() != null) {
            // Ensure this user is allowed to update this series
            String accessControlXml = entity.getAccessControl();
            if (accessControlXml != null) {
                AccessControlList acl = AccessControlParser.parseAcl(accessControlXml);
                User currentUser = securityService.getUser();
                Organization currentOrg = securityService.getOrganization();
                if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, EDIT_SERIES_PERMISSION)) {
                    throw new UnauthorizedException(
                            currentUser + " is not authorized to update ACLs on series " + seriesId);
                }
            }
            updated = true;
        }
        entity.setAccessControl(serializedAC);
        em.merge(entity);
        tx.commit();
        return updated;
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not update series: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SeriesServiceDatabaseException(e);
    } finally {
        em.close();
    }
}