Example usage for javax.persistence EntityManager createQuery

List of usage examples for javax.persistence EntityManager createQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createQuery.

Prototype

public Query createQuery(CriteriaDelete deleteQuery);

Source Link

Document

Create an instance of Query for executing a criteria delete query.

Usage

From source file:es.uvigo.ei.sing.rubioseq.gui.util.DBInitializer.java

/**
 * This method is responsible for the initializacion of the BD, that is:
 * - Creating the default RUbioSeqConfiguration.
 * - Creating the default users./*from w  ww . jav  a2s. c  o m*/
 * - Creating a datastore pointing to "/" for the admin user.
 * 
 * This method also plays a key role in the deployment of the application 
 * since it prints the message "[DBInitializer] DB initialized." which is
 * triggered by the launch-rubioseq-gui.sh in order to know that the app. is
 * deployed and launch a browser.
 * 
 * @author hlfernandez
 */
static void initDatabase() {
    System.out.println("[DBInitializer] Initializing DB ...");

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("rubioseq-database");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = null;
    try {
        /*
         * Store Global Configuration
         */
        if (em.createQuery("SELECT u FROM RUbioSeqConfiguration u").getResultList().size() == 0) {
            RUbioSeqConfiguration config = new RUbioSeqConfiguration();
            config.setRubioseqCommand("/opt/RUbioSeq3.7/RUbioSeq.pl");
            config.setPrivateDatastoresRootDirectory("/path/to/private/datastores/root");
            config.setCreatePrivateDatastoresOnUserRegistration(false);

            tx = em.getTransaction();
            try {
                tx.begin();
                em.persist(config);
                tx.commit();
            } finally {
                if (tx != null && tx.isActive()) {
                    tx.rollback();
                }
            }
        }
        /*
         * Create Default Users
         */
        if (em.createQuery("SELECT u FROM User u").getResultList().size() == 0) {
            User user = new User();
            user.setUsername("rubiosequser");
            user.setPassword(DigestUtils.md5Hex("rubioseqpass"));
            user.setAdmin(false);
            user.setEmail("rubiosequser@rubioseg.org");

            tx = em.getTransaction();
            try {
                tx.begin();
                em.persist(user);
                tx.commit();
            } finally {
                if (tx != null && tx.isActive()) {
                    tx.rollback();
                }
            }

            user = new User();
            user.setUsername("admin");
            user.setPassword(DigestUtils.md5Hex("admin"));
            user.setAdmin(true);
            user.setEmail("rubiosequser@rubioseg.org");

            tx = em.getTransaction();
            try {
                tx.begin();
                em.persist(user);
                tx.commit();
            } finally {
                if (tx != null && tx.isActive()) {
                    tx.rollback();
                }
            }
        }
        /*
         * Create Default Datastores
         */
        boolean createDefaultAdminDatastore = true;
        List<User> adminUsers = getAdminUsers(em);
        @SuppressWarnings("unchecked")
        List<DataStore> datastores = (List<DataStore>) em.createQuery("SELECT d FROM DataStore d")
                .getResultList();
        for (User adminUser : adminUsers) {
            if (datastores.size() == 0) {
                createDefaultAdminDatastore = true;
            } else {
                for (DataStore d : datastores) {
                    if (d.getUser() != null && d.getUser().equals(adminUser) && d.getPath().equals("/")) {
                        createDefaultAdminDatastore = false;
                    }
                }
            }
            if (createDefaultAdminDatastore) {
                DataStore adminDS = new DataStore();
                adminDS.setUser(adminUser);
                adminDS.setPath("/");
                adminDS.setMode(DataStoreMode.Private);
                adminDS.setType(DataStoreType.Input_Output);
                adminDS.setName(adminUser.getUsername() + "_default");

                tx = em.getTransaction();
                try {
                    tx.begin();
                    em.persist(adminDS);
                    tx.commit();
                } finally {
                    if (tx != null && tx.isActive()) {
                        tx.rollback();
                    }
                }
            }
        }
    } finally {
        em.close();
    }

    System.out.println("[DBInitializer] DB initialized.");
}

