Example usage for org.hibernate Criteria add

List of usage examples for org.hibernate Criteria add

Introduction

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

Prototype

public Criteria add(Criterion criterion);

Source Link

Document

Add a Criterion restriction to constrain the results to be retrieved.

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  www  . j  a  v a  2s  . 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

/**
 * Use this method when we want to load the Collection of Administrator roles as a Collectio<String>. The method looks up Ark Super Administrator
 * and Administrator roles given a LdapUserName. It populates it into a Collection<String> that represent a unique set of administration roles for
 * this user. It does not take into account the Module or Study. This is usually when the user has logged in first and we want to know if the user
 * has a role of type Administator so he can have access to Create function.
 * /*from w  ww.  j  a  v a2s  . c  o m*/
 * @param ldapUserName
 * @return Collection<String>
 * @throws EntityNotFoundException
 */
@SuppressWarnings("unchecked")
public Collection<String> getUserAdminRoles(String ldapUserName) throws EntityNotFoundException {

    ArkUser arkUser = getArkUser(ldapUserName);

    StatelessSession session = getStatelessSession();
    Criteria criteria = session.createCriteria(ArkUserRole.class);// getSession().createCriteria(ArkUserRole.class);
    ArkRole arkRoleSuperAdmin = getArkRoleByName(RoleConstants.ARK_ROLE_SUPER_ADMINISTATOR);
    ArkRole arkRoleAdmin = getArkRoleByName(RoleConstants.ARK_ROLE_ADMINISTATOR);
    criteria.add(Restrictions.or(Restrictions.eq("arkRole", arkRoleSuperAdmin),
            Restrictions.eq("arkRole", arkRoleAdmin)));
    criteria.add(Restrictions.eq("arkUser", arkUser));
    List<ArkUserRole> arkUserRoleList = (List<ArkUserRole>) criteria.list();
    Set<String> roles = new HashSet<String>(0);
    for (ArkUserRole arkUserRole : arkUserRoleList) {
        String roleName = arkUserRole.getArkRole().getName();
        roles.add(roleName);
    }

    Collection<String> userRoles = new ArrayList<String>();
    for (String roleName : roles) {
        userRoles.add(roleName);
    }
    session.close();
    return userRoles;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<ArkUserRole> getArkUserAdminRoles(String ldapUserName) throws EntityNotFoundException {
    ArkUser arkUser = getArkUser(ldapUserName);
    StatelessSession session = getStatelessSession();
    Criteria criteria = session.createCriteria(ArkUserRole.class);// getSession().createCriteria(ArkUserRole.class);
    ArkRole arkRoleStudyAdmin = getArkRoleByName(RoleConstants.ARK_ROLE_STUDY_ADMINISTATOR);
    criteria.add(Restrictions.eq("arkRole", arkRoleStudyAdmin));
    criteria.add(Restrictions.eq("arkUser", arkUser));
    List<ArkUserRole> arkUserRoleList = (List<ArkUserRole>) criteria.list();
    session.close();/*from   w ww . jav a 2 s  .  c  o  m*/
    return arkUserRoleList;
}

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);//from  w  ww . j  a  v a  2  s.co  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  va2s .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 av a  2 s  .co  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);//  ww w  .  j  a v  a2s  . c  om
    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 va2s .c  o m
    ArkModule arkModule = (ArkModule) criteria.uniqueResult();
    return arkModule;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public Collection<String> getArkRolePermission(ArkFunction arkFunction, String userRole, ArkModule arkModule)
        throws EntityNotFoundException {

    Collection<String> stringPermissions = new ArrayList<String>();
    ArkRole arkRole = getArkRoleByName(userRole);
    Criteria criteria = getSession().createCriteria(ArkRolePolicyTemplate.class);

    if (arkModule != null) {
        criteria.add(Restrictions.eq("arkModule", arkModule));
    }/*from  w  w  w . j a va  2s.co m*/

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

    if (arkRole != null) {
        criteria.add(Restrictions.eq("arkRole", arkRole));

        criteria.createAlias("arkPermission", "permission");
        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.groupProperty("permission.name"));
        criteria.setProjection(projectionList);

        stringPermissions = criteria.list();
    }
    return stringPermissions;
}

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

License:Open Source License

/**
 * Get a list of Modules that are linked to the study and then get the roles linked to each module A VO List of ArkModuleVO will contain the
 * ArkModule and a list of ArkRoles. Note:This implementation will exclude Reporting Module from the list specifically.
 * /*  w  ww . j a v  a2 s . com*/
 * @param study
 * @return Collection<ArkModuleVO> Minus the Reporting Module
 */
@SuppressWarnings("unchecked")
public Collection<ArkModuleVO> getArkModulesAndRolesLinkedToStudy(Study study) {

    ArkModule arkModuleToExclude = getArkModuleByName(Constants.ARK_MODULE_REPORTING);

    Collection<LinkStudyArkModule> arkStudyLinkedModuleList = new ArrayList<LinkStudyArkModule>();
    Collection<ArkModuleVO> arkModuleVOList = new ArrayList<ArkModuleVO>();

    Criteria criteria = getSession().createCriteria(LinkStudyArkModule.class);

    criteria.add(Restrictions.eq("study", study));
    //criteria.add(Restrictions.ne("arkModule", arkModuleToExclude));
    criteria.createAlias("arkModule", "module");
    criteria.addOrder(Order.asc("module.id"));

    arkStudyLinkedModuleList = criteria.list();

    // For each one in the List get the associated Roles i.e for each module get the Roles
    for (LinkStudyArkModule linkStudyArkModule : arkStudyLinkedModuleList) {
        // Here is a Module linked to a study get the Roles linked to this module
        ArkModuleVO arkModuleVO = new ArkModuleVO();
        arkModuleVO.setArkModule(linkStudyArkModule.getArkModule());
        arkModuleVO.setArkModuleRoles(getArkRoleLinkedToModule(linkStudyArkModule.getArkModule()));
        arkModuleVOList.add(arkModuleVO);
    }

    // getArkUserRoleList(study,null);
    return arkModuleVOList;
}