All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.miragon.miranum.connect.json.registry.application.service.SchemaService Maven / Gradle / Ivy

There is a newer version: 0.7.3
Show newest version
package io.miragon.miranum.connect.json.registry.application.service;

import io.miragon.miranum.connect.json.registry.application.ports.in.ReadSchemaUseCase;
import io.miragon.miranum.connect.json.registry.application.ports.in.SaveSchemaInCommand;
import io.miragon.miranum.connect.json.registry.application.ports.in.SaveSchemaUseCase;
import io.miragon.miranum.connect.json.registry.application.ports.out.LoadSchemaPort;
import io.miragon.miranum.connect.json.registry.application.ports.out.SaveSchemaPort;
import io.miragon.miranum.connect.json.registry.domain.Schema;
import io.miragon.miranum.connect.shared.ObjectNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SchemaService implements SaveSchemaUseCase, ReadSchemaUseCase {

    private final SaveSchemaPort saveSchemaPort;
    private final LoadSchemaPort loadSchemaPort;

    @Override
    @Transactional
    public List saveSchema(SaveSchemaInCommand saveSchemaInCommand) {
        final List existingSchemas = this.loadSchemaPort
                .loadAllSchemaByRefAndBundle(saveSchemaInCommand.getBundle(), saveSchemaInCommand.getRef());

        List schemasToCreate = saveSchemaInCommand.getTags().stream()
                .map(tag -> mapToSchema(existingSchemas, saveSchemaInCommand, tag.trim().toLowerCase()))
                .toList();

        return this.saveSchemaPort.saveAllSchemas(schemasToCreate);
    }

    public Schema loadSchema(String bundle, String ref, String tag) {
        return this.loadSchemaPort.loadTaggedSchema(bundle, ref, tag)
                .orElseThrow(() -> new ObjectNotFoundException(
                        String.format("No schema found for bundle %s, ref %s and tag %s", bundle, ref, tag)));
    }

    /**
     * Overwrites existing schema if tag already exist, otherwise creates a new schema.
     */
    private Schema mapToSchema(List existingSchemasByRefAndBundle, SaveSchemaInCommand saveSchemaInCommand, String tag) {
        return existingSchemasByRefAndBundle.stream()
                .filter(schema -> schema.getTag().equals(tag))
                .findFirst()
                .map(existing -> new Schema(existing, saveSchemaInCommand.getJsonNode()))
                .orElse(new Schema(saveSchemaInCommand.getBundle(), saveSchemaInCommand.getRef(), tag, saveSchemaInCommand.getJsonNode()));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy