use of io.gravitee.repository.management.api.search.UserCriteria in project gravitee-management-rest-api by gravitee-io.
the class UserMetadataServiceImpl method deleteAllByCustomFieldId.
@Override
public void deleteAllByCustomFieldId(String key, String refId, CustomUserFieldReferenceType refType) {
try {
// CustomField is linked to an Org
// we have to retrieve users based on org and then
// delete the user metadata identifier by the field key and the userId
String orgId = null;
if (refType.equals(CustomUserFieldReferenceType.ENVIRONMENT)) {
final EnvironmentEntity cufEnvironment = this.environmentService.findById(refId);
orgId = cufEnvironment.getOrganizationId();
} else {
orgId = refId;
}
final UserCriteria criteria = new UserCriteria.Builder().organizationId(orgId).build();
int pageNumber = 0;
Page<User> pageOfUser = null;
do {
pageOfUser = this.userRepository.search(criteria, new PageableBuilder().pageNumber(pageNumber).pageSize(100).build());
for (User user : pageOfUser.getContent()) {
try {
this.delete(CustomFieldSanitizer.formatKeyValue(key), USER, user.getId());
} catch (MetadataNotFoundException e) {
LOGGER.debug("Metadata key={}, refType={}, refId={} not found," + " ignore error because we want to delete it and user may not have this metadata", key, USER, user.getId());
}
}
pageNumber++;
} while (pageOfUser.getPageElements() > 0);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to all metadata with key {}", key, ex);
throw new TechnicalManagementException("An error occurred while trying to all metadata with key " + key, ex);
}
}
use of io.gravitee.repository.management.api.search.UserCriteria in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method search.
@Override
public Page<UserEntity> search(UserCriteria criteria, Pageable pageable) {
try {
LOGGER.debug("search users");
UserCriteria.Builder builder = new UserCriteria.Builder().organizationId(GraviteeContext.getCurrentOrganization()).statuses(criteria.getStatuses());
if (criteria.hasNoStatus()) {
builder.noStatus();
}
UserCriteria newCriteria = builder.build();
Page<User> users = userRepository.search(newCriteria, new PageableBuilder().pageNumber(pageable.getPageNumber() - 1).pageSize(pageable.getPageSize()).build());
List<UserEntity> entities = users.getContent().stream().map(u -> convert(u, false)).collect(toList());
populateUserFlags(entities);
return new Page<>(entities, users.getPageNumber() + 1, (int) users.getPageElements(), users.getTotalElements());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to search users", ex);
throw new TechnicalManagementException("An error occurs while trying to search users", ex);
}
}
Aggregations