Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

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

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:org.apache.openmeetings.data.user.dao.UsersDao.java

public User getUserByEmail(String email) {
    try {/*from   w w w . ja va2 s  . c  o m*/
        TypedQuery<User> query = em.createNamedQuery("getUserByEmail", User.class);
        query.setParameter("email", email);
        query.setParameter("deleted", true);
        User us = null;
        try {
            us = query.getSingleResult();
        } catch (NoResultException ex) {
        }
        return us;
    } catch (Exception e) {
        log.error("[getUserByAdressesId]", e);
    }
    return null;
}

From source file:org.apache.openmeetings.data.user.dao.UsersDao.java

public Object getUserByHash(String hash) {
    try {//from w  ww .ja va 2s  .  c  om
        if (hash.length() == 0)
            return new Long(-5);
        TypedQuery<User> query = em.createNamedQuery("getUserByHash", User.class);
        query.setParameter("resethash", hash);
        query.setParameter("deleted", true);
        User us = null;
        try {
            us = query.getSingleResult();
        } catch (NoResultException ex) {
        }
        if (us != null) {
            return us;
        } else {
            return new Long(-5);
        }
    } catch (Exception e) {
        log.error("[getUserByAdressesId]", e);
    }
    return new Long(-1);
}

From source file:org.apache.openmeetings.data.user.UserManager.java

/**
 * get user-role 1 - user 2 - moderator 3 - admin
 * //  w  ww . j  a v  a 2  s .  c  o m
 * @param user_id
 * @return
 */
public Long getUserLevelByID(Long user_id) {

    try {
        if (user_id == null)
            return new Long(0);
        // For direct access of linked users
        if (user_id == -1) {
            return new Long(1);
        }

        TypedQuery<User> query = em.createQuery(
                "select c from User as c where c.user_id = :user_id AND c.deleted <> true", User.class);
        query.setParameter("user_id", user_id);
        User us = null;
        try {
            us = query.getSingleResult();
        } catch (NoResultException e) {
            // u=null}
        }

        if (us != null) {
            return us.getLevel_id();
        } else {
            return -1L;
        }
    } catch (Exception ex2) {
        log.error("[getUserLevelByID]", ex2);
    }
    return null;
}

From source file:org.apache.openmeetings.data.user.UserManager.java

public Long getUserLevelByIdAndOrg(Long user_id, Long organisation_id) {

    try {//from w  w  w. j av  a2  s  . c  om
        if (user_id == null)
            return new Long(0);
        // For direct access of linked users
        if (user_id == -1) {
            return new Long(1);
        }

        TypedQuery<User> query = em.createQuery(
                "select c from User as c where c.user_id = :user_id AND c.deleted <> true", User.class);
        query.setParameter("user_id", user_id);
        User us = null;
        try {
            us = query.getSingleResult();
        } catch (NoResultException e) {
            // u=null}
        }

        if (us != null) {

            if (us.getLevel_id() > 2) {
                return us.getLevel_id();
            } else {

                log.debug("user_id, organisation_id" + user_id + ", " + organisation_id);

                Organisation_Users ou = organisationManager.getOrganisation_UserByUserAndOrganisation(user_id,
                        organisation_id);

                log.debug("ou: " + ou);

                if (ou != null) {
                    if (ou.getIsModerator() != null && ou.getIsModerator()) {
                        return 2L;
                    } else {
                        return us.getLevel_id();
                    }
                } else {
                    return us.getLevel_id();
                }
            }

        } else {
            return -1L;
        }
    } catch (Exception ex2) {
        log.error("[getUserLevelByID]", ex2);
    }
    return null;
}

From source file:org.apache.openmeetings.data.user.UserManager.java

/**
 * /*from w ww.j a  va 2s .  c om*/
 * Find User by Id
 */
// -----------------------------------------------------------------------------------------------------
public User getUserById(Long id) {
    log.debug("Usermanagement.getUserById");

    if (id == null || id <= 0) {
        return null;
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> c = cq.from(User.class);
    Predicate condition = cb.equal(c.get("deleted"), false);
    Predicate subCondition = cb.equal(c.get("user_id"), id);
    cq.where(condition, subCondition);
    TypedQuery<User> q = em.createQuery(cq);
    User u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    } catch (NonUniqueResultException ex) {
    }

    return u;
}

From source file:org.apache.openmeetings.data.user.UserManager.java

public User getUserByIdAndDeleted(Long id) throws Exception {
    log.debug("Usermanagement.getUserById");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> c = cq.from(User.class);
    Predicate condition = cb.equal(c.get("user_id"), id);
    cq.where(condition);/*from ww  w  .  jav a  2s . co m*/
    TypedQuery<User> q = em.createQuery(cq);
    User u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    }

    return u;

}

From source file:org.apache.openmeetings.data.user.UserManager.java

/**
 * @author o.becherer Find User by LoginName (test existence of a active
 *         user with login - name/* ww  w.j  ava 2s  . co m*/
 */
// -----------------------------------------------------------------------------------------------------
public User getUserByLogin(String login) throws Exception {
    log.debug("Usermanagement.getUserByLogin : " + login);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> c = cq.from(User.class);
    Predicate condition = cb.equal(c.get("deleted"), false);
    Predicate subCondition = cb.equal(c.get("login"), login);
    cq.where(condition, subCondition);
    TypedQuery<User> q = em.createQuery(cq);
    User u = null;
    try {
        u = q.getSingleResult();
    } catch (NoResultException e) {
        // u=null}
    }

    return u;

}

From source file:org.apache.openmeetings.data.user.UserManager.java

/**
 * @param hash/*from w  w  w  .ja  v a2  s .  c  om*/
 * @return
 */
public User getUserByActivationHash(String hash) {
    try {
        String hql = "SELECT u FROM User as u " + " where u.activatehash = :activatehash"
                + " AND u.deleted <> :deleted";
        TypedQuery<User> query = em.createQuery(hql, User.class);
        query.setParameter("activatehash", hash);
        query.setParameter("deleted", true);
        User u = null;
        try {
            u = query.getSingleResult();
        } catch (NoResultException e) {
            // u=null}
        }
        return u;
    } catch (Exception e) {
        log.error("[getUserByActivationHash]", e);
    }
    return null;

}

From source file:org.apache.openmeetings.db.dao.basic.ConfigurationDao.java

@Override
public long count(String search) {
    TypedQuery<Long> q = em.createQuery(
            DaoHelper.getSearchQuery("Configuration", "c", search, true, true, null, searchFields), Long.class);
    return q.getSingleResult();
}

From source file:org.apache.openmeetings.db.dao.user.AbstractUserDao.java

public long count(String search, boolean excludeContacts, long currentUserId) {
    TypedQuery<Long> q = em.createQuery(DaoHelper.getSearchQuery("User", "u", search, true, true,
            getAdditionalWhere(excludeContacts), null, searchFields), Long.class);
    setAdditionalParams(q, excludeContacts, currentUserId);
    return q.getSingleResult();
}