Custom Cleanse Functions



Below is an example of a simple cleanse function that takes the user name from INPUT_PORT_1 and outputs the full user name in OUTPUT_PORT_1. The example shows:

  • Self configuration (configure() call);

  • Adding fields (userService, module, two configuration variables);

  • Using input/output parameters

package tests;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.unidata.mdm.core.dto.UserWithPasswordDTO;
import org.unidata.mdm.core.service.UserService;
import org.unidata.mdm.data.configuration.DataConfigurationConstants;
import org.unidata.mdm.data.module.DataModule;
import org.unidata.mdm.dq.core.context.CleanseFunctionContext;
import org.unidata.mdm.dq.core.dto.CleanseFunctionResult;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunction;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionConfiguration;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionExecutionScope;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionInputParam;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionOutputParam;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionPortFilteringMode;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionPortInputType;
import org.unidata.mdm.dq.core.type.cleanse.CleanseFunctionPortValueType;
import org.unidata.mdm.dq.core.type.constant.CleanseConstants;
import org.unidata.mdm.system.type.annotation.ConfigurationRef;
import org.unidata.mdm.system.type.annotation.ModuleRef;
import org.unidata.mdm.system.type.configuration.ConfigurationValue;
import org.unidata.mdm.system.type.module.Module;

/**
* @author Mikhail Mikhailov on Feb 16, 2021
*/
public class TestCleanseFunction implements CleanseFunction {

    @Autowired
    private UserService userService;

    @ModuleRef(DataModule.MODULE_ID)
    private Module dataModule;

    @ConfigurationRef(DataConfigurationConstants.PROPERTY_DATA_NODES)
    private ConfigurationValue<String> nodes;

    @ConfigurationRef(DataConfigurationConstants.PROPERTY_DATA_SHARDS)
    private ConfigurationValue<Long> shards;

    /**
    * This function configuration.
    */
    private static final CleanseFunctionConfiguration CONFIGURATION
        = CleanseFunctionConfiguration.configuration()
            .supports(CleanseFunctionExecutionScope.LOCAL)
            .input(CleanseFunctionConfiguration.port()
                    .name(CleanseConstants.INPUT_PORT_1)
                    .displayName("User name")
                    .description("User name to resolve")
                    .filteringMode(CleanseFunctionPortFilteringMode.MODE_ONCE)
                    .inputTypes(CleanseFunctionPortInputType.SIMPLE)
                    .valueTypes(CleanseFunctionPortValueType.STRING)
                    .required(true)
                    .build())
            .output(CleanseFunctionConfiguration.port()
                    .name(CleanseConstants.OUTPUT_PORT_1)
                    .displayName("Full name")
                    .description("Resolved full name or null")
                    .filteringMode(CleanseFunctionPortFilteringMode.MODE_ALL)
                    .inputTypes(CleanseFunctionPortInputType.SIMPLE)
                    .valueTypes(CleanseFunctionPortValueType.STRING)
                    .required(true)
                    .build())
            .build();

    /**
    * Constructor.
    */
    public TestCleanseFunction() {
        super();
    }

    /**
    * {@inheritDoc}
    */
    @Override
    public CleanseFunctionConfiguration configure() {
        return CONFIGURATION;
    }

    /**
    * {@inheritDoc}
    */
    @Override
    public CleanseFunctionResult execute(CleanseFunctionContext ctx) {

        CleanseFunctionResult output = new CleanseFunctionResult();
        CleanseFunctionInputParam param1 = ctx.getInputParam(CleanseConstants.INPUT_PORT_1);

        String result = null;
        if (param1 != null && !param1.isEmpty() && param1.isSingleton()) {

            String input = param1.toSingletonValue();
            UserWithPasswordDTO uwp = userService.getUserByName(input);
            if (Objects.nonNull(uwp)) {
                result = uwp.getFullName();
            }
        }

        output.putOutputParam(CleanseFunctionOutputParam.of(CleanseConstants.OUTPUT_PORT_1, result));

        return output;
    }
}