Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DataCleaner (community edition) Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.datacleaner.job;
 
 import java.io.File;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
public class JaxbJobReader implements JobReader<InputStream> {
    private static final Logger logger = LoggerFactory.getLogger(JaxbJobReader.class);
    private final JAXBContext _jaxbContext;
    public JaxbJobReader(DataCleanerConfiguration configuration) {
        if (configuration == null) {
            throw new IllegalArgumentException("Configuration cannot be null");
        }
         = configuration;
        try {
             = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName(),
                    ObjectFactory.class.getClassLoader());
        } catch (JAXBException e) {
            throw new IllegalStateException(e);
        }
    }

    
    @Override
        try (AnalysisJobBuilder ajb = create(inputStream)) {
            return ajb.toAnalysisJob();
        }
    }
    @Override
    public AnalysisJob read(InputStream inputStreamSourceColumnMapping sourceColumnMapping) {
        try (AnalysisJobBuilder ajb = create(inputStreamsourceColumnMapping)) {
            return ajb.toAnalysisJob();
        }
    }
        InputStream inputStream = null;
        try {
            inputStream = file.getContent().getInputStream();
            return readMetadata(inputStream);
        } catch (FileSystemException e) {
            throw new IllegalArgumentException(e);
        } finally {
            FileHelper.safeClose(inputStream);
        }
    }
    public AnalysisJobMetadata readMetadata(File file) {
        InputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            return readMetadata(inputStream);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        } finally {
            FileHelper.safeClose(inputStream);
        }
    }
    @Override
    public AnalysisJobMetadata readMetadata(InputStream inputStream) {
        JobType job = unmarshallJob(inputStream);
        return readMetadata(job);
    }
    public AnalysisJobMetadata readMetadata(JobType job) {
        final String datastoreName = job.getSource().getDataContext().getRef();
        final List<StringsourceColumnPaths = getSourceColumnPaths(job);
        final List<org.apache.metamodel.schema.ColumnTypesourceColumnTypes = getSourceColumnTypes(job);
        final Map<StringStringvariables = getVariables(job);
        final String jobName;
        final String jobVersion;
        final String jobDescription;
        final String author;
        final Date createdDate;
        final Date updatedDate;
        final Map<StringStringmetadataProperties;
        JobMetadataType metadata = job.getJobMetadata();
        if (metadata == null) {
            jobName = null;
            jobVersion = null;
            jobDescription = null;
            author = null;
            createdDate = null;
            updatedDate = null;
            metadataProperties = Collections.emptyMap();
        } else {
            jobName = metadata.getJobName();
            jobVersion = metadata.getJobVersion();
            jobDescription = metadata.getJobDescription();
            author = metadata.getAuthor();
            metadataProperties = getMetadataProperties(metadata);
            final XMLGregorianCalendar createdDateCal = metadata.getCreatedDate();
            if (createdDateCal == null) {
                createdDate = null;
            } else {
                createdDate = createdDateCal.toGregorianCalendar().getTime();
            }
            final XMLGregorianCalendar updatedDateCal = metadata.getUpdatedDate();
            if (updatedDateCal == null) {
                updatedDate = null;
            } else {
                updatedDate = updatedDateCal.toGregorianCalendar().getTime();
            }
        }
        return new ImmutableAnalysisJobMetadata(jobNamejobVersionjobDescriptionauthorcreatedDateupdatedDate,
                datastoreNamesourceColumnPathssourceColumnTypesvariablesmetadataProperties);
    }
    private Map<StringStringgetMetadataProperties(JobMetadataType metadata) {
        MetadataProperties properties = metadata.getMetadataProperties();
        if (properties == null) {
            return Collections.emptyMap();
        }
        Map<StringStringmetadataProperties = new HashMap<StringString>();
        List<org.datacleaner.job.jaxb.MetadataProperties.Propertyproperty = properties.getProperty();
        for (int i = 0; i < property.size(); i++) {
            String name = property.get(i).getName();
            String value = property.get(i).getValue();
            metadataProperties.put(namevalue);
        }
        return metadataProperties;
    }
    public Map<StringStringgetVariables(JobType job) {
        final Map<StringStringresult = new HashMap<StringString>();
        VariablesType variablesType = job.getSource().getVariables();
        if (variablesType != null) {
            List<VariableTypevariables = variablesType.getVariable();
            for (VariableType variableType : variables) {
                String id = variableType.getId();
                String value = variableType.getValue();
                result.put(idvalue);
            }
        }
        return result;
    }
    public List<StringgetSourceColumnPaths(JobType job) {
        final List<Stringpaths;
        final ColumnsType columnsType = job.getSource().getColumns();
        if (columnsType != null) {
            final List<ColumnTypecolumns = columnsType.getColumn();
            paths = new ArrayList<String>(columns.size());
            for (ColumnType columnType : columns) {
                final String path = columnType.getPath();
                paths.add(path);
            }
        } else {
            paths = Collections.emptyList();
        }
        return paths;
    }
        final List<org.apache.metamodel.schema.ColumnTypetypes;
        final ColumnsType columnsType = job.getSource().getColumns();
        if (columnsType != null) {
            final List<ColumnTypecolumns = columnsType.getColumn();
            types = new ArrayList<org.apache.metamodel.schema.ColumnType>(columns.size());
            for (ColumnType columnType : columns) {
                final String typeName = columnType.getType();
                if (StringUtils.isNullOrEmpty(typeName)) {
                    types.add(null);
                } else {
                    try {
                        final org.apache.metamodel.schema.ColumnType type = org.apache.metamodel.schema.ColumnTypeImpl
                                .valueOf(typeName);
                        types.add(type);
                    } catch (IllegalArgumentException e) {
                        // type literal was not a valid ColumnType
                        .warn("Unrecognized column type: {}"typeName);
                        types.add(null);
                    }
                }
            }
        } else {
            types = Collections.emptyList();
        }
        return types;
    }
    public AnalysisJobBuilder create(FileObject file) {
        InputStream inputStream = null;
        try {
            inputStream = file.getContent().getInputStream();
            return create(inputStream);
        } catch (FileSystemException e) {
            throw new IllegalArgumentException(e);
        } finally {
            FileHelper.safeClose(inputStream);
        }
    }
    public AnalysisJobBuilder create(File file) {
        InputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            return create(inputStream);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        } finally {
            FileHelper.safeClose(inputStream);
        }
    }
    public AnalysisJobBuilder create(InputStream inputStreamthrows NoSuchDatastoreException {
        return create(unmarshallJob(inputStream), nullnull);
    }
    public AnalysisJobBuilder create(InputStream inputStreamSourceColumnMapping sourceColumnMapping)
            throws NoSuchDatastoreException {
        return create(inputStreamsourceColumnMappingnull);
    }
    public AnalysisJobBuilder create(InputStream inputStreamSourceColumnMapping sourceColumnMapping,
            Map<StringStringvariableOverridesthrows NoSuchDatastoreException {
        return create(unmarshallJob(inputStream), sourceColumnMappingvariableOverrides);
    }
    public AnalysisJobBuilder create(InputStream inputStreamMap<StringStringvariableOverrides)
            throws NoSuchDatastoreException {
        return create(unmarshallJob(inputStream), nullvariableOverrides);
    }
    private JobType unmarshallJob(InputStream inputStream) {
        try {
            Unmarshaller unmarshaller = .createUnmarshaller();
            unmarshaller.setEventHandler(new JaxbValidationEventHandler());
            JobType job = (JobTypeunmarshaller.unmarshal(inputStream);
            return job;
        } catch (JAXBException e) {
            throw new IllegalArgumentException(e);
        }
    }
    public AnalysisJobBuilder create(JobType job) {
        return create(jobnullnull);
    }
    public AnalysisJobBuilder create(JobType jobSourceColumnMapping sourceColumnMapping,
            Map<StringStringvariableOverridesthrows NoSuchDatastoreException {
        if (job == null) {
            throw new IllegalArgumentException("Job cannot be null");
        }
        if (sourceColumnMapping != null && !sourceColumnMapping.isSatisfied()) {
            throw new IllegalArgumentException("Source column mapping is not satisfied!");
        }
        final Map<StringStringvariables = getVariables(job);
        if (variableOverrides != null) {
            final Set<Entry<StringString>> entrySet = variableOverrides.entrySet();
            for (Entry<StringStringentry : entrySet) {
                final String key = entry.getKey();
                final String value = entry.getValue();
                String originalValue = variables.put(keyvalue);
                .info("Overriding variable: {}={} (original value was {})"new Object[] { keyvalue,
                        originalValue });
            }
        }
        final JobMetadataType metadata = job.getJobMetadata();
        if (metadata != null) {
            .info("Job name: {}"metadata.getJobName());
            .info("Job version: {}"metadata.getJobVersion());
            .info("Job description: {}"metadata.getJobDescription());
            .info("Author: {}"metadata.getAuthor());
            .info("Created date: {}"metadata.getCreatedDate());
            .info("Updated date: {}"metadata.getUpdatedDate());
            .info("Job metadata properties: {}"getMetadataProperties(metadata));
        }
        final AnalysisJobBuilder builder = new AnalysisJobBuilder();
        try {
            final AnalysisJobBuilder result = create(jobsourceColumnMappingmetadatavariablesbuilder);
            return result;
        } catch (RuntimeException e) {
            FileHelper.safeClose(builder);
            throw e;
        }
    }
    private AnalysisJobBuilder create(JobType jobSourceColumnMapping sourceColumnMappingJobMetadataType metadata,
            final Map<StringStringvariablesfinal AnalysisJobBuilder analysisJobBuilder) {
        final Datastore datastore;
        final DatastoreConnection datastoreConnection;
        final SourceType source = job.getSource();
        if (sourceColumnMapping == null) {
            // use automatic mapping if no explicit mapping is supplied
            final DataContextType dataContext = source.getDataContext();
            final String ref = dataContext.getRef();
            if (StringUtils.isNullOrEmpty(ref)) {
                throw new IllegalStateException("Datastore ref cannot be null");
            }
            datastore = .getDatastoreCatalog().getDatastore(ref);
            if (datastore == null) {
                throw new NoSuchDatastoreException(ref);
            }
            datastoreConnection = datastore.openConnection();
            final List<StringsourceColumnPaths = getSourceColumnPaths(job);
            sourceColumnMapping = new SourceColumnMapping(sourceColumnPaths);
            sourceColumnMapping.autoMap(datastore);
        } else {
            datastore = sourceColumnMapping.getDatastore();
            datastoreConnection = datastore.openConnection();
        }
        try {
            analysisJobBuilder.setDatastore(datastore);
            if (metadata != null) {
                final ImmutableAnalysisJobMetadata immutableAnalysisJobMetadata = new ImmutableAnalysisJobMetadata(
                        metadata.getJobName(), metadata.getJobVersion(), metadata.getJobDescription(),
                        metadata.getAuthor(), convertToDate(metadata.getCreatedDate()),
                        convertToDate(metadata.getUpdatedDate()), datastore.getName(), getSourceColumnPaths(job),
                        getSourceColumnTypes(job), variablesgetMetadataProperties(metadata));
                analysisJobBuilder.setAnalysisJobMetadata(immutableAnalysisJobMetadata);
            }
            // map column id's to input columns
            final Map<StringInputColumn<?>> inputColumns = readSourceColumns(sourceColumnMappinganalysisJobBuilder,
                    source);
            final StringConverter stringConverter = createStringConverter(analysisJobBuilder);
            final DescriptorProvider descriptorProvider = .getEnvironment().getDescriptorProvider();
            final Map<ComponentTypeComponentBuildercomponentBuilders = new HashMap<>();
            // iterate to create all the initial component builders without any
            // wiring
            final List<ComponentTypeallComponentTypes = getAllComponentTypes(job);
            for (ComponentType componentType : allComponentTypes) {
                final ComponentBuilder componentBuilder = createComponentBuilder(analysisJobBuilder,
                        descriptorProvidercomponentType);
                initializeComponentBuilder(variablesstringConvertercomponentBuilderscomponentType,
                        componentBuilder);
            }
            wireInputColumns(inputColumnscomponentBuilders);
            wireRequirements(componentBuilders);
            return analysisJobBuilder;
        } finally {
            datastoreConnection.close();
        }
    }
    private void wireRequirements(final Map<ComponentTypeComponentBuildercomponentBuilders) {
        final Map<StringFilterOutcomeoutcomeMapping = new HashMap<StringFilterOutcome>();
        // iterate initialize collect all outcomes by their IDs
        for (ComponentType componentType : componentBuilders.keySet()) {
            if (componentType instanceof FilterType) {
                final FilterType filterType = (FilterTypecomponentType;
                final FilterComponentBuilder<?, ?> filterBuilder = (FilterComponentBuilder<?, ?>) componentBuilders
                        .get(componentType);
                final List<OutcomeTypeoutcomeTypes = filterType.getOutcome();
                for (OutcomeType outcomeType : outcomeTypes) {
                    final String categoryName = outcomeType.getCategory();
                    final Enum<?> category = filterBuilder.getDescriptor().getOutcomeCategoryByName(categoryName);
                    if (category == null) {
                        throw new ComponentConfigurationException("No such outcome category name: " + categoryName
                                + " (in " + filterBuilder.getDescriptor().getDisplayName() + ")");
                    }
                    final String id = outcomeType.getId();
                    if (StringUtils.isNullOrEmpty(id)) {
                        throw new IllegalStateException("Outcome id cannot be null");
                    }
                    if (outcomeMapping.containsKey(id)) {
                        throw new ComponentConfigurationException("Outcome id '" + id + "' is not unique");
                    }
                    outcomeMapping.put(idfilterBuilder.getFilterOutcome(category));
                }
            }
        }
        // iterate again to set up filter outcome dependencies
        for (ComponentType componentType : componentBuilders.keySet()) {
            wireRequirement(outcomeMappingcomponentBuilderscomponentType);
        }
    }
    private ComponentBuilder createComponentBuilder(final AnalysisJobBuilder analysisJobBuilder,
            final DescriptorProvider descriptorProviderComponentType componentType) {
        String ref;
        final ComponentBuilder componentBuilder;
        if (componentType instanceof TransformerType) {
            // special instantiation of transformers
            final TransformerType transformer = (TransformerTypecomponentType;
            ref = transformer.getDescriptor().getRef();
            if (StringUtils.isNullOrEmpty(ref)) {
                throw new IllegalStateException("Transformer descriptor ref cannot be null");
            }
            final TransformerDescriptor<?> descriptor = descriptorProvider.getTransformerDescriptorByDisplayName(ref);
            if (descriptor == null) {
                throw new NoSuchComponentException(Transformer.classref);
            }
            componentBuilder = analysisJobBuilder.addTransformer(descriptor);
        } else if (componentType instanceof FilterType) {
            // special instantiation of filters
            final FilterType filter = (FilterTypecomponentType;
            ref = filter.getDescriptor().getRef();
            if (StringUtils.isNullOrEmpty(ref)) {
                throw new IllegalStateException("Filter descriptor ref cannot be null");
            }
            final FilterDescriptor<?, ?> descriptor = descriptorProvider.getFilterDescriptorByDisplayName(ref);
            if (descriptor == null) {
                throw new NoSuchComponentException(Filter.classref);
            }
            componentBuilder = analysisJobBuilder.addFilter(descriptor);
        } else if (componentType instanceof AnalyzerType) {
            // special instantiation of analyzers
            final AnalyzerType analyzer = (AnalyzerTypecomponentType;
            ref = analyzer.getDescriptor().getRef();
            if (StringUtils.isNullOrEmpty(ref)) {
                throw new IllegalStateException("Analyzer descriptor ref cannot be null");
            }
            final AnalyzerDescriptor<?> descriptor = descriptorProvider.getAnalyzerDescriptorByDisplayName(ref);
            if (descriptor == null) {
                throw new NoSuchComponentException(Analyzer.classref);
            }
            final Class<? extends Analyzer<?>> analyzerClass = descriptor.getComponentClass();
            componentBuilder = analysisJobBuilder.addAnalyzer(analyzerClass);
        } else {
            throw new UnsupportedOperationException("Unexpected transformation component type: " + componentType);
        }
        return componentBuilder;
    }
        final List<ComponentTyperesult = new ArrayList<>();
        final TransformationType transformation = job.getTransformation();
        if (transformation != null) {
            result.addAll(transformation.getTransformerOrFilter());
        }
        final AnalysisType analysis = job.getAnalysis();
        if (analysis != null) {
            result.addAll(analysis.getAnalyzer());
        }
        return result;
    }

    
Wires input columns from either source or transformer output. This process is an iteration to find the next consumer with "satisfied column requirements".

Parameters:
inputColumns
componentBuilders
    private void wireInputColumns(final Map<StringInputColumn<?>> inputColumns,
            final Map<ComponentTypeComponentBuildercomponentBuilders) {
        // iterate again to set up column dependencies (one at a time -
        // whichever is possible based on the configuration of the column
        // sources (transformers))
        final List<ComponentTypeunconfiguredComponentKeys = new LinkedList<ComponentType>(componentBuilders.keySet());
        while (!unconfiguredComponentKeys.isEmpty()) {
            boolean progress = false;
            for (Iterator<ComponentTypeit = unconfiguredComponentKeys.iterator(); it.hasNext();) {
                boolean configurable = true;
                final ComponentType unconfiguredTransformerKey = it.next();
                final List<InputTypeinput = unconfiguredTransformerKey.getInput();
                for (InputType inputType : input) {
                    final String ref = inputType.getRef();
                    if (StringUtils.isNullOrEmpty(ref)) {
                        final String value = inputType.getValue();
                        if (value == null) {
                            throw new IllegalStateException("Component input column ref & value cannot be null");
                        }
                    } else if (!inputColumns.containsKey(ref)) {
                        configurable = false;
                        break;
                    }
                }
                if (configurable) {
                    progress = true;
                    final ComponentBuilder componentBuilder = componentBuilders.get(unconfiguredTransformerKey);
                    applyInputColumns(inputinputColumnscomponentBuilder);
                    if (componentBuilder instanceof TransformerComponentBuilder) {
                        final TransformerComponentBuilder<?> transformerBuilder = (TransformerComponentBuilder<?>) componentBuilder;
                        final TransformerType transformerType = (TransformerTypeunconfiguredTransformerKey;
                        final List<MutableInputColumn<?>> outputColumns = transformerBuilder.getOutputColumns();
                        final List<OutputTypeoutput = transformerType.getOutput();
                        if (outputColumns.size() != output.size()) {
                            final String message = "Expected " + outputColumns.size() + " output column(s), but found "
                                    + output.size() + " (" + transformerBuilder + ")";
                            if (outputColumns.isEmpty()) {
                                // typically empty output columns is due to
                                // a component not being configured, we'll
                                // attach the configuration exception as a
                                // cause.
                                try {
                                    transformerBuilder.isConfigured(true);
                                } catch (Exception e) {
                                    throw new ComponentConfigurationException(messagee);
                                }
                            }
                            throw new ComponentConfigurationException(message);
                        }
                        for (int i = 0; i < output.size(); i++) {
                            final OutputType o1 = output.get(i);
                            final MutableInputColumn<?> o2 = outputColumns.get(i);
                            final String name = o1.getName();
                            if (!StringUtils.isNullOrEmpty(name)) {
                                o2.setName(name);
                            }
                            final Boolean hidden = o1.isHidden();
                            if (hidden != null && hidden.booleanValue()) {
                                o2.setHidden(true);
                            }
                            final String id = o1.getId();
                            if (StringUtils.isNullOrEmpty(id)) {
                                throw new IllegalStateException("Transformer output column id cannot be null");
                            }
                            registerInputColumn(inputColumnsido2);
                        }
                    }
                    // remove this component from the "unconfigured" set
                    it.remove();
                }
            }
            if (!progress) {
                // no progress was made in a complete iteration - no satisfied
                // requirements where found. Time to produce an error message...
                final StringBuilder sb = new StringBuilder();
                for (ComponentType transformerType : unconfiguredComponentKeys) {
                    if (sb.length() != 0) {
                        sb.append(", ");
                    }
                    final DescriptorType descriptor = transformerType.getDescriptor();
                    sb.append(descriptor.getRef());
                    sb.append("(input: ");
                    final List<InputTypeinput = transformerType.getInput();
                    int i = 0;
                    for (InputType inputType : input) {
                        if (i != 0) {
                            sb.append(", ");
                        }
                        final String ref = inputType.getRef();
                        if (StringUtils.isNullOrEmpty(ref)) {
                            sb.append("value=" + inputType.getValue());
                        } else {
                            sb.append("ref=" + ref);
                        }
                        i++;
                    }
                    sb.append(")");
                }
                throw new ComponentConfigurationException("Could not connect column dependencies for components: "
                        + sb.toString());
            }
        }
    }
    private void initializeComponentBuilder(final Map<StringStringvariablesfinal StringConverter stringConverter,
            final Map<ComponentTypeComponentBuildercomponentBuildersComponentType componentType,
            final ComponentBuilder componentBuilder) {
        // shared setting of properties (except for input columns)
        componentBuilder.setName(componentType.getName());
        applyProperties(componentBuildercomponentType.getProperties(), componentType.getMetadataProperties(),
                stringConvertervariables);
        componentBuilders.put(componentTypecomponentBuilder);
    }
    private void wireRequirement(final Map<StringFilterOutcomeoutcomeMapping,
            final Map<ComponentTypeComponentBuildercomponentBuildersComponentType componentType) {
        final String ref = componentType.getRequires();
        if (ref != null) {
            final ComponentBuilder builder = componentBuilders.get(componentType);
            final ComponentRequirement requirement = getRequirement(refoutcomeMapping);
            builder.setComponentRequirement(requirement);
        }
    }

    
Reads the source element of the job to extract a map of column IDs and related source org.datacleaner.api.InputColumns.

Parameters:
sourceColumnMapping
analysisJobBuilder
source
Returns:
    private Map<StringInputColumn<?>> readSourceColumns(SourceColumnMapping sourceColumnMapping,
            final AnalysisJobBuilder analysisJobBuilderfinal SourceType source) {
        final Map<StringInputColumn<?>> inputColumns = new HashMap<StringInputColumn<?>>();
        final ColumnsType columnsType = source.getColumns();
        if (columnsType != null) {
            final List<ColumnTypecolumns = columnsType.getColumn();
            for (ColumnType column : columns) {
                final String path = column.getPath();
                if (StringUtils.isNullOrEmpty(path)) {
                    throw new IllegalStateException("Column path cannot be null");
                }
                final Column physicalColumn = sourceColumnMapping.getColumn(path);
                if (physicalColumn == null) {
                    .error("Column {} not found in {}"pathsourceColumnMapping);
                    throw new NoSuchColumnException(path);
                }
                final MetaModelInputColumn inputColumn = new MetaModelInputColumn(physicalColumn);
                final String id = column.getId();
                if (StringUtils.isNullOrEmpty(id)) {
                    throw new IllegalStateException("Source column id cannot be null");
                }
                final String expectedType = column.getType();
                if (expectedType != null) {
                    org.apache.metamodel.schema.ColumnType actualType = physicalColumn.getType();
                    if (actualType != null && !expectedType.equals(actualType.toString())) {
                        .warn("Column '{}' had type '{}', but '{}' was expected."new Object[] { path,
                                actualTypeexpectedType });
                    }
                }
                registerInputColumn(inputColumnsidinputColumn);
                analysisJobBuilder.addSourceColumn(inputColumn);
            }
        }
        return inputColumns;
    }
    private Date convertToDate(XMLGregorianCalendar calendar) {
        if (calendar != null) {
            return calendar.toGregorianCalendar().getTime();
        }
        return null;
    }
    private ComponentRequirement getRequirement(String refMap<StringFilterOutcomeoutcomeMapping) {
        if (..equals(ref)) {
            return AnyComponentRequirement.get();
        }
        // check for simple component requirements
        {
            final FilterOutcome filterOutcome = outcomeMapping.get(ref);
            if (filterOutcome != null) {
                return new SimpleComponentRequirement(filterOutcome);
            }
        }
        // check for compound component requirements
        final List<Stringtokens = Splitter.on(" OR ").omitEmptyStrings().trimResults().splitToList(ref);
        if (tokens.size() > 1) {
            final List<FilterOutcomelist = new ArrayList<>(tokens.size());
            for (final String token : tokens) {
                final FilterOutcome filterOutcome = outcomeMapping.get(token);
                if (filterOutcome == null) {
                    throw new ComponentConfigurationException("Could not resolve outcome '" + token
                            + "' in requirement: " + ref);
                }
                list.add(filterOutcome);
            }
            return new CompoundComponentRequirement(list);
        }
        throw new ComponentConfigurationException("Could not resolve requirement: " + ref);
    }
    private void applyInputColumns(List<InputTypeinputMap<StringInputColumn<?>> inputColumns,
            ComponentBuilder componentBuilder) {
        // build a map of inputs first so that we can set the
        // input in one go
        final ListMultimap<ConfiguredPropertyDescriptorInputColumn<?>> inputMap = MultimapBuilder.hashKeys()
                .arrayListValues().build();
        for (InputType inputType : input) {
            String name = inputType.getName();
            String ref = inputType.getRef();
            InputColumn<?> inputColumn;
            if (StringUtils.isNullOrEmpty(ref)) {
                inputColumn = createExpressionBasedInputColumn(inputType);
            } else {
                inputColumn = inputColumns.get(ref);
            }
            if (StringUtils.isNullOrEmpty(name)) {
                ConfiguredPropertyDescriptor propertyDescriptor = componentBuilder
                        .getDefaultConfiguredPropertyForInput();
                inputMap.put(propertyDescriptorinputColumn);
            } else {
                ConfiguredPropertyDescriptor propertyDescriptor = componentBuilder.getDescriptor()
                        .getConfiguredProperty(name);
                inputMap.put(propertyDescriptorinputColumn);
            }
        }
        final Set<ConfiguredPropertyDescriptorkeys = inputMap.keySet();
        for (ConfiguredPropertyDescriptor propertyDescriptor : keys) {
            List<InputColumn<?>> inputColumnsForProperty = inputMap.get(propertyDescriptor);
            componentBuilder.addInputColumns(inputColumnsForPropertypropertyDescriptor);
        }
    }
    private StringConverter createStringConverter(final AnalysisJobBuilder analysisJobBuilder) {
        final AnalysisJob job = analysisJobBuilder.toAnalysisJob(false);
        return new StringConverter(job);
    }
    private InputColumn<?> createExpressionBasedInputColumn(InputType inputType) {
        String expression = inputType.getValue();
        if (expression == null) {
            throw new IllegalStateException("Input ref & value cannot both be null");
        }
        if (expression.indexOf("#{") == -1) {
            return new ConstantInputColumn(expression);
        } else {
            return new ELInputColumn(expression);
        }
    }
    private void registerInputColumn(Map<StringInputColumn<?>> inputColumnsString idInputColumn<?> inputColumn) {
        if (StringUtils.isNullOrEmpty(id)) {
            throw new IllegalStateException("Column id cannot be null");
        }
        if (inputColumns.containsKey(id)) {
            throw new ComponentConfigurationException("Column id is not unique: " + id);
        }
        inputColumns.put(idinputColumn);
    }
    private void applyProperties(final ComponentBuilder builder,
            final ConfiguredPropertiesType configuredPropertiesTypefinal MetadataProperties metadataPropertiesType,
            final StringConverter stringConverterfinal Map<StringStringvariables) {
        if (configuredPropertiesType != null) {
            final List<Propertyproperties = configuredPropertiesType.getProperty();
            final ComponentDescriptor<?> descriptor = builder.getDescriptor();
            for (Property property : properties) {
                final String name = property.getName();
                final ConfiguredPropertyDescriptor configuredProperty = descriptor.getConfiguredProperty(name);
                if (configuredProperty == null) {
                    throw new ComponentConfigurationException("No such property: " + name);
                }
                String stringValue = getValue(property);
                if (stringValue == null) {
                    String variableRef = property.getRef();
                    if (variableRef == null) {
                        throw new IllegalStateException("Neither value nor ref was specified for property: " + name);
                    }
                    stringValue = variables.get(variableRef);
                    if (stringValue == null) {
                        throw new ComponentConfigurationException("No such variable: " + variableRef);
                    }
                }
                final Class<? extends Converter<?>> customConverter = configuredProperty.getCustomConverter();
                final Object value = stringConverter.deserialize(stringValueconfiguredProperty.getType(),
                        customConverter);
                .debug("Setting property '{}' to {}"namevalue);
                builder.setConfiguredProperty(configuredPropertyvalue);
            }
        }
        if (metadataPropertiesType != null) {
            final List<org.datacleaner.job.jaxb.MetadataProperties.PropertypropertyList = metadataPropertiesType
                    .getProperty();
            for (org.datacleaner.job.jaxb.MetadataProperties.Property property : propertyList) {
                final String name = property.getName();
                final String value = property.getValue();
                builder.setMetadataProperty(namevalue);
            }
        }
    }
    private String getValue(Property property) {
        String value = property.getValue();
        if (StringUtils.isNullOrEmpty(value)) {
            final String valueAttribute = property.getValueAttribute();
            if (value != null) {
                value = valueAttribute;
            }
        }
        return value;
    }
New to GrepCode? Check out our FAQ X