Search in sources :

Example 1 with CustomUserFieldReferenceType

use of io.gravitee.repository.management.model.CustomUserFieldReferenceType 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)

Example 2 with CustomUserFieldReferenceType

use of io.gravitee.repository.management.model.CustomUserFieldReferenceType 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)

Example 3 with CustomUserFieldReferenceType

use of io.gravitee.repository.management.model.CustomUserFieldReferenceType in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceImpl method createAuditLog.

private void createAuditLog(Audit.AuditEvent event, Date createdAt, CustomUserField oldValue, CustomUserField newValue) {
    String key = oldValue != null ? oldValue.getKey() : newValue.getKey();
    CustomUserFieldReferenceType type = oldValue != null ? oldValue.getReferenceType() : newValue.getReferenceType();
    if (type == ORGANIZATION) {
        auditService.createOrganizationAuditLog(Collections.singletonMap(USER_FIELD, key), event, createdAt, oldValue, newValue);
    } else if (type == ENVIRONMENT) {
        auditService.createEnvironmentAuditLog(Collections.singletonMap(USER_FIELD, key), event, createdAt, oldValue, newValue);
    }
}
Also used : CustomUserFieldReferenceType(io.gravitee.repository.management.model.CustomUserFieldReferenceType)

Example 4 with CustomUserFieldReferenceType

use of io.gravitee.repository.management.model.CustomUserFieldReferenceType 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);
    }
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CustomUserFieldReferenceType(io.gravitee.repository.management.model.CustomUserFieldReferenceType) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with CustomUserFieldReferenceType

use of io.gravitee.repository.management.model.CustomUserFieldReferenceType 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);
    }
}
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) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

CustomUserFieldReferenceType (io.gravitee.repository.management.model.CustomUserFieldReferenceType)6 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 CustomUserField (io.gravitee.repository.management.model.CustomUserField)4 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)4 Date (java.util.Date)3 Page (io.gravitee.common.data.domain.Page)1 Metadata (io.gravitee.repository.management.model.Metadata)1 User (io.gravitee.repository.management.model.User)1 CustomUserFieldAlreadyExistException (io.gravitee.rest.api.service.exceptions.CustomUserFieldAlreadyExistException)1 CustomUserFieldNotFoundException (io.gravitee.rest.api.service.exceptions.CustomUserFieldNotFoundException)1 Test (org.junit.Test)1