Search in sources :

Example 1 with AuditEntity

use of io.gravitee.management.model.audit.AuditEntity in project gravitee-management-rest-api by gravitee-io.

the class AuditServiceImpl method convert.

private AuditEntity convert(Audit audit) {
    AuditEntity auditEntity = new AuditEntity();
    auditEntity.setReferenceType(audit.getReferenceType().name());
    auditEntity.setReferenceId(audit.getReferenceId());
    auditEntity.setEvent(audit.getEvent());
    auditEntity.setProperties(audit.getProperties());
    auditEntity.setUser(audit.getUser());
    auditEntity.setId(audit.getId());
    auditEntity.setPatch(audit.getPatch());
    auditEntity.setCreatedAt(audit.getCreatedAt());
    return auditEntity;
}
Also used : AuditEntity(io.gravitee.management.model.audit.AuditEntity)

Example 2 with AuditEntity

use of io.gravitee.management.model.audit.AuditEntity in project gravitee-management-rest-api by gravitee-io.

the class AuditServiceImpl method search.

@Override
public MetadataPage<AuditEntity> search(AuditQuery query) {
    Audit.AuditReferenceType referenceType = query.isManagementLogsOnly() ? Audit.AuditReferenceType.PORTAL : (query.getApiIds() != null && !query.getApiIds().isEmpty()) ? Audit.AuditReferenceType.API : (query.getApplicationIds() != null && !query.getApplicationIds().isEmpty()) ? Audit.AuditReferenceType.APPLICATION : null;
    Builder criteria = new Builder().from(query.getFrom()).to(query.getTo());
    if (referenceType != null) {
        List<String> referenceIds;
        switch(referenceType) {
            case API:
                referenceIds = query.getApiIds();
                break;
            case APPLICATION:
                referenceIds = query.getApplicationIds();
                break;
            default:
                referenceIds = Collections.singletonList("DEFAULT");
        }
        criteria.references(referenceType, referenceIds);
    }
    if (query.getEvents() != null && !query.getEvents().isEmpty()) {
        criteria.events(query.getEvents());
    }
    Page<Audit> auditPage = auditRepository.search(criteria.build(), new PageableBuilder().pageNumber(query.getPage() - 1).pageSize(query.getSize()).build());
    List<AuditEntity> content = auditPage.getContent().stream().map(this::convert).collect(Collectors.toList());
    return new MetadataPage<>(content, query.getPage(), query.getSize(), auditPage.getTotalElements(), getMetadata(content));
}
Also used : AuditEntity(io.gravitee.management.model.audit.AuditEntity) MetadataPage(io.gravitee.common.data.domain.MetadataPage) Builder(io.gravitee.repository.management.api.search.AuditCriteria.Builder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder)

Example 3 with AuditEntity

use of io.gravitee.management.model.audit.AuditEntity in project gravitee-management-rest-api by gravitee-io.

the class AuditServiceImpl method getMetadata.

private Map<String, String> getMetadata(List<AuditEntity> content) {
    Map<String, String> metadata = new HashMap<>();
    for (AuditEntity auditEntity : content) {
        if (auditEntity.getProperties() != null) {
            for (Map.Entry<String, String> property : auditEntity.getProperties().entrySet()) {
                String metadataKey = new StringJoiner(":").add(property.getKey()).add(property.getValue()).add("name").toString();
                if (!metadata.containsKey(metadataKey)) {
                    String name = property.getValue();
                    try {
                        switch(Audit.AuditProperties.valueOf(property.getKey())) {
                            case PAGE:
                                Optional<io.gravitee.repository.management.model.Page> optPage = pageRepository.findById(property.getValue());
                                if (optPage.isPresent()) {
                                    name = optPage.get().getName();
                                }
                                break;
                            case PLAN:
                                Optional<Plan> optPlan = planRepository.findById(property.getValue());
                                if (optPlan.isPresent()) {
                                    name = optPlan.get().getName();
                                }
                                break;
                            case METADATA:
                                MetadataReferenceType refType = (Audit.AuditReferenceType.API.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.API : (Audit.AuditReferenceType.APPLICATION.name().equals(auditEntity.getReferenceType())) ? MetadataReferenceType.APPLICATION : MetadataReferenceType.DEFAULT;
                                String refId = refType.equals(MetadataReferenceType.DEFAULT) ? getDefautReferenceId() : auditEntity.getReferenceId();
                                Optional<Metadata> optMetadata = metadataRepository.findById(property.getValue(), refId, refType);
                                if (optMetadata.isPresent()) {
                                    name = optMetadata.get().getName();
                                }
                                break;
                            case GROUP:
                                Optional<Group> optGroup = groupRepository.findById(property.getValue());
                                if (optGroup.isPresent()) {
                                    name = optGroup.get().getName();
                                }
                                break;
                            case USER:
                                Optional<User> optUser = userRepository.findById(property.getValue());
                                if (optUser.isPresent()) {
                                    if (optUser.get().getFirstname() != null && optUser.get().getLastname() != null) {
                                        name = optUser.get().getFirstname() + " " + optUser.get().getLastname();
                                    } else {
                                        name = optUser.get().getUsername();
                                    }
                                }
                            default:
                                break;
                        }
                    } catch (TechnicalException e) {
                        LOGGER.error("Error finding metadata {}", metadataKey);
                        name = property.getValue();
                    }
                    metadata.put(metadataKey, name);
                }
            }
        }
    }
    return metadata;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Page(io.gravitee.common.data.domain.Page) MetadataPage(io.gravitee.common.data.domain.MetadataPage) AuditEntity(io.gravitee.management.model.audit.AuditEntity)

Aggregations

AuditEntity (io.gravitee.management.model.audit.AuditEntity)3 MetadataPage (io.gravitee.common.data.domain.MetadataPage)2 Page (io.gravitee.common.data.domain.Page)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Builder (io.gravitee.repository.management.api.search.AuditCriteria.Builder)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1