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

com.fivefaces.structureclient.config.SetupEssentialWorkflows Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
package com.fivefaces.structureclient.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fivefaces.cloud.workflow.awsonprem.EssentialWorkflowsDescriptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
@Slf4j
public class SetupEssentialWorkflows implements EssentialWorkflowsDescriptor {

    private final ObjectMapper objectMapper;

    @Value("classpath:essentialworkflows")
    private Resource stepFunctionsFolder;

    @Override
    public List> essentialWorkflows() {
        try (Stream paths = Files.walk(Paths.get(stepFunctionsFolder.getURI()))) {
            Set workflowPaths = paths.filter(Files::isRegularFile)
                    .filter(file -> FilenameUtils.getExtension(file.getFileName().toString()).equalsIgnoreCase("json")).collect(Collectors.toSet());
            List> readWorkflows = new ArrayList<>(workflowPaths.size());
            for (Path workflowPath : workflowPaths) {
                readWorkflows.add(readMap(Files.readString(workflowPath)));
            }
            return readWorkflows;
        } catch (Exception e) {
            log.error("Could not create core workflows", e);
            return List.of();
        }
    }

    private Map readMap(String json) throws JsonProcessingException {
        if (StringUtils.isBlank(json)) {
            return null;
        }
        return objectMapper.readValue(json, new TypeReference<>() {
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy