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

org.ikasan.mapping.service.MappingServiceImpl Maven / Gradle / Ivy

There is a newer version: 3.3.5
Show newest version
package org.ikasan.mapping.service;

import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import org.ikasan.mapping.dao.MappingConfigurationDao;
import org.ikasan.mapping.service.configuration.MappingConfigurationServiceConfiguration;
import org.ikasan.mapping.util.SetProducer;
import org.ikasan.spec.mapping.MappingService;
import org.ikasan.spec.mapping.NamedResult;
import org.ikasan.spec.mapping.QueryParameter;

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

/**
 * Created by Ikasan Development Team on 16/05/2017.
 */
public class MappingServiceImpl implements MappingService
{
    private static Logger logger = LoggerFactory.getLogger(MappingServiceImpl.class);

    protected final MappingConfigurationDao dao;
    protected MappingConfigurationServiceConfiguration configuration;

    /**
     * Constructor
     *
     * @param dao
     */
    public MappingServiceImpl(final MappingConfigurationDao dao)
    {
        this.dao = dao;
        if (this.dao == null)
        {
            throw new IllegalArgumentException("The MappingConfigurationDao cannot be null.");
        }
    }

    /*
     * (non-Javadoc)
     * @see org.ikasan.mapping.service.MappingConfigurationService#getTargetConfigurationValue(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
     */
    @Override
    public String getTargetConfigurationValue(final String clientName, final String configurationType, final String sourceSystem,
                                              final String targetSystem, final List sourceSystemValues)
    {
        if(sourceSystemValues == null || sourceSystemValues.size() == 0)
        {
            throw new RuntimeException("Null or empty source paramaters cannot be supplied to a mapping configuration look up.");
        }

        if(this.configuration != null && this.configuration.isReverseMapping())
        {
            if(sourceSystemValues.size() > 1)
            {
                throw new RuntimeException("The mapping configuration is configured for reverse mappings. Only one source parameter can be" +
                        " provided for a reverse mapping as only one to one reverse mappings are supported. You have provided " + sourceSystemValues.size()
                        + " source parameters.");
            }

            return this.dao.getReverseMapping(clientName, configurationType, sourceSystem, targetSystem, sourceSystemValues.get(0));
        }
        else
        {
            return this.dao.getTargetConfigurationValue(clientName, configurationType, sourceSystem, targetSystem, sourceSystemValues);
        }
    }

    /* (non-Javadoc)
     * @see org.ikasan.mapping.service.MappingConfigurationService#getTargetConfigurationValue(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public String getTargetConfigurationValue(final String clientName, String configurationType, String sourceSystem, String targetSystem,
                                              String sourceSystemValue)
    {
        if(sourceSystemValue == null)
        {
            throw new RuntimeException("A null source paramater cannot be supplied to a mapping configuration look up.");
        }

        List sourceSystemValues = new ArrayList();
        sourceSystemValues.add(sourceSystemValue);

        return getTargetConfigurationValue(clientName, configurationType, sourceSystem, targetSystem, sourceSystemValues);
    }


    /* (non-Javadoc)
	 * @see com.mizuho.cmi2.mappingConfiguration.service.MappingConfigurationService#getTargetConfigurationValueWithIgnores(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
	 */
    public String getTargetConfigurationValueWithIgnores(String clientName,
                                                         String configurationTypeName, String sourceContext,
                                                         String targetContext, List sourceSystemValues)
    {
        String returnValue = this.dao.getTargetConfigurationValueWithIgnores(clientName, configurationTypeName, sourceContext,
                targetContext, sourceSystemValues, sourceSystemValues.size());

        boolean resultFound = false;

        SetProducer setProducer = new SetProducer();

        if(returnValue == null)
        {
            for(int i=sourceSystemValues.size() - 1; i>0; i--)
            {
                List> subSets = setProducer.combinations(sourceSystemValues, i);

                String result = null;

                for(List subSet: subSets)
                {
                    ArrayList subList = new ArrayList();
                    subList.addAll(subSet);

                    returnValue = this.dao.getTargetConfigurationValueWithIgnores(clientName, configurationTypeName, sourceContext,
                            targetContext, subList, sourceSystemValues.size());

                    if(returnValue != null && resultFound)
                    {
                        StringBuffer sourceSystemValuesSB = new StringBuffer();

                        sourceSystemValuesSB.append("[SourceSystemValues = ");
                        for(String sourceSystemValue: sourceSystemValues)
                        {
                            sourceSystemValuesSB.append(sourceSystemValue).append(" ");
                        }
                        sourceSystemValuesSB.append("]");

                        String errorMessage = "Multiple sub results returned from the mapping configuration service. " +
                                "[Client = " + clientName + "] [MappingConfigurationType = " + configurationTypeName + "] [SourceContext = " + sourceContext + "] " +
                                "[TargetContext = " + targetContext + "] " + sourceSystemValuesSB.toString();

                        logger.error(errorMessage);

                        throw new RuntimeException(errorMessage);
                    }

                    if(returnValue != null)
                    {
                        resultFound = true;
                        result = returnValue;
                    }
                }

                if(result != null)
                {
                    return result;
                }
            }
        }

        return returnValue;
    }

