Example usage for javax.persistence EntityManager isOpen

List of usage examples for javax.persistence EntityManager isOpen

Introduction

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

Prototype

public boolean isOpen();

Source Link

Document

Determine whether the entity manager is open.

Usage

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

public static Resource getGateway(String gatewayName) throws RegistryException {
    EntityManager em = null;
    try {// ww w  .  j  a va2 s .c  om
        if (isGatewayExist(gatewayName)) {
            em = getEntityManager();
            Gateway gateway = em.find(Gateway.class, gatewayName);
            GatewayResource gatewayResource = (GatewayResource) Utils.getResource(ResourceType.GATEWAY,
                    gateway);
            em.close();
            return gatewayResource;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return null;
}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

public static boolean isUserExist(String username) throws RegistryException {
    EntityManager em = null;
    try {//  w ww .  ja  v  a  2 s . co  m
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractResource.USERS);
        generator.setParameter(AbstractResource.UserConstants.USERNAME, username);
        Query q = generator.selectQuery(em);
        int size = q.getResultList().size();
        em.getTransaction().commit();
        em.close();
        return size > 0;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

public static Resource getUser(String userName) throws RegistryException {
    EntityManager em = null;
    try {//from  w w  w.  j a v a  2 s .  c  o  m
        if (isUserExist(userName)) {
            em = getEntityManager();
            Users user = em.find(Users.class, userName);
            UserResource userResource = (UserResource) Utils.getResource(ResourceType.USER, user);
            em.close();
            return userResource;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return null;

}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

public static Resource getWorker(String gatewayName, String userName) throws RegistryException {
    EntityManager em = null;
    try {//from w  ww. j  a  va 2  s  .co m
        em = getEntityManager();
        Gateway_Worker gatewayWorker = em.find(Gateway_Worker.class,
                new Gateway_Worker_PK(gatewayName, userName));
        WorkerResource workerResource = (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER,
                gatewayWorker);
        em.close();
        return workerResource;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }

}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param gatewayName//from  w  w w.j  a  v a 2s.c o m
 * @return
 */
public static boolean isGatewayExist(String gatewayName) throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractResource.GATEWAY);
        generator.setParameter(AbstractResource.GatewayConstants.GATEWAY_NAME, gatewayName);
        Query q = generator.selectQuery(em);
        int size = q.getResultList().size();
        em.getTransaction().commit();
        em.close();
        return size > 0;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }

}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param gatewayName/*w w  w  . j av  a  2s  .c  o  m*/
 * @return
 */
public static boolean removeGateway(String gatewayName) {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractResource.GATEWAY);
        generator.setParameter(AbstractResource.GatewayConstants.GATEWAY_NAME, gatewayName);
        Query q = generator.deleteQuery(em);
        q.executeUpdate();
        em.getTransaction().commit();
        em.close();
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param gatewayResource/* w ww .j  a  v a  2  s  .co  m*/
 * @param userResource
 */
public static WorkerResource addGatewayWorker(GatewayResource gatewayResource, UserResource userResource)
        throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        if (!isGatewayExist(gatewayResource.getGatewayName())) {
            gatewayResource.save();
        }
        if (!isUserExist(userResource.getUserName())) {
            userResource.save();
        }
        Gateway gateway = em.find(Gateway.class, gatewayResource.getGatewayName());
        Users user = em.find(Users.class, userResource.getUserName());
        Gateway_Worker gatewayWorker = new Gateway_Worker();
        gatewayWorker.setGateway(gateway);
        gatewayWorker.setUser(user);
        em.persist(gatewayWorker);
        em.getTransaction().commit();
        em.close();
        return (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param gatewayResource/*from ww  w  . ja  va 2 s  .com*/
 * @param userResource
 * @return
 */
public static boolean removeGatewayWorker(GatewayResource gatewayResource, UserResource userResource) {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractResource.GATEWAY_WORKER);
        generator.setParameter(AbstractResource.GatewayWorkerConstants.GATEWAY_NAME,
                gatewayResource.getGatewayName());
        generator.setParameter(AbstractResource.UserConstants.USERNAME, userResource.getUserName());
        Query q = generator.deleteQuery(em);
        q.executeUpdate();
        em.getTransaction().commit();
        em.close();
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }

}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param configKey/*from w w w .j a v  a2s .c  o m*/
 * @return
 */
public static List<ConfigurationResource> getConfigurations(String configKey) {
    List<ConfigurationResource> list = new ArrayList<ConfigurationResource>();
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractResource.CONFIGURATION);
        generator.setParameter(AbstractResource.ConfigurationConstants.CONFIG_KEY, configKey);
        Query q = generator.selectQuery(em);
        List<?> resultList = q.getResultList();
        if (resultList.size() != 0) {
            for (Object result : resultList) {
                ConfigurationResource configurationResource = createConfigurationResourceObject(result);
                list.add(configurationResource);
            }
        }
        em.getTransaction().commit();
        em.close();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return list;
}

From source file:org.apache.airavata.persistance.registry.jpa.ResourceUtils.java

/**
 * @param configkey/*from  w w w  . j ava2s .  c om*/
 * @param configValue
 */
public static void removeConfiguration(String configkey, String configValue) throws RegistryException {
    QueryGenerator queryGenerator = new QueryGenerator(AbstractResource.CONFIGURATION);
    queryGenerator.setParameter(AbstractResource.ConfigurationConstants.CONFIG_KEY, configkey);
    queryGenerator.setParameter(AbstractResource.ConfigurationConstants.CONFIG_VAL, configValue);
    EntityManager em = null;
    try {
        if (isConfigurationExists(configkey, configValue)) {
            em = getEntityManager();
            em.getTransaction().begin();
            Query q = queryGenerator.deleteQuery(em);
            q.executeUpdate();
            em.getTransaction().commit();
            em.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}