Search in sources :

Example 1 with VXPortalUserList

use of org.apache.ranger.view.VXPortalUserList in project ranger by apache.

the class UserMgr method searchUsers.

/**
 * @param searchCriteria
 * @return
 */
public VXPortalUserList searchUsers(SearchCriteria searchCriteria) {
    VXPortalUserList returnList = new VXPortalUserList();
    ArrayList<VXPortalUser> objectList = new ArrayList<VXPortalUser>();
    String queryStr = "SELECT u FROM  XXPortalUser u ";
    String countQueryStr = "SELECT COUNT(u) FROM XXPortalUser u ";
    // Get total count first
    Query query = createUserSearchQuery(countQueryStr, null, searchCriteria);
    Long count = (Long) query.getSingleResult();
    int resultSize = count != null ? count.intValue() : 0;
    if (resultSize == 0) {
        return returnList;
    }
    // Get actual data
    // Add sort by
    String sortBy = searchCriteria.getSortBy();
    String querySortBy = "u.loginId";
    if (sortBy != null && !sortBy.trim().isEmpty()) {
        sortBy = sortBy.trim();
        if (sortBy.equalsIgnoreCase("userId")) {
            querySortBy = "u.id";
        } else if (sortBy.equalsIgnoreCase("loginId")) {
            querySortBy = "ua.loginId";
        } else if (sortBy.equalsIgnoreCase("emailAddress")) {
            querySortBy = "u.emailAddress";
        } else if (sortBy.equalsIgnoreCase("firstName")) {
            querySortBy = "u.firstName";
        } else if (sortBy.equalsIgnoreCase("lastName")) {
            querySortBy = "u.lastName";
        } else {
            sortBy = "loginId";
            logger.error("Invalid sortBy provided. sortBy=" + sortBy);
        }
    } else {
        sortBy = "loginId";
    }
    // Default sort field
    String sortClause = " order by " + querySortBy + " ";
    // Add sort type
    String sortType = searchCriteria.getSortType();
    String querySortType = "asc";
    if (sortType != null) {
        if (sortType.equalsIgnoreCase("asc") || sortType.equalsIgnoreCase("desc")) {
            querySortType = sortType;
        } else {
            logger.error("Invalid sortType. sortType=" + sortType);
        }
    }
    sortClause += querySortType;
    query = createUserSearchQuery(queryStr, sortClause, searchCriteria);
    // Set start index
    query.setFirstResult(searchCriteria.getStartIndex());
    searchUtil.updateQueryPageSize(query, searchCriteria);
    @SuppressWarnings("rawtypes") List resultList = query.getResultList();
    // Iterate over the result list and create the return list
    for (Object object : resultList) {
        XXPortalUser gjUser = (XXPortalUser) object;
        VXPortalUser userProfile = new VXPortalUser();
        gjUserToUserProfile(gjUser, userProfile);
        objectList.add(userProfile);
    }
    returnList.setResultSize(resultSize);
    returnList.setPageSize(query.getMaxResults());
    returnList.setSortBy(sortBy);
    returnList.setSortType(querySortType);
    returnList.setStartIndex(query.getFirstResult());
    returnList.setTotalCount(count.longValue());
    returnList.setVXPortalUsers(objectList);
    return returnList;
}
Also used : XXPortalUser(org.apache.ranger.entity.XXPortalUser) Query(javax.persistence.Query) VXPortalUserList(org.apache.ranger.view.VXPortalUserList) ArrayList(java.util.ArrayList) VXPortalUser(org.apache.ranger.view.VXPortalUser) VXPortalUserList(org.apache.ranger.view.VXPortalUserList) List(java.util.List) ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString)

Example 2 with VXPortalUserList

use of org.apache.ranger.view.VXPortalUserList in project ranger by apache.

the class TestUserMgr method test15searchUsers.

@Test
public void test15searchUsers() {
    Query query = Mockito.mock(Query.class);
    EntityManager entityManager = Mockito.mock(EntityManager.class);
    SearchCriteria searchCriteria = new SearchCriteria();
    searchCriteria.setDistinct(true);
    searchCriteria.setGetChildren(true);
    searchCriteria.setGetCount(true);
    searchCriteria.setMaxRows(12);
    searchCriteria.setOwnerId(userId);
    searchCriteria.setStartIndex(1);
    searchCriteria.setSortBy("userId");
    searchCriteria.setSortType("asc");
    Long count = 1l;
    Mockito.when(daoManager.getEntityManager()).thenReturn(entityManager);
    Mockito.when(entityManager.createQuery(Mockito.anyString())).thenReturn(query);
    Mockito.when(query.getSingleResult()).thenReturn(count);
    VXPortalUserList dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("loginId");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("emailAddress");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("firstName");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("lastName");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("source");
    searchCriteria.setSortType("");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    searchCriteria.setSortBy("");
    searchCriteria.setSortType("desc");
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    VXPortalUser userProfile = userProfile();
    XXPortalUser user = new XXPortalUser();
    user.setId(userProfile.getId());
    user.setLoginId(userProfile.getLoginId());
    user.setEmailAddress(userProfile.getEmailAddress());
    user.setLoginId(userProfile.getLoginId());
    List<XXPortalUser> resultList = new ArrayList<XXPortalUser>();
    resultList.add(user);
    Mockito.when(query.getResultList()).thenReturn(resultList);
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    count = 0l;
    Mockito.when(query.getSingleResult()).thenReturn(count);
    dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
}
Also used : XXPortalUser(org.apache.ranger.entity.XXPortalUser) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) VXPortalUserList(org.apache.ranger.view.VXPortalUserList) ArrayList(java.util.ArrayList) VXPortalUser(org.apache.ranger.view.VXPortalUser) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Example 3 with VXPortalUserList

use of org.apache.ranger.view.VXPortalUserList in project ranger by apache.

the class XPortalUserServiceBase method searchXPortalUsers.

/**
 * @param searchCriteria
 * @return
 */
public VXPortalUserList searchXPortalUsers(SearchCriteria searchCriteria) {
    VXPortalUserList returnList = new VXPortalUserList();
    List<VXPortalUser> xPortalUserList = new ArrayList<VXPortalUser>();
    List<T> resultList = searchResources(searchCriteria, searchFields, sortFields, returnList);
    // Iterate over the result list and create the return list
    for (T gjXPortalUser : resultList) {
        VXPortalUser vXPortalUser = populateViewBean(gjXPortalUser);
        xPortalUserList.add(vXPortalUser);
    }
    returnList.setVXPortalUsers(xPortalUserList);
    return returnList;
}
Also used : VXPortalUserList(org.apache.ranger.view.VXPortalUserList) ArrayList(java.util.ArrayList) VXPortalUser(org.apache.ranger.view.VXPortalUser)

Example 4 with VXPortalUserList

use of org.apache.ranger.view.VXPortalUserList in project ranger by apache.

the class TestUserMgr method test25searchUsers.

@Test
public void test25searchUsers() {
    Query query = Mockito.mock(Query.class);
    EntityManager entityManager = Mockito.mock(EntityManager.class);
    SearchCriteria searchCriteria = new SearchCriteria();
    searchCriteria.setDistinct(true);
    searchCriteria.setGetChildren(true);
    searchCriteria.setGetCount(true);
    searchCriteria.setMaxRows(12);
    searchCriteria.setOwnerId(userId);
    searchCriteria.setStartIndex(1);
    searchCriteria.setSortBy("asc");
    Long count = 1l;
    Mockito.when(daoManager.getEntityManager()).thenReturn(entityManager);
    Mockito.when(entityManager.createQuery(Mockito.anyString())).thenReturn(query);
    Mockito.when(query.getSingleResult()).thenReturn(count);
    VXPortalUserList dbVXPortalUserList = userMgr.searchUsers(searchCriteria);
    Assert.assertNotNull(dbVXPortalUserList);
    Mockito.verify(query).getSingleResult();
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) VXPortalUserList(org.apache.ranger.view.VXPortalUserList) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Example 5 with VXPortalUserList

use of org.apache.ranger.view.VXPortalUserList in project ranger by apache.

the class TestUserREST method test1SearchUsers.

@Test
public void test1SearchUsers() {
    SearchCriteria searchCriteria = new SearchCriteria();
    vXPUserExpList = new VXPortalUserList();
    vXPUserExpList.setPageSize(pageSize);
    List<Integer> status = new ArrayList<Integer>();
    String publicScreenName = "nrp";
    List<String> roles = new ArrayList<String>();
    Mockito.when(searchUtil.extractCommonCriterias(Matchers.eq(request), Matchers.anyListOf(SortField.class))).thenReturn(searchCriteria);
    Mockito.when(searchUtil.extractLong(request, searchCriteria, "userId", "User Id")).thenReturn(userId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "loginId", "Login Id", null)).thenReturn(loginId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "emailAddress", "Email Address", null)).thenReturn(emailId);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "firstName", "First Name", StringUtil.VALIDATION_NAME)).thenReturn(firstName);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "lastName", "Last Name", StringUtil.VALIDATION_NAME)).thenReturn(lastName);
    Mockito.when(searchUtil.extractEnum(request, searchCriteria, "status", "Status", "statusList", RangerConstants.ActivationStatus_MAX)).thenReturn(status);
    Mockito.when(searchUtil.extractString(request, searchCriteria, "publicScreenName", "Public Screen Name", StringUtil.VALIDATION_NAME)).thenReturn(publicScreenName);
    Mockito.when(searchUtil.extractStringList(request, searchCriteria, "role", "Role", "roleList", configUtil.getRoles(), StringUtil.VALIDATION_NAME)).thenReturn(roles);
    Mockito.when(userManager.searchUsers(searchCriteria)).thenReturn(vXPUserExpList);
    VXPortalUserList vXPUserListAct = userREST.searchUsers(request);
    Assert.assertNotNull(vXPUserListAct);
    Assert.assertEquals(vXPUserExpList, vXPUserListAct);
    Assert.assertEquals(vXPUserExpList.getPageSize(), vXPUserListAct.getPageSize());
    Mockito.verify(searchUtil).extractCommonCriterias(Matchers.eq(request), Matchers.anyListOf(SortField.class));
    Mockito.verify(searchUtil).extractLong(request, searchCriteria, "userId", "User Id");
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "loginId", "Login Id", null);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "emailAddress", "Email Address", null);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "firstName", "First Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "lastName", "Last Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractEnum(request, searchCriteria, "status", "Status", "statusList", RangerConstants.ActivationStatus_MAX);
    Mockito.verify(searchUtil).extractString(request, searchCriteria, "publicScreenName", "Public Screen Name", StringUtil.VALIDATION_NAME);
    Mockito.verify(searchUtil).extractStringList(request, searchCriteria, "role", "Role", "roleList", configUtil.getRoles(), StringUtil.VALIDATION_NAME);
    Mockito.verify(userManager).searchUsers(searchCriteria);
}
Also used : VXPortalUserList(org.apache.ranger.view.VXPortalUserList) ArrayList(java.util.ArrayList) SortField(org.apache.ranger.common.SortField) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Aggregations

VXPortalUserList (org.apache.ranger.view.VXPortalUserList)6 ArrayList (java.util.ArrayList)5 VXPortalUser (org.apache.ranger.view.VXPortalUser)4 Query (javax.persistence.Query)3 SearchCriteria (org.apache.ranger.common.SearchCriteria)3 Test (org.junit.Test)3 EntityManager (javax.persistence.EntityManager)2 XXPortalUser (org.apache.ranger.entity.XXPortalUser)2 List (java.util.List)1 SortField (org.apache.ranger.common.SortField)1 VXString (org.apache.ranger.view.VXString)1