Search in sources :

Example 1 with TagEntity

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

the class FlowServiceImpl method getPlatformFlowSchemaForm.

@Override
public String getPlatformFlowSchemaForm() {
    LOGGER.debug("Get platform schema form");
    String fileContent = getFileContent("/flow/platform-flow-schema-form.json");
    List<TagEntity> tags = tagService.findByReference(GraviteeContext.getCurrentOrganization(), TagReferenceType.ORGANIZATION);
    if (tags.size() > 0) {
        LOGGER.debug("Append {} tag(s) to platform schema form", tags.size());
        try {
            final ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonSchema = mapper.readTree(fileContent);
            final ObjectNode consumers = (ObjectNode) jsonSchema.get("properties").get("consumers");
            final ArrayNode enumNode = (ArrayNode) consumers.get("items").get("enum");
            Map<String, String> titleMap = new HashMap<>();
            tags.forEach(tagEntity -> titleMap.put(tagEntity.getId(), tagEntity.getName()));
            titleMap.keySet().forEach(enumNode::add);
            JsonNode titleMapNode = mapper.valueToTree(titleMap);
            ObjectNode xSchemaForm = mapper.createObjectNode();
            consumers.set("x-schema-form", xSchemaForm);
            xSchemaForm.set("titleMap", titleMapNode);
            return jsonSchema.toPrettyString();
        } catch (JsonProcessingException ex) {
            final String error = "An error occurs while append tags to platform flow schema form";
            LOGGER.error(error, ex);
            throw new TechnicalManagementException(error, ex);
        }
    }
    return fileContent;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TagEntity(io.gravitee.rest.api.model.TagEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with TagEntity

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

the class TagServiceImpl method update.

@Override
public List<TagEntity> update(final List<UpdateTagEntity> tagEntities, String referenceId, TagReferenceType referenceType) {
    final List<TagEntity> savedTags = new ArrayList<>(tagEntities.size());
    tagEntities.forEach(tagEntity -> {
        try {
            Tag tag = convert(tagEntity);
            Optional<Tag> tagOptional = tagRepository.findByIdAndReference(tag.getId(), referenceId, repoTagReferenceType(referenceType));
            if (tagOptional.isPresent()) {
                Tag existingTag = tagOptional.get();
                tag.setReferenceId(existingTag.getReferenceId());
                tag.setReferenceType(existingTag.getReferenceType());
                savedTags.add(convert(tagRepository.update(tag)));
                auditService.createOrganizationAuditLog(Collections.singletonMap(TAG, tag.getId()), TAG_UPDATED, new Date(), tagOptional.get(), tag);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update tag {}", tagEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update tag " + tagEntity.getName(), ex);
        }
    });
    return savedTags;
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) TagEntity(io.gravitee.rest.api.model.TagEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with TagEntity

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

the class TagServiceImpl method convert.

private TagEntity convert(final Tag tag) {
    final TagEntity tagEntity = new TagEntity();
    tagEntity.setId(tag.getId());
    tagEntity.setName(tag.getName());
    tagEntity.setDescription(tag.getDescription());
    tagEntity.setRestrictedGroups(tag.getRestrictedGroups());
    return tagEntity;
}
Also used : NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) TagEntity(io.gravitee.rest.api.model.TagEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity)

Example 4 with TagEntity

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

the class TagServiceImpl method create.

@Override
public List<TagEntity> create(final List<NewTagEntity> tagEntities, String referenceId, TagReferenceType referenceType) {
    // First we prevent the duplicate tag name
    final List<String> tagNames = tagEntities.stream().map(NewTagEntity::getName).collect(toList());
    final Optional<TagEntity> optionalTag = findByReference(referenceId, referenceType).stream().filter(tag -> tagNames.contains(tag.getName())).findAny();
    if (optionalTag.isPresent()) {
        throw new DuplicateTagNameException(optionalTag.get().getName());
    }
    final List<TagEntity> savedTags = new ArrayList<>(tagEntities.size());
    tagEntities.forEach(tagEntity -> {
        try {
            Tag tag = convert(tagEntity, referenceId, referenceType);
            savedTags.add(convert(tagRepository.create(tag)));
            auditService.createOrganizationAuditLog(Collections.singletonMap(TAG, tag.getId()), TAG_CREATED, new Date(), null, tag);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create tag {}", tagEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create tag " + tagEntity.getName(), ex);
        }
    });
    return savedTags;
}
Also used : TagReferenceType(io.gravitee.rest.api.model.TagReferenceType) java.util(java.util) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) DuplicateTagNameException(io.gravitee.rest.api.service.exceptions.DuplicateTagNameException) Autowired(org.springframework.beans.factory.annotation.Autowired) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) ApiService(io.gravitee.rest.api.service.ApiService) Collections.singletonList(java.util.Collections.singletonList) TagRepository(io.gravitee.repository.management.api.TagRepository) IdGenerator(io.gravitee.common.utils.IdGenerator) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) TagNotFoundException(io.gravitee.rest.api.service.exceptions.TagNotFoundException) TAG(io.gravitee.repository.management.model.Audit.AuditProperties.TAG) Collectors.toSet(java.util.stream.Collectors.toSet) TagEntity(io.gravitee.rest.api.model.TagEntity) GroupService(io.gravitee.rest.api.service.GroupService) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) AuditService(io.gravitee.rest.api.service.AuditService) AuditEvent(io.gravitee.repository.management.model.Tag.AuditEvent) Collectors.toList(java.util.stream.Collectors.toList) Tag(io.gravitee.repository.management.model.Tag) Component(org.springframework.stereotype.Component) GroupEntity(io.gravitee.rest.api.model.GroupEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) TagService(io.gravitee.rest.api.service.TagService) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NewTagEntity(io.gravitee.rest.api.model.NewTagEntity) TagEntity(io.gravitee.rest.api.model.TagEntity) UpdateTagEntity(io.gravitee.rest.api.model.UpdateTagEntity) DuplicateTagNameException(io.gravitee.rest.api.service.exceptions.DuplicateTagNameException) Tag(io.gravitee.repository.management.model.Tag) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TagEntity (io.gravitee.rest.api.model.TagEntity)4 NewTagEntity (io.gravitee.rest.api.model.NewTagEntity)3 UpdateTagEntity (io.gravitee.rest.api.model.UpdateTagEntity)3 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Tag (io.gravitee.repository.management.model.Tag)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IdGenerator (io.gravitee.common.utils.IdGenerator)1 TagRepository (io.gravitee.repository.management.api.TagRepository)1 TAG (io.gravitee.repository.management.model.Audit.AuditProperties.TAG)1 AuditEvent (io.gravitee.repository.management.model.Tag.AuditEvent)1 GroupEntity (io.gravitee.rest.api.model.GroupEntity)1 TagReferenceType (io.gravitee.rest.api.model.TagReferenceType)1 ApiService (io.gravitee.rest.api.service.ApiService)1 AuditService (io.gravitee.rest.api.service.AuditService)1 GroupService (io.gravitee.rest.api.service.GroupService)1