Example usage for javax.persistence EntityManager find

List of usage examples for javax.persistence EntityManager find

Introduction

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

Prototype

public <T> T find(Class<T> entityClass, Object primaryKey);

Source Link

Document

Find by primary key.

Usage

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

public static ExperimentCatResource getGateway(String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {//ww  w . j  a va2s. co 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:fredboat.db.entity.SearchResult.java

/**
 * @param playerManager the PlayerManager to perform encoding and decoding with
 * @param provider      the search provider that shall be used for this search
 * @param searchTerm    the query to search for
 * @param maxAgeMillis  the maximum age of the cached search result; provide a negative value for eternal cache
 * @return the cached search result; may return null for a non-existing or outdated search
 *//*from  w  ww  .  j a  v a2  s  . c  o  m*/
public static AudioPlaylist load(AudioPlayerManager playerManager, SearchUtil.SearchProvider provider,
        String searchTerm, long maxAgeMillis) throws DatabaseNotReadyException {
    DatabaseManager dbManager = FredBoat.getDbManager();
    if (dbManager == null || !dbManager.isAvailable()) {
        throw new DatabaseNotReadyException();
    }

    EntityManager em = dbManager.getEntityManager();
    SearchResult sr;
    SearchResultId sId = new SearchResultId(provider, searchTerm);
    try {
        em.getTransaction().begin();
        sr = em.find(SearchResult.class, sId);
        em.getTransaction().commit();
    } catch (PersistenceException e) {
        log.error("Unexpected error while trying to look up a search result for provider {} and search term {}",
                provider.name(), searchTerm, e);
        throw new DatabaseNotReadyException(e);
    } finally {
        em.close();
    }

    if (sr != null && (maxAgeMillis < 0 || System.currentTimeMillis() < sr.timestamp + maxAgeMillis)) {
        return sr.getSearchResult(playerManager);
    } else {
        return null;
    }
}

From source file:org.apache.juddi.validation.ValidateValueSetValidation.java

public static Tmodel GetTModel_MODEL_IfExists(String tmodelKey) throws ValueNotAllowedException {
    EntityManager em = PersistenceManager.getEntityManager();

    Tmodel model = null;// w ww.j  a v  a 2  s .c  om
    if (em == null) {
        //this is normally the Install class firing up
        log.warn(new ErrorMessage("errors.tmodel.ReferentialIntegrityNullEM"));
        return null;
    } else {

        EntityTransaction tx = em.getTransaction();
        try {

            tx.begin();
            model = em.find(org.apache.juddi.model.Tmodel.class, tmodelKey);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }

    }
    return model;
}

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 {//w w w  . ja v  a2  s.  c  o 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:actors.SessionCleaner.java

public static boolean clean(WebSession session, boolean timedout, boolean forceCleanup) {
    if (session == null) {
        return false;
    }/*from  w ww  .  java2  s . co m*/

    Logger.info("deleting session " + UuidUtils.normalize(session.getId()));
    List<ProgressObserverToken> poTokens = ProgressObserverToken.find.where().eq("session", session).findList();
    for (ProgressObserverToken poToken : poTokens) {
        poToken.delete();
    }

    if (session.getStatus() == SessionStatus.ALIVE || forceCleanup) {
        session.setStatus(timedout ? SessionStatus.TIMEDOUT : SessionStatus.KILLED);
        session.update();

        // if the session is not authenticated, delete all stores owned.
        if (!SessionedAction.isAuthenticated(session)) {
            EntityManager em = SareTransactionalAction.createEntityManager();
            em.getTransaction().begin();

            // delete all owned stores.
            for (String uuid : new PersistentDocumentStoreController().getAllUuids(em,
                    UuidUtils.normalize(session.getId()))) {
                Logger.info("deleting store " + uuid + " owned by " + UuidUtils.normalize(session.getId()));
                PersistentDocumentStore store = em.find(PersistentDocumentStore.class, UuidUtils.toBytes(uuid));
                if (store != null) {
                    em.remove(store);
                }
            }

            em.getTransaction().commit();
            em.close();
        }
    }

    return true;
}

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

public static Resource getUser(String userName) throws RegistryException {
    EntityManager em = null;
    try {//from   ww w  .  ja v a2 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.juddi.validation.ValidateValueSetValidation.java

/**
 * return the publisher/*  ww  w .jav a2 s  .c o  m*/
 *
 * @param tmodelKey
 * @return
 * @throws ValueNotAllowedException
 */
public static TModel GetTModel_API_IfExists(String tmodelKey) throws ValueNotAllowedException {
    EntityManager em = PersistenceManager.getEntityManager();

    TModel apitmodel = null;
    if (em == null) {
        //this is normally the Install class firing up
        log.warn(new ErrorMessage("errors.tmodel.ReferentialIntegrityNullEM"));
        return null;
    } else {

        EntityTransaction tx = em.getTransaction();
        try {
            Tmodel modelTModel = null;
            tx.begin();
            modelTModel = em.find(org.apache.juddi.model.Tmodel.class, tmodelKey);
            if (modelTModel != null) {
                apitmodel = new TModel();
                try {
                    MappingModelToApi.mapTModel(modelTModel, apitmodel);
                } catch (DispositionReportFaultMessage ex) {
                    log.warn(ex);
                    apitmodel = null;
                }

            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }

    }
    return apitmodel;
}

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

public static Resource getGateway(String gatewayName) throws RegistryException {
    EntityManager em = null;
    try {//  w w w  .j ava  2s  .  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 isConfigurationExists(String configKey, String configVal) throws RegistryException {
    EntityManager em = null;
    try {// w ww  . j a va  2  s. c om
        //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 getWorker(String gatewayId, String userName) throws RegistryException {
    EntityManager em = null;
    try {//from  w ww .j  a  v  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();
        }
    }

}