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

/**
 * @param configkey/*from  w ww  . j a v  a2 s  .c  om*/
 */
public static void removeConfiguration(String configkey) throws RegistryException {
    QueryGenerator queryGenerator = new QueryGenerator(AbstractResource.CONFIGURATION);
    queryGenerator.setParameter(AbstractResource.ConfigurationConstants.CONFIG_KEY, configkey);
    EntityManager em = null;
    try {
        if (isConfigurationExist(configkey)) {
            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();
        }
    }
}

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

public static boolean isConfigurationExists(String configKey, String configVal) throws RegistryException {
    EntityManager em = null;
    try {//from   www.  j a va  2s .c  o m
        //Currently categoryID is hardcoded value
        em = ResourceUtils.getEntityManager();
        Configuration existing = em.find(Configuration.class, new Configuration_PK(configKey, configVal,
                AbstractResource.ConfigurationConstants.CATEGORY_ID_DEFAULT_VALUE));
        em.close();
        return existing != null;
    } 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.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static ExperimentCatResource getGateway(String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {//from w  w w .java2  s .c o  m
        if (isGatewayExist(gatewayId)) {
            em = getEntityManager();
            Gateway gateway = em.find(Gateway.class, gatewayId);
            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.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static boolean isUserExist(String username, String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {//  ww  w  .ja va  2  s.co  m
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.USERS);
        generator.setParameter(AbstractExpCatResource.UserConstants.USERNAME, username);
        generator.setParameter(AbstractExpCatResource.UserConstants.GATEWAY_ID, gatewayId);
        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.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static ExperimentCatResource getUser(String userName, String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {// ww w. j a v  a  2  s . c o  m
        if (isUserExist(userName, gatewayId)) {
            em = getEntityManager();
            UserPK userPK = new UserPK();
            userPK.setUserName(userName);
            userPK.setGatewayId(gatewayId);
            Users user = em.find(Users.class, userPK);
            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.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static ExperimentCatResource getWorker(String gatewayId, String userName) throws RegistryException {
    EntityManager em = null;
    try {//ww  w  . j av  a 2 s  . c  om
        em = getEntityManager();
        GatewayWorkerPK gatewayWorkerPK = new GatewayWorkerPK();
        gatewayWorkerPK.setGatewayId(gatewayId);
        gatewayWorkerPK.setUserName(userName);
        GatewayWorker gatewayWorker = em.find(GatewayWorker.class, gatewayWorkerPK);
        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.registry.core.experiment.catalog.ExpCatResourceUtils.java

/**
 * @param gatewayId//from  www.  j a va 2s  .com
 * @return
 */
public static boolean isGatewayExist(String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.GATEWAY);
        generator.setParameter(AbstractExpCatResource.GatewayConstants.GATEWAY_ID, gatewayId);
        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.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static List<ExperimentCatResource> getAllGateways() throws RegistryException {
    List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>();
    EntityManager em = null;
    try {//from w  w w .ja  v  a2s  .  com
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.GATEWAY);
        Query q = generator.selectQuery(em);
        List results = q.getResultList();
        if (results.size() != 0) {
            for (Object result : results) {
                Gateway gateway = (Gateway) result;
                GatewayResource gatewayResource = (GatewayResource) Utils.getResource(ResourceType.GATEWAY,
                        gateway);
                resourceList.add(gatewayResource);
            }
        }
        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();
        }
    }
    return resourceList;
}

From source file:org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils.java

/**
 * @param gatewayId/*from   w  w w.  j  a v  a 2 s.  c  o m*/
 * @return
 */
public static boolean removeGateway(String gatewayId) {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.GATEWAY);
        generator.setParameter(AbstractExpCatResource.GatewayConstants.GATEWAY_ID, gatewayId);
        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.registry.core.experiment.catalog.ExpCatResourceUtils.java

/**
 * @param gatewayResource/*www .j  ava2s.  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(), gatewayResource.getGatewayId())) {
            userResource.save();
        }
        Gateway gateway = em.find(Gateway.class, gatewayResource.getGatewayId());
        GatewayWorker gatewayWorker = new GatewayWorker();
        gatewayWorker.setGateway(gateway);
        gatewayWorker.setUserName(userResource.getUserName());
        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();
        }
    }
}