use of io.gravitee.rest.api.service.exceptions.MetadataNotFoundException 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);
}
}
Aggregations