From source file:org.synyx.hades.dao.query.QueryUtils.java

/**
 * Creates a where-clause referencing the given entities and appends it to
 * the given query string. Binds the given entities to the query.
 * /*from ww  w.ja  v a  2s . c  o m*/
 * @param <T>
 * @param queryString
 * @param entities
 * @param entityManager
 * @return
 */
public static <T> Query applyAndBind(String queryString, Collection<T> entities, EntityManager entityManager) {

    Assert.notNull(queryString);
    Assert.notNull(entities);
    Assert.notNull(entityManager);

    Iterator<T> iterator = entities.iterator();

    if (!iterator.hasNext()) {
        return entityManager.createQuery(queryString);
    }

    String alias = detectAlias(queryString);
    StringBuilder builder = new StringBuilder(queryString);
    builder.append(" where");

    for (int i = 0; i < entities.size(); i++) {

        builder.append(String.format(" %s = ?%d", alias, i + 1));

        if (i < entities.size() - 1) {
            builder.append(" or");
        }
    }

    Query query = entityManager.createQuery(builder.toString());

    for (int i = 0; i < entities.size(); i++) {
        query.setParameter(i + 1, iterator.next());
    }

    return query;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<OnlineApplication> getAllOnlineApplications() {
    Logger.trace("Get All OnlineApplications from database.");

    List<OnlineApplication> result = null;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllOnlineApplications"));
    result = query.getResultList();/*from  www .jav  a2s .  c o m*/

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<OnlineApplication> getAllNewOnlineApplications() {
    Logger.trace("Get All OnlineApplications from database.");

    List<OnlineApplication> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllNewOnlineApplications"));
    result = query.getResultList();/*from  w  w w.  j  av a  2s  .  c  o m*/

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<UserDatabase> getAllUsers() {
    Logger.trace("Get All OnlineApplications from database.");

    List<UserDatabase> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllUsers"));
    result = query.getResultList();//  w w w .  j av  a  2  s  . c  o m

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<UserDatabase> getAllNewUsers() {
    Logger.trace("Get all new Users from Database");

    List<UserDatabase> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllNewUsers"));
    result = query.getResultList();/*from w w w  .  ja va  2 s.co  m*/

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<UserDatabase> getAllOpenUsersRequests() {
    Logger.trace("Get all new Users from Database");

    List<UserDatabase> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllOpenUsersRequests"));
    result = query.getResultList();//from w  w w.j a v  a2  s  .c  o m

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static MOAIDConfiguration getMOAIDConfiguration() {
    Logger.trace("Load MOAID Configuration from database.");

    List<MOAIDConfiguration> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getMOAIDConfiguration"));
    result = query.getResultList();/* w w  w .  j  av  a2 s .co m*/

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found. Create fresh instance.");
        return null;
    }

    return (MOAIDConfiguration) result.get(0);
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static List<OnlineApplication> getAllActiveOnlineApplications() {
    Logger.trace("Get All active OnlineApplications from database.");

    List<OnlineApplication> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getAllActiveOnlineApplications"));
    result = query.getResultList();// w w w .ja v a  2s.  c  o  m

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        result = new ArrayList<OnlineApplication>();

    }
    return result;
}

From source file:at.gv.egovernment.moa.id.commons.db.ConfigurationDBRead.java

public static UserDatabase getUserWithID(long id) {
    MiscUtil.assertNotNull(id, "UserID");
    Logger.trace("Getting Userinformation with ID " + id + " from database.");

    List<UserDatabase> result;
    EntityManager session = ConfigurationDBUtils.getCurrentSession();

    javax.persistence.Query query = session.createQuery(QUERIES.get("getUserWithUserID"));
    query.setParameter("id", id);
    result = query.getResultList();/* ww  w .j a  v a 2 s. c om*/

    Logger.trace("Found entries: " + result.size());

    if (result.size() == 0) {
        Logger.trace("No entries found.");
        return null;
    }
    return (UserDatabase) result.get(0);
}