use of io.gravitee.repository.management.model.CustomUserField 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);
}
}
use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.
the class CustomUserFieldsServiceImpl method listAllFields.
@Override
public List<CustomUserFieldEntity> listAllFields() {
try {
final String refId = GraviteeContext.getCurrentOrganization();
final CustomUserFieldReferenceType refType = ORGANIZATION;
LOGGER.debug("List all custom user fields [refId={}/refType={}]", refId, refType);
List<CustomUserField> records = this.customUserFieldsRepository.findByReferenceIdAndReferenceType(refId, refType);
return records.stream().map(this::map).collect(Collectors.toList());
} catch (TechnicalException e) {
LOGGER.error("An error occurs while trying to list all CustomUserField", e);
throw new TechnicalManagementException("An error occurs while trying to list all CustomUserField", e);
}
}
use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.
the class CustomUserFieldsServiceImpl method delete.
@Override
public void delete(String key) {
try {
final String refId = GraviteeContext.getCurrentOrganization();
final CustomUserFieldReferenceType refType = ORGANIZATION;
LOGGER.debug("Delete custom user field [key={}, refId={}]", key, refId);
Optional<CustomUserField> existingRecord = this.customUserFieldsRepository.findById(formatKeyValue(key), refId, refType);
if (existingRecord.isPresent()) {
customUserFieldsRepository.delete(formatKeyValue(key), refId, refType);
createAuditLog(CUSTOM_USER_FIELD_DELETED, new Date(), existingRecord.get(), null);
// remove all instance of this field from UserMetadata
this.userMetadataService.deleteAllByCustomFieldId(existingRecord.get().getKey(), existingRecord.get().getReferenceId(), existingRecord.get().getReferenceType());
}
} 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);
}
}
use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.
the class CustomUserFieldsServiceTest method shouldListAllEnvField.
@Test
public void shouldListAllEnvField() throws Exception {
final CustomUserField existingField1 = mock(CustomUserField.class);
when(existingField1.getKey()).thenReturn("key1");
when(existingField1.getFormat()).thenReturn(MetadataFormat.STRING);
final CustomUserField existingField2 = mock(CustomUserField.class);
when(existingField2.getKey()).thenReturn("key2");
when(existingField2.getFormat()).thenReturn(MetadataFormat.STRING);
when(customUserFieldsRepository.findByReferenceIdAndReferenceType(ORG_ID, REF_TYPE)).thenReturn(Arrays.asList(existingField1, existingField2));
List<CustomUserFieldEntity> entities = service.listAllFields();
verify(customUserFieldsRepository).findByReferenceIdAndReferenceType(ORG_ID, REF_TYPE);
assertNotNull("Fields", entities);
Assertions.assertThat(entities.stream().map(CustomUserFieldEntity::getKey).collect(Collectors.toList())).containsExactlyInAnyOrder("key1", "key2");
}
Aggregations