Search in sources :

Example 1 with CustomUserFieldAlreadyExistException

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

the class CustomUserFieldsServiceImpl method create.

@Override
public CustomUserFieldEntity create(CustomUserFieldEntity newFieldEntity) {
    try {
        final String refId = GraviteeContext.getCurrentOrganization();
        final CustomUserFieldReferenceType refType = ORGANIZATION;
        LOGGER.debug("Create custom user field [key={}, refId={}]", newFieldEntity.getKey(), refId);
        Optional<CustomUserField> existingRecord = this.customUserFieldsRepository.findById(formatKeyValue(newFieldEntity.getKey()), refId, refType);
        if (existingRecord.isPresent()) {
            throw new CustomUserFieldAlreadyExistException(newFieldEntity.getKey());
        } else {
            CustomUserField fieldToCreate = map(newFieldEntity);
            fieldToCreate.setReferenceId(refId);
            fieldToCreate.setReferenceType(refType);
            final Date now = new Date();
            fieldToCreate.setCreatedAt(now);
            fieldToCreate.setUpdatedAt(now);
            final CustomUserField recorded = customUserFieldsRepository.create(fieldToCreate);
            createAuditLog(CUSTOM_USER_FIELD_CREATED, now, null, recorded);
            return map(recorded);
        }
    } catch (TechnicalException e) {
        LOGGER.error("An error occurs while trying to create CustomUserField", e);
        throw new TechnicalManagementException("An error occurs while trying to create CustomUserField", e);
    }
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CustomUserFieldReferenceType(io.gravitee.repository.management.model.CustomUserFieldReferenceType) CustomUserFieldAlreadyExistException(io.gravitee.rest.api.service.exceptions.CustomUserFieldAlreadyExistException) Date(java.util.Date) 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 CustomUserFieldAlreadyExistException (io.gravitee.rest.api.service.exceptions.CustomUserFieldAlreadyExistException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Date (java.util.Date)1