    /* (non-Javadoc)
     * @see com.mizuho.cmi2.mappingConfiguration.service.MappingConfigurationService#getTargetConfigurationValueWithIgnores(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
     */
    public String getTargetConfigurationValueWithIgnoresWithOrdinality(String clientName,
                                                                       String configurationTypeName, String sourceContext,
                                                                       String targetContext, List sourceSystemValues)
    {
        String returnValue = this.dao.getTargetConfigurationValueWithIgnoresWithOrdinality(clientName, configurationTypeName, sourceContext,
                targetContext, sourceSystemValues, sourceSystemValues.size());

        boolean resultFound = false;

        SetProducer setProducer = new SetProducer();

        if(returnValue == null)
        {
            for(int i=sourceSystemValues.size() - 1; i>0; i--)
            {
                List> subSets = setProducer.combinations(sourceSystemValues, i);

                String result = null;

                for(List subSet: subSets)
                {
                    ArrayList subList = new ArrayList();
                    subList.addAll(subSet);

                    returnValue = this.dao.getTargetConfigurationValueWithIgnoresWithOrdinality(clientName, configurationTypeName, sourceContext,
                            targetContext, subList, sourceSystemValues.size());

                    if(returnValue != null && resultFound)
                    {
                        StringBuffer sourceSystemValuesSB = new StringBuffer();

                        sourceSystemValuesSB.append("[SourceSystemValues = ");
                        for(QueryParameter sourceSystemValue: sourceSystemValues)
                        {
                            sourceSystemValuesSB.append(sourceSystemValue).append(" ");
                        }
                        sourceSystemValuesSB.append("]");

                        String errorMessage = "Multiple sub results returned from the mapping configuration service. " +
                                "[Client = " + clientName + "] [MappingConfigurationType = " + configurationTypeName + "] [SourceContext = " + sourceContext + "] " +
                                "[TargetContext = " + targetContext + "] " + sourceSystemValuesSB.toString();

                        logger.error(errorMessage);

                        throw new RuntimeException(errorMessage);
                    }

                    if(returnValue != null)
                    {
                        resultFound = true;
                        result = returnValue;
                    }
                }

                if(result != null)
                {
                    return result;
                }
            }
        }

        return returnValue;
    }

    @Override
    public List getTargetConfigurationValues(String clientName, String configurationType, String sourceContext, String targetContext, List sourceSystemValues)
    {
        return this.dao.getTargetConfigurationValues(clientName, configurationType, sourceContext, targetContext, sourceSystemValues);
    }

    @Override
    public List getTargetConfigurationValuesWithOrdinality(String clientName, String configurationType, String sourceContext, String targetContext, List sourceSystemValues)
    {
        return this.dao.getTargetConfigurationValuesWithOrdinality(clientName, configurationType, sourceContext, targetContext, sourceSystemValues);
    }

    @Override
    public void setConfiguration(MappingConfigurationServiceConfiguration configuration)
    {
        this.configuration = configuration;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy