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);
}
}
Aggregations