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

io.lsn.spring.system.mapper.SystemMapperService Maven / Gradle / Ivy

The newest version!
package io.lsn.spring.system.mapper;

import io.lsn.logger.factory.LoggerFactory;
import io.lsn.logger.factory.logger.Logger;
import io.lsn.spring.system.mapper.domain.SystemMapperChangeEvent;
import io.lsn.spring.system.mapper.domain.SystemMapperDaoInterface;
import io.lsn.spring.system.mapper.domain.SystemMapperItem;
import io.lsn.spring.system.mapper.exception.SystemMapperException;
import io.lsn.spring.utilities.json.mapper.JsonMapper;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemMapperService {

    private final Logger logger = LoggerFactory.getLogger(SystemMapperService.class);

    private SystemMapperDaoInterface dao;

    private RTopic systemMapperChangeEventRTopic;

    private List systemMappingList;

    @Autowired
    public SystemMapperService(SystemMapperDaoInterface dao, RedissonClient redissonClient) {
        this.dao = dao;
        if (redissonClient != null) {
            systemMapperChangeEventRTopic = redissonClient.getTopic("io.lsn.lib.system.mapper");
            initListeners();
        }
        this.load();
        SystemMapperRegistry.setService(this);
    }


    /**
     * init listeners
     */
    private void initListeners() {
        systemMapperChangeEventRTopic.addListener(SystemMapperChangeEvent.class, (s, systemMapperChangeEvent) -> {
            logger.info("reload system mapping definition");
            this.load();
        });
    }

    /**
     * load mappings
     */
    private void load() {
        String mappingsJson = dao.getList();
        this.systemMappingList = new ArrayList<>();
        try {
            this.systemMappingList = JsonMapper.getInstance().mapListFromJsonSnakeCase(mappingsJson, SystemMapperItem[].class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


    /**
     * get system mappings
     *
     * @return
     */
    public List getSystemMappingList() {
        return systemMappingList;
    }


    /**
     * Returns definition of system-to-system mapping
     *
     * @param sourceSystem      source system name
     * @param sourceDictionary  source dictionary
     * @param sourceCode        source code
     * @param destinationSystem destination system name
     * @return
     * @throws SystemMapperException
     */
    public SystemMapperItem getSystemMapping(String sourceSystem,
                                             String sourceDictionary,
                                             String sourceCode,
                                             String destinationSystem) throws SystemMapperException {
        SystemMapperItem result = this.getSystemMappingList().stream()
                .filter(e -> e.getSourceSystem().equalsIgnoreCase(sourceSystem)
                        && e.getSourceDictionary().equalsIgnoreCase(sourceDictionary)
                        && e.getSourceCode().equalsIgnoreCase(sourceCode)
                        && e.getDestinationSystem().equalsIgnoreCase(destinationSystem)
                )
                .findFirst()
                .orElse(null);

        if (result == null) {
            // if not found - throw an exception
            String sourceIndex = sourceSystem + "." + sourceDictionary + "." + sourceCode;
            String msg = "Cant find proper mapping for index: ".concat(sourceIndex).concat(" to system ").concat(destinationSystem);
            throw new SystemMapperException(msg);
        }

        return result;
    }


    /**
     * return translated (mapped) destination code or null if not exists
     *
     * @param sourceSystem      source system name
     * @param sourceDictionary  source system dictionary code
     * @param sourceCode        source dictionary element code
     * @param destinationSystem destination system element code
     * @return
     */
    public String getSystemMappingCodeSafe(String sourceSystem,
                                           String sourceDictionary,
                                           String sourceCode,
                                           String destinationSystem) {
        try {
            return getSystemMapping(sourceSystem, sourceDictionary, sourceCode, destinationSystem).getDestinationCode();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * event notification for dictionary change
     */
    private void notifyMappingChange() {
        if (systemMapperChangeEventRTopic != null) {
            systemMapperChangeEventRTopic.publish(new SystemMapperChangeEvent());
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy