Last active
December 16, 2022 10:10
-
-
Save neilherbertuk/db9f9fd40ca65621999e57c01e472180 to your computer and use it in GitHub Desktop.
Finding large number of users with pagination witin OpenIAM with a groovy script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List | |
import org.apache.commons.logging.Log | |
import org.apache.commons.logging.LogFactory | |
import org.openiam.base.request.BaseSearchServiceRequest | |
import org.openiam.base.request.BaseServiceRequest | |
import org.openiam.base.response.data.IntResponse | |
import org.openiam.base.response.data.BaseDataResponse | |
import org.openiam.base.ws.MatchType | |
import org.openiam.base.ws.SearchParam | |
import org.openiam.common.beans.mq.RabbitMQSender | |
import org.openiam.common.beans.mq.UserRabbitMQService | |
import org.openiam.idm.searchbeans.UserSearchBean | |
import org.openiam.idm.srvc.user.dto.User | |
import org.openiam.idm.srvc.user.dto.UserCollection | |
import org.openiam.idm.srvc.user.dto.UserStatusEnum | |
import org.openiam.mq.constants.api.OpenIAMAPI | |
import org.openiam.mq.constants.api.user.UserServiceAPI | |
import org.openiam.mq.constants.queue.user.UserServiceQueue | |
import org.springframework.context.ApplicationContext | |
// Dependencies | |
Log log = LogFactory.getLog("findUserTest") // Logging | |
ApplicationContext appContext = (ApplicationContext) context // IoC Container | |
UserRabbitMQService userRabbitMQService = appContext.getBean(UserRabbitMQService.class) // Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/common/beans/mq/UserRabbitMQService.html | |
RabbitMQSender rabbitMQSender = appContext.getBean(RabbitMQSender.class) // Underlying RabbitMQSender | |
UserServiceQueue queue = appContext.getBean(UserServiceQueue.class) // Queue to use | |
log.info("Finding All Users") | |
UserSearchBean usb = new UserSearchBean() // Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/idm/searchbeans/UserSearchBean.html | |
// Build a list of attributes you want returned for the user - Docs - https://download.openiam.com/release/enterprise/4.2.1.2/javadoc/org/openiam/idm/srvc/user/dto/UserCollection.html | |
List<UserCollection> userCollection = [UserCollection.PRINCIPALS, UserCollection.ATTRIBUTES, UserCollection.ORGANIZATIONS, UserCollection.EMAILS, UserCollection.SUPERVISORS] | |
log.info("Getting total results from RabbitMQSender") | |
Integer totalCount = 0 | |
BaseDataResponse response = (BaseDataResponse) rabbitMQSender.sendAndReceive((UserServiceQueue) queue, (OpenIAMAPI) UserServiceAPI.Count, new BaseSearchServiceRequest(usb), IntResponse.class) | |
if (response.isFailure()) { | |
log.info(String.format("Can't getValue: code %s, errorText %s", response.getErrorCode().name(), response.getErrorText())); | |
} else { | |
totalCount = (Integer) response.getValue() | |
} | |
log.info("There are ${totalCount} results") | |
int total, from = 0 | |
int size = 5 // Number of results to get each time | |
List<User> users = new ArrayList<>() // Empty list to hold users | |
while (total < totalCount) { | |
List<User> results = userRabbitMQService.findBeans(usb, userCollection as UserCollection[], from, size) // Get page | |
if(results.size() > 0) { | |
total += results.size() | |
from += size | |
users.addAll(results) // Add results to users array | |
log.info("${total}/${totalCount} users retrieved") | |
} else { | |
log.info("Done") | |
break | |
} | |
} | |
log.info("There are ${users.size()} users") | |
for (User user : users) { | |
log.info(user.getId()) | |
} | |
output = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment