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:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the granted number of calls./*w  w w  .j  a v  a 2  s  . co  m*/
 *
 * @param userName
 *            the user name.
 * @param serviceName
 *            the service name.
 * @param operationName
 *            the operation name.
 * @return the number of calls.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public int getGrantedRequestNumber(final String userName, final String serviceName,
        final String operationName) {
    Integer requests = 0;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    q.where(cb.equal(c.<String>get(AuthenticationType_.context), this.contextNameProvider.getContextName()),
            cb.equal(c.<String>get(AuthenticationType_.userName), userName));

    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    try {
        AuthenticationType log = typedQuery.getSingleResult();
        for (GrantedAuthorityType role : log.getGrantedAuthority()) {
            if (this.getRoleName(serviceName, operationName).equals(role.getRoleName())) {
                requests = role.getMaxRequests();
            }
        }
        if (requests == null) {
            requests = Integer.MAX_VALUE;
        }
    } catch (Exception e) {
        requests = 0;
    }
    return requests;
}

From source file:br.eti.danielcamargo.backend.hsnpts.core.business.MicrocicloService.java

/**
 * Se o microciclo existir e for incompleto, ele  retornado.
 * <br />//from   w  w  w . j  a  va 2s. co m
 * Se o microciclo no existir ou estiver completo, cria um novo.
 *
 * @param alunoId
 * @return
 * @throws
 * br.eti.danielcamargo.backend.hsnpts.core.business.exceptions.ProgramaFinalizadoException
 */
@Transactional
public Microciclo resolverMicrociclo(Long alunoId) throws ProgramaFinalizadoException {

    StringBuilder hql = new StringBuilder();

    hql.append("SELECT ");
    hql.append(" p ");
    hql.append("FROM ");
    hql.append("  Programa p ");
    hql.append("WHERE ");
    hql.append("  p.aluno.id = :alunoId ");
    hql.append("  AND p.dataTermino IS NULL ");

    TypedQuery<Programa> queryPrograma = em.createQuery(hql.toString(), Programa.class);
    queryPrograma.setParameter("alunoId", alunoId);
    Programa programa = queryPrograma.getSingleResult();

    hql.append("SELECT ");
    hql.append(" m ");
    hql.append("FROM ");
    hql.append(" Microciclo m ");
    hql.append("WHERE ");
    hql.append(" m.programa = :programa ");
    hql.append("ORDER BY ");
    hql.append(" m.numero DESC ");

    TypedQuery<Microciclo> queryMicrociclo = em.createQuery(hql.toString(), Microciclo.class);
    queryMicrociclo.setParameter("programa", programa);
    queryMicrociclo.setMaxResults(1);

    List<Microciclo> result = queryMicrociclo.getResultList();

    HsnPersonal hsnPersonal = HsnPersonalUtils.getInstance();
    ObjetivoPrograma objetivoPrograma = programa.getObjetivoPrograma();
    FrequenciaSemanal frequenciaSemanal = programa.getFrequenciaSemanal();

    if (result.isEmpty()) {
        //criar primeiro microciclo
        Microciclo microciclo = hsnPersonal.buscarMicrociclo(objetivoPrograma, frequenciaSemanal, 1);
        microciclo.setPrograma(programa);
        save(microciclo);
        return microciclo;
    } else {
        //verificar se o programa esta finalizado
        Microciclo microciclo = result.get(0);
        if (microciclo.getDataTermino() != null) {
            if (microciclo.getNumero().equals(24)) {
                throw new ProgramaFinalizadoException();
            }
            microciclo = hsnPersonal.buscarMicrociclo(objetivoPrograma, frequenciaSemanal, 1);
            microciclo.setPrograma(programa);
            save(microciclo);
            return microciclo;
        }
        return microciclo;
    }
}

From source file:com.simplecrud.dao.LogInDao.java

public Object getUserId(String username, String password) {

    TypedQuery<NewMember> q = em.createQuery(
            "SELECT n.id FROM NewMember n WHERE n.username = :username AND n.password = :password",
            NewMember.class);
    q.setParameter("username", username);
    q.setParameter("password", password);
    return q.getSingleResult();
}

From source file:top.knos.user.services.JpaDaoImpl.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    TypedQuery<User> tq = em.createQuery(usersByUsernameQuery, User.class).setParameter(1, username);
    UserDetails user = tq.getSingleResult();
    return fillAuthority(user);
}

From source file:com.simplecrud.dao.UserInfoDao.java

/**
 *
 * @param id/*  w w  w .ja  v  a  2 s  . c om*/
 * @return
 */
public Object getUserEmailbyId(Long id) {

    TypedQuery<NewMember> q = em.createQuery("SELECT n.email FROM NewMember n WHERE n.id = :id",
            NewMember.class);
    q.setParameter("id", id);
    return q.getSingleResult();
}

From source file:de.lava.marvin.whaosleaguepersistence.dao.impl.PlayerDaoImpl.java

@Override
public long getCountAllWinsForPlayer(Player p) {
    TypedQuery<Long> query = entityManager.createNamedQuery("countAllWinsForPlayer", Long.class);
    query.setParameter("player", p);
    return query.getSingleResult();
}

From source file:de.lava.marvin.whaosleaguepersistence.dao.impl.PlayerDaoImpl.java

@Override
public long getCountAllLosesForPlayer(Player p) {
    TypedQuery<Long> query = entityManager.createNamedQuery("countAllLosesForPlayer", Long.class);
    query.setParameter("player", p);
    return query.getSingleResult();
}

From source file:bzzAgent.BiteSizedBzzDaoJpa.java

@Override
public BiteSizedActivityEntity getById(Integer bzzactivityId) {
    String sql = "select bsb from BiteSizedActivityEntity bsb" + " left join fetch bsb.incentives incentive "
            + " join fetch bsb.campaignEntity c " + " join fetch c.campaignPermissionsEntity cp "
            + " left join fetch c.campaignProfiles profiles "
            + " left join fetch profiles.memberGroupEntity mg " + "where bsb.bzzActivityEntity.id = :id";

    final TypedQuery<BiteSizedActivityEntity> result = em.createQuery(sql, BiteSizedActivityEntity.class)
            .setParameter("id", bzzactivityId);
    return result.getSingleResult();
}

From source file:com.simplecrud.dao.LogInDao.java

/**
 * Find the user if exist in the database:
 *
 * @param username/*from w  ww  .  j a v a 2s .  c  o m*/
 * @param password
 * @return Object
 */
public Object getSpecificUsername(String username, String password) {

    TypedQuery<NewMember> q = em.createQuery(
            "SELECT n.username FROM NewMember n WHERE n.username = :username AND n.password = :password",
            NewMember.class);
    q.setParameter("username", username);
    q.setParameter("password", password);
    return q.getSingleResult();
}

From source file:com.simplecrud.dao.UserInfoDao.java

/**
 *
 * @param Id/*from w  w  w . j  a va 2 s. c om*/
 * @return
 */
public Object getUserNamebyId(Long Id) {

    TypedQuery<NewMember> q = em.createQuery("SELECT n.username FROM NewMember n WHERE n.id = :Id",
            NewMember.class);
    q.setParameter("Id", Id);
    return q.getSingleResult();
}