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

com.kenshoo.pl.entity.spi.helpers.CopyFieldsOnCreateEnricher Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.spi.helpers;

import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.spi.PostFetchCommandEnricher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

public class CopyFieldsOnCreateEnricher> implements PostFetchCommandEnricher {

    final List> fields2Copy = new ArrayList<>();
    protected final Set> requiredFields = new HashSet<>();

    public  CopyFieldsOnCreateEnricher(EntityField sourceField, EntityField targetField) {
        //noinspection unchecked
        this(new Field2Copy<>(sourceField, targetField));
    }

    public  CopyFieldsOnCreateEnricher(EntityField sourceField1, EntityField targetField1,
                                               EntityField sourceField2, EntityField targetField2) {
        //noinspection unchecked
        this(new Field2Copy<>(sourceField1, targetField1), new Field2Copy<>(sourceField2, targetField2));
    }

    public  CopyFieldsOnCreateEnricher(EntityField sourceField1, EntityField targetField1,
                                                   EntityField sourceField2, EntityField targetField2,
                                                   EntityField sourceField3, EntityField targetField3) {
        //noinspection unchecked
        this(new Field2Copy<>(sourceField1, targetField1),
                new Field2Copy<>(sourceField2, targetField2),
                new Field2Copy<>(sourceField3, targetField3));
    }

    public  CopyFieldsOnCreateEnricher(EntityField sourceField1, EntityField targetField1,
                                                   EntityField sourceField2, EntityField targetField2,
                                                   EntityField sourceField3, EntityField targetField3,
                                                   EntityField sourceField4, EntityField targetField4) {
        //noinspection unchecked
        this(new Field2Copy<>(sourceField1, targetField1),
                new Field2Copy<>(sourceField2, targetField2),
                new Field2Copy<>(sourceField3, targetField3),
                new Field2Copy<>(sourceField4, targetField4));
    }

    @SuppressWarnings("unchecked")
    private CopyFieldsOnCreateEnricher(Field2Copy... fieldsToCopy) {
        this.fields2Copy.addAll(Arrays.asList(fieldsToCopy));
        addRequiredFields();
    }

    protected void addRequiredFields() {
        this.fields2Copy.stream()
                        .map(input -> input.source)
                        .forEach(requiredFields::add);
    }

    @Override
    public SupportedChangeOperation getSupportedChangeOperation() {
        return SupportedChangeOperation.CREATE;
    }

    @Override
    public void enrich(Collection> commands, ChangeOperation changeOperation, ChangeContext changeContext) {
        for (ChangeEntityCommand command : commands) {
            Entity entity = changeContext.getEntity(command);
            for (Field2Copy field2Copy : fields2Copy) {
                copyField(field2Copy, entity, command);
            }
        }
    }

    @Override
    public Stream> fieldsToEnrich() {
        return fields2Copy.stream().map(pair -> pair.target);
    }
    
    private  void copyField(Field2Copy field2Copy, Entity entity, ChangeEntityCommand command) {
        command.set(field2Copy.target, entity.get(field2Copy.source));
    }

    @Override
    public Stream> requiredFields(Collection> fieldsToUpdate, ChangeOperation changeOperation) {
        return requiredFields.stream();
    }


    public static class Field2Copy, T> {
        private final EntityField source;
        private final EntityField target;

        Field2Copy(EntityField source, EntityField target) {
            this.source = source;
            this.target = target;
        }

        public EntityField getTarget() {
            return target;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy