Search in sources :

Example 1 with CustomUserFieldNotFoundException

use of io.gravitee.rest.api.service.exceptions.CustomUserFieldNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceImpl method update.

@Override
public CustomUserFieldEntity update(CustomUserFieldEntity updateFieldEntity) {
    try {
        final String refId = GraviteeContext.getCurrentOrganization();
        final CustomUserFieldReferenceType refType = ORGANIZATION;
        LOGGER.debug("Update custom user field [key={}, refId={}]", updateFieldEntity.getKey(), refId);
        Optional<CustomUserField> existingRecord = this.customUserFieldsRepository.findById(formatKeyValue(updateFieldEntity.getKey()), refId, refType);
        if (existingRecord.isPresent()) {
            CustomUserField fieldToUpdate = map(updateFieldEntity);
            fieldToUpdate.setKey(existingRecord.get().getKey());
            fieldToUpdate.setReferenceId(existingRecord.get().getReferenceId());
            fieldToUpdate.setReferenceType(existingRecord.get().getReferenceType());
            fieldToUpdate.setCreatedAt(existingRecord.get().getCreatedAt());
            final Date updatedAt = new Date();
            fieldToUpdate.setUpdatedAt(updatedAt);
            final CustomUserField updatedField = customUserFieldsRepository.update(fieldToUpdate);
            createAuditLog(CUSTOM_USER_FIELD_UPDATED, updatedAt, existingRecord.get(), updatedField);
            return map(updatedField);
        } else {
            throw new CustomUserFieldNotFoundException(updateFieldEntity.getKey());
        }
    } catch (TechnicalException e) {
        LOGGER.error("An error occurs while trying to update CustomUserField", e);
        throw new TechnicalManagementException("An error occurs while trying to update CustomUserField", e);
    }
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CustomUserFieldReferenceType(io.gravitee.repository.management.model.CustomUserFieldReferenceType) Date(java.util.Date) CustomUserFieldNotFoundException(io.gravitee.rest.api.service.exceptions.CustomUserFieldNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 CustomUserField (io.gravitee.repository.management.model.CustomUserField)1 CustomUserFieldReferenceType (io.gravitee.repository.management.model.CustomUserFieldReferenceType)1 CustomUserFieldNotFoundException (io.gravitee.rest.api.service.exceptions.CustomUserFieldNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Date (java.util.Date)1