List of usage examples for javax.persistence EntityManager getTransaction
public EntityTransaction getTransaction();
EntityTransaction
object. 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 .j a va2 s .c o 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>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Tests if an agent has expressed a Flame on a resource * @param _creator//w w w . j ava 2 s . 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. j ava 2 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:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Creates an AnnotationStatus that specifies another AnnotationStatus * @param label/*from www.j a v a 2 s. c o m*/ * @param comment * @param father * @return */ public boolean createAnnotationStatusChild(String label, String comment, String color, AnnotationStatus father, JSONArray descripteur) { label = StringOp.deleteBlanks(label); if (!StringOp.isNull(label)) { comment = StringOp.deleteBlanks(comment); AnnotationStatus _as = new AnnotationStatus(); _as.setComment(comment); _as.setLabel(label); _as.setFather(father); _as.setDescripteur(descripteur); _as.setColor(color); // EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); if (father.getId() != null) { AnnotationStatus _synchro_father = em.find(AnnotationStatus.class, father.getId()); if (_synchro_father != null) _as.setFather(_synchro_father); } em.persist(_as); tx.commit(); // em.close(); return true; } catch (Exception e) { System.out .println("[CreateAnnotationStatus.createAnnotationStatus] fails to create AnnotationStatus" + " label : " + label + " comment : " + comment + " cause : " + e.getMessage()); tx.rollback(); // em.close(); return false; } } else { System.out.println("[CreateAnnotationStatus.createAnnotationStatus] unable to persist AnnotationStatus" + " not a valid label : " + label); return false; } }
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 w w. j av a2 s. co m * @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:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Creates an AnnotationStatus/*from ww w . j a v a 2 s . c o m*/ * @param label * @param comment * @return */ public boolean createAnnotationStatus(String label, String comment, String color, JSONArray descripteur) { label = StringOp.deleteBlanks(label); if (!StringOp.isNull(label)) { comment = StringOp.deleteBlanks(comment); AnnotationStatus _as = new AnnotationStatus(); _as.setComment(comment); _as.setLabel(label); _as.setDescripteur(descripteur); _as.setColor(color); // 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(); if (annotationstatus != null) { System.out.println( "[CreateAnnotationStatus.createAnnotationStatus] this status already exists. Status : " + label); return false; } } catch (Exception e) { tx.rollback(); System.out.println( "[CreateAnnotationStatus.createAnnotationStatus] this status doesn't already exist. Status : " + label + " is gonna be created."); } try { tx.begin(); em.persist(_as); tx.commit(); // em.close(); return true; } catch (Exception e) { System.out .println("[CreateAnnotationStatus.createAnnotationStatus] fails to create AnnotationStatus" + " label : " + label + " comment : " + comment + " cause : " + e.getMessage()); tx.rollback(); // em.close(); return false; } } else { System.out.println("[CreateAnnotationStatus.createAnnotationStatus] unable to persist AnnotationStatus" + " not a valid label : " + label); return false; } }
From source file:nl.b3p.viewer.admin.stripes.LayoutManagerActionBean.java
public Resolution saveComponentConfig() { EntityManager em = Stripersist.getEntityManager(); if (component == null) { component = new ConfiguredComponent(); }//from ww w .ja v a 2 s . c o m component.setConfig(configObject); component.setName(name); component.setClassName(className); component.setApplication(application); Map<String, String> compDetails = new HashMap<String, String>(); try { if (componentLayout != null) { JSONObject compLayout = new JSONObject(componentLayout); for (Iterator<String> it = compLayout.keys(); it.hasNext();) { String key = it.next(); Object val = compLayout.get(key); compDetails.put(key, val.toString()); } } } catch (JSONException ex) { Logger.getLogger(LayoutManagerActionBean.class.getName()).log(Level.SEVERE, null, ex); } // details.put("layout", componentLayout); component.setDetails(compDetails); component.getReaders().clear(); component.setReaders(new HashSet<String>(groups)); em.persist(component); application.authorizationsModified(); em.getTransaction().commit(); return config(); }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Creates an Annotation/*from w ww.ja v a 2 s . c o m*/ * @param label * @param context_creation * @param hTMLContent * @param access * @param representsResource * @param status * @param added * @param annotated * @return */ public boolean createAnnotation(String label, String context_creation, URI access, URI representsResource, AnnotationStatus status, Collection<Resource> added, Collection<Resource> annotated, Collection<URI> annotatedURIs, Agent _creator) { label = StringOp.deleteBlanks(label); if (!StringOp.isNull(label)) { Annotation _annotation = new Annotation(); _annotation.setContextCreation(context_creation); _annotation.setCreation(new Date()); _annotation.setLabel(label); _annotation.setAccess(access); _annotation.setRepresentsResource(representsResource); _annotation.setStatus(status); _annotation.setCreator(_creator); //_annotation.setAdded(added); //_annotation.setAnnotated(annotated); //_annotation.setAnnotatedURIs(annotatedURIs); //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); if (representsResource.getId() != null) { URI _synchro_represents_resource = em.find(URI.class, representsResource.getId()); if (_synchro_represents_resource != null) _annotation.setRepresentsResource(_synchro_represents_resource); } if (access.getId() != null) { URI _synchro_access = em.find(URI.class, access.getId()); if (_synchro_access != null) _annotation.setAccess(_synchro_access); } if (status.getId() != null) { AnnotationStatus _synchro_status = em.find(AnnotationStatus.class, status.getId()); if (_synchro_status != null) _annotation.setStatus(_synchro_status); } if (_creator != null && _creator.getId() != null) { Agent _synchro_agent = em.find(_creator.getClass(), _creator.getId()); if (_synchro_agent != null) _annotation.setCreator(_synchro_agent); } Collection<Resource> _synchro_added = new ArrayList<Resource>(); for (Resource _to_add : added) { if (_to_add.getId() != null) { Resource _synchro_to_add = em.find(_to_add.getClass(), _to_add.getId()); if (_synchro_to_add != null) _synchro_added.add(_synchro_to_add); } else _synchro_added.add(_to_add); } _annotation.setAdded(_synchro_added); Collection<Resource> _synchro_annotated = new ArrayList<Resource>(); for (Resource _to_annotate : annotated) { if (_to_annotate.getId() != null) { Resource _synchro_to_annotate = em.find(_to_annotate.getClass(), _to_annotate.getId()); if (_synchro_to_annotate != null) _synchro_annotated.add(_synchro_to_annotate); } else _synchro_annotated.add(_to_annotate); } _annotation.setAnnotated(_synchro_annotated); Collection<URI> synchro_annotatedURIs = new ArrayList<URI>(); for (URI _to_annotate : annotatedURIs) { if (_to_annotate.getId() != null) { URI _synchro_to_annotate = em.find(_to_annotate.getClass(), _to_annotate.getId()); if (_synchro_to_annotate != null) { //empcher qu'une mme URI soit ajoute plusieurs fois une mme annotation if (!synchro_annotatedURIs.contains(_synchro_to_annotate)) synchro_annotatedURIs.add(_synchro_to_annotate); } } else synchro_annotatedURIs.add(_to_annotate); } _annotation.setAnnotatedURIs(synchro_annotatedURIs); em.persist(_annotation); tx.commit(); em.close(); return true; } catch (Exception e) { System.out.println( "[CreateAnnotation.createAnnotation] fails to create annotation" + " context creation : " + context_creation + " label : " + label + " cause : " + e.getMessage()); tx.rollback(); //em.close(); return false; } } else { System.out.println("[CreateAnnotation.createAnnotation] unable to persist annotation" + " not a valid label : " + label); return false; } }
From source file:com.headissue.pigeon.setup.DemoSetup.java
public DemoSetup() { setPersistenceConfig(new PersistenceConfig() { @Override/*from w w w .j a va 2 s. c o m*/ public String getUnitName() { return "pigeon-demo"; } @Override public void initializeDatabase(EntityManager _manager) { Survey _survey = new Survey(); _survey.setName("Default Survey"); _survey.setCreateAt(new Date()); _survey.setUpdateAt(new Date()); _survey.setStatus(SurveyStatus.ENABLED); Question q1 = new Question(); q1.setTitle("Default"); q1.setText("A Yes / No Question"); q1.setType(QuestionType.BOOL); q1.addAnswer("Yes", 1); q1.addAnswer("No", 2); _survey.addQuestion(q1); Question q2 = new Question(); q2.setTitle("Default"); q2.setText("Choose Question"); q2.setType(QuestionType.CHOICE); q2.addAnswer("A", 1); q2.addAnswer("B", 2); q2.addAnswer("C", 3); q2.addAnswer("D", 4); _survey.addQuestion(q2); Question q3 = new Question(); q3.setTitle("Default"); q3.setText("Multiple Question"); q3.setType(QuestionType.MULTIPLE); q3.addAnswer("1", 1); q3.addAnswer("2", 2); q3.addAnswer("3", 3); q3.addAnswer("4", 4); _survey.addQuestion(q3); Question q4 = new Question(); q4.setTitle("Default"); q4.setText("Free Text Question"); q4.setType(QuestionType.FREE); _survey.addQuestion(q4); _manager.getTransaction().begin(); _manager.persist(_survey); _manager.getTransaction().commit(); LogUtils.info(log, "Initialize the default survey (id=%s)", _survey.getId()); } }); }
From source file:com.enioka.jqm.api.HibernateClient.java
private void closeQuietly(EntityManager em) { try {// ww w . j a va2 s . c o m if (em != null) { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } } catch (Exception e) { // fail silently } }