Search in sources :

Example 1 with MetadataNotFoundException

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);
    }
}
Also used : MetadataNotFoundException(io.gravitee.rest.api.service.exceptions.MetadataNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 User (io.gravitee.repository.management.model.User)1 MetadataNotFoundException (io.gravitee.rest.api.service.exceptions.MetadataNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1