Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Criteria uniqueResult.

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

private boolean isUserAdminHelper(String ldapUserName, String roleName, ArkFunction arkFunction,
        ArkModule arkModule) throws EntityNotFoundException {
    boolean isAdminType = false;
    StatelessSession session = getStatelessSession();
    // Check or get user ark_user object based on ldapUserName
    ArkUser arkUser = getArkUser(ldapUserName);
    Criteria criteria = session.createCriteria(ArkUserRole.class);
    ArkRole arkRole = getArkRoleByName(roleName);
    criteria.add(Restrictions.eq("arkRole", arkRole));
    criteria.add(Restrictions.eq("arkUser", arkUser));
    criteria.add(Restrictions.eq("arkModule", arkModule));

    criteria.setMaxResults(1);//from  w ww  .jav a2  s  .c  o m
    ArkUserRole arkUserRole = (ArkUserRole) criteria.uniqueResult();
    if (arkUserRole != null) {
        isAdminType = true;
    }
    session.close();
    return isAdminType;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

public String getUserRoleForStudy(String ldapUserName, Study study) throws EntityNotFoundException {
    String roleName = "";
    ArkUser arkUser = getArkUser(ldapUserName);
    StatelessSession session = getStatelessSession();

    Criteria criteria = session.createCriteria(ArkUserRole.class);// getSession().createCriteria(ArkUserRole.class);
    criteria.createAlias("arkUser", "auserObject");
    criteria.add(Restrictions.eq("arkUser", arkUser));
    criteria.add(Restrictions.eq("auserObject.study", study));
    criteria.setMaxResults(1);//w w  w  .j  a  v  a2s  .c o m
    ArkUserRole arkUserRole = (ArkUserRole) criteria.uniqueResult();
    if (arkUserRole != null) {
        roleName = arkUserRole.getArkRole().getName();
    }
    session.close();
    return roleName;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

/**
 * Retrieve a Logged in user's role by providing the Ldap User Name, Usecase id, module id & or study id. We need the Ldap User Name & ArkUseCase
 * Id as a mandatory one./*from w w  w .ja  v  a  2 s  .c o m*/
 * 
 * @throws EntityNotFoundException
 */

@SuppressWarnings("unchecked")
public String getUserRole(String ldapUserName, ArkFunction arkFunction, ArkModule arkModule, Study study)
        throws EntityNotFoundException {
    String roleName = "";

    ArkUser arkUser = getArkUser(ldapUserName);
    Criteria criteria = getSession().createCriteria(ArkUserRole.class);
    criteria.createAlias("arkUser", "auserObject");

    criteria.add(Restrictions.eq("arkUser", arkUser));
    // Even if there is a study in session the criteria must be applied only if the logged in user has a study registered for him. Ie if he is not a
    // Super Admin
    if (!isSuperAdministrator(ldapUserName) && study != null) {

        criteria.add(Restrictions.eq("study", study));
        if (arkModule != null) {
            criteria.add(Restrictions.eq("arkModule", arkModule));
        }
        // criteria.setMaxResults(1);
        List<ArkUserRole> list = (List<ArkUserRole>) criteria.list();
        if (list.size() > 0) {
            ArkUserRole arkUserRole = (ArkUserRole) criteria.list().get(0);
            // ArkUserRole arkUserRole = (ArkUserRole)criteria.list().get(0);
            if (arkUserRole != null) {
                roleName = arkUserRole.getArkRole().getName();
            }
        }

    } else {
        if (arkModule != null) {
            criteria.add(Restrictions.eq("arkModule", arkModule));
        }

        criteria.setMaxResults(1);
        ArkUserRole arkUserRole = (ArkUserRole) criteria.uniqueResult();
        if (arkUserRole != null) {
            roleName = arkUserRole.getArkRole().getName();
        }
    }

    return roleName;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

public ArkFunction getArkFunctionByName(String functionName) {
    Criteria criteria = getSession().createCriteria(ArkFunction.class);
    criteria.add(Restrictions.eq("name", functionName));
    criteria.setMaxResults(1);// w  w w .  j a v  a  2 s . c  o m
    ArkFunction arkFunction = (ArkFunction) criteria.uniqueResult();
    return arkFunction;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

public ArkModule getArkModuleByName(String moduleName) {
    Criteria criteria = getSession().createCriteria(ArkModule.class);
    criteria.add(Restrictions.eq("name", moduleName));
    criteria.setMaxResults(1);//  w  w w.  j  av  a  2  s  . co  m
    ArkModule arkModule = (ArkModule) criteria.uniqueResult();
    return arkModule;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

public ArkModule getArkModuleById(Long moduleId) {
    Criteria criteria = getSession().createCriteria(ArkModule.class);
    criteria.add(Restrictions.eq("id", moduleId));
    criteria.setMaxResults(1);//from w  ww .  j  a  va2  s  .  c  o m
    ArkModule arkModule = (ArkModule) criteria.uniqueResult();
    return arkModule;
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

public Boolean isArkUserLinkedToStudies(ArkUser arkUser) {
    Boolean flag = false;/*from   w  ww.  j ava2 s.  co m*/

    Criteria criteria = getSession().createCriteria(ArkUserRole.class);
    criteria.add(Restrictions.eq("arkUser", arkUser));
    criteria.setProjection(Projections.rowCount());
    Long totalCount = (Long) criteria.uniqueResult();
    if (totalCount > 0) {
        flag = true;
    }
    return flag;
}

From source file:au.org.theark.core.dao.ArkUidGenerator.java

License:Open Source License

/**
 * TODO this should use a real fkey/*from ww w  .java2 s  .c  om*/
 * @param studyNameKy
 * @return
 */
public Integer getUidAndIncrement(String studyNameKy, int numToInsert) {
    Criteria criteria = getSession().createCriteria(SubjectUidSequence.class);
    criteria.add(Restrictions.eq("studyNameId", studyNameKy));
    SubjectUidSequence seqData = (SubjectUidSequence) criteria.uniqueResult();
    if (seqData == null) {
        //log.error("sequence does not exist...creating");
        SubjectUidSequence seq = new SubjectUidSequence(studyNameKy, numToInsert, false);
        getSession().persist(seq);
        getSession().flush();
        return new Integer(0);
    } else {
        int currentSeqNumber = seqData.getUidSequence();
        seqData.setUidSequence((currentSeqNumber + numToInsert));
        getSession().update(seqData);
        getSession().flush();
        return currentSeqNumber;//TODO ...perhaps this should be handled transactionally in one class, and probably with generators...although this isnt really even a key
    }
}

From source file:au.org.theark.core.dao.AuditDao.java

License:Open Source License

@Override
public boolean isAudited(Class<?> type) {
    Criteria criteria = getSession().createCriteria(AuditEntity.class);
    criteria.add(Restrictions.eq("classIdentifier", type.getName()));
    criteria.setProjection(Projections.rowCount());
    Long rowCount = (Long) criteria.uniqueResult();
    return rowCount != 0;
}

From source file:au.org.theark.core.dao.AuditDao.java

License:Open Source License

@Override
public String getFieldName(Class<?> cls, String field) {
    Criteria criteria = getSession().createCriteria(AuditField.class);
    criteria.add(Restrictions.eq("fieldName", field));
    criteria.createAlias("auditEntity", "ae");
    criteria.add(Restrictions.eq("ae.classIdentifier", cls.getCanonicalName()));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return ((AuditField) criteria.uniqueResult()).getName();
}