Java tutorial
/* * Copyright Paolo Dragone 2014. * Copyright Alessandro Ronca 2014. * * This file is part of Wiktionary Ontology. * * Wiktionary Ontology is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Wiktionary Ontology is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Wiktionary Ontology. If not, see <http://www.gnu.org/licenses/>. */ package org.dragoneronca.nlp.wol.domain; import org.apache.commons.configuration.PropertiesConfiguration; import org.dragoneronca.nlp.wol.WolConfiguration; import org.dragoneronca.nlp.wol.domain.entities.*; import org.dragoneronca.util.nlp.Language; import org.dragoneronca.util.nlp.POS; import javax.persistence.*; import java.util.Iterator; /** * The entry point to the domain of entities of Wiktionary Ontology project. * <p/> * It provides access to thread-safe EntityManager instances and is used as interface for retrieving * objects from the database. * * @author Paolo Dragone * @author Alessandro Ronca */ public class WolDomainContext { private static final String CONFIG_FILE = "environment"; private static final String PERSISTENCE_UNIT_NAME = "db.persistence_unit_name"; private static WolDomainContext instance; private static boolean first = true; private final EntityManagerFactory entityManagerFactory; private final ThreadLocal<EntityManager> tlEntityManager; private WolDomainContext() { WolConfiguration wolConfiguration = WolConfiguration.getInstance(); PropertiesConfiguration configuration = wolConfiguration.getConfiguration(CONFIG_FILE); String persistenceUnitName = configuration.getString(PERSISTENCE_UNIT_NAME); entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); tlEntityManager = new ThreadLocal<>(); } /** * Finds a Page by its unique title. * * @param title The title of the page (case-sensitive) * @return The retrieved Page or null if no Page with the given title. */ public Page findPageByTitle(String title) { Query q = getEntityManager().createNamedQuery("findPageByTitle"); q.setParameter(1, title); try { return (Page) q.getSingleResult(); } catch (NoResultException ignore) { } catch (Exception e) { e.printStackTrace(); } return null; } /** * Gets a thread-safe instance of the EntityManager. * <p/> * Be aware that sharing entities between threads may lead to detaching problems. * * @return A thread specific instance of the EntityManager. */ public EntityManager getEntityManager() { EntityManager entityManager = tlEntityManager.get(); if (entityManager == null) { entityManager = entityManagerFactory.createEntityManager(); tlEntityManager.set(entityManager); } if (first) { first = false; createIndices(entityManager); } return entityManager; } private void createIndices(EntityManager entityManager) { entityManager.getTransaction().begin(); Query q1 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSESET_UNIQUE ON SenseSet (word, " + "language, POS)"); q1.executeUpdate(); Query q2 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSE_UNIQUE ON Sense (word, language, " + "POS, number)"); q2.executeUpdate(); /*Query q3 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSERELATION_UNIQUE ON SenseRelation (id," + " DTYPE)"); q3.executeUpdate(); Query q4 = entityManager.createNativeQuery( "CREATE INDEX IF NOT EXISTS SENSERELATION_DTYPE ON SenseRelation (DTYPE)"); q4.executeUpdate(); Query q5 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSESET_SENSERELATION_DERIVEDTERMS_ID ON " + "SenseSet_SenseRelation (SenseSet_id, DerivedTerms_id)"); q5.executeUpdate(); Query q6 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSESET_SENSERELATION_RELATEDTERMS_ID ON " + "SenseSet_SenseRelation (SenseSet_id, RelatedTerms_id)"); q6.executeUpdate(); Query q7 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSESET_SENSERELATION_SYNONYMS_ID ON " + "SenseSet_SenseRelation (SenseSet_id, Synonyms_id)"); q7.executeUpdate(); Query q8 = entityManager.createNativeQuery( "CREATE UNIQUE INDEX IF NOT EXISTS SENSESET_SENSERELATION_TRANSLATIONS_ID ON " + "SenseSet_SenseRelation (SenseSet_id, Translations_id)"); q8.executeUpdate();*/ entityManager.getTransaction().commit(); } public Iterator<Sense> senseIterator() { return new WolEntityIterator<>(Sense.class, "getAllSenses"); } public Iterator<Sense> senseIterator(boolean clear) { return new WolEntityIterator<>(Sense.class, "getAllSenses", 50000, false, clear); } public Iterator<Sense> senseIterator(int max) { return new WolEntityIterator<>(Sense.class, "getAllSenses", max); } public Iterator<Sense> senseIterator(String word, Language language, POS pos) { WolEntityIterator<Sense> iterator = new WolEntityIterator<>(Sense.class, "findSensesByWordLanguagePOS"); iterator.setParameters(word, language, pos); return iterator; } public Iterator<SenseSet> senseSetIterator(int max) { return new WolEntityIterator<>(SenseSet.class, "getAllSenseSets", max, true); } public Iterator<Sense> senseIterator(String targetWord) { WolEntityIterator<Sense> iterator = new WolEntityIterator<>(Sense.class, "findSensesByWord"); iterator.setParameters(targetWord); return iterator; } // [id, originsense_id, targetsense_id, targetsense_word, correctness, probability] public Iterator<Object[]> edgesIterator() { WolDomainContext domainContext = WolDomainContext.getInstance(); TypedQuery<Object[]> q = domainContext.getEntityManager().createNamedQuery("getSemanticEdgesMap", Object[].class); return q.getResultList().iterator(); } /** * Singleton access to the WolDomainContext. * * @return The WolDomainContext global instance. */ public static WolDomainContext getInstance() { if (instance == null) { instance = new WolDomainContext(); } return instance; } public void updateScore(int id, double score) { WolDomainContext domainContext = WolDomainContext.getInstance(); EntityManager entityManager = domainContext.getEntityManager(); SemanticEdge edge = entityManager.find(SemanticEdge.class, id); if (edge == null || edge instanceof TermSemanticEdge) { Query q = entityManager.createNamedQuery("updateScoreTermSemanticEdge"); q.setParameter(1, score).setParameter(2, id); q.executeUpdate(); } else { Query q = entityManager.createNamedQuery("updateScoreSemanticEdge"); q.setParameter(1, score).setParameter(2, id); q.executeUpdate(); } } }