Set More than one Initializer

Hi,

I’m trying to extend CreateAction functionality as CreateChild , which will eventually create Child record for the selected record in the TreeDataGrid and TreeTable.

I’m using the initializer to set relationship between the records as below.

public CreateChild(String id) {
        super(id);
        initChildAction();
    }

    private void initChildAction(){
        this.initializer = this::initParent;
        this.primary = false;
    }

@Override
    public void setInitializer(Consumer<E> initializer) {
        this.initializer.andThen(initializer);
    }

    private void initParent(E e){
        if(e instanceof Task)
        {
            Task task = (Task) e;
            task.setParentTask((Task) target.getSingleSelected());
        }
    }

I would like to provide screen specific initializer as well. So I included below code expecting the initializer in the Action will be called and then initializer in the screen will be called later as per Consumer. However it is not happening, the first initializer which was set first only getting called.

@Override
        public void setInitializer(Consumer<E> initializer) {
            this.initializer.andThen(initializer);
        }

The initializer set by individual screens are not getting called.

@Install(to = "tasksTable.createChild", subject = "initializer")
    private void tasksTableChildInitializer(Task task) {
        task.setEntity(entityField.getValue());
        task.setAsset(assetDc.getItem());
    }

I need your help and assistance in getting this done.
For now I do have a workaround of removing the initializer set by the action and do that in each screen .

Hello @harikrishnadhas.k1,

The Consumer#andThen() method return a composed Consumer that performs in sequence this operation followed by the after operation:

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }

You could try to replace your setInitializer() method with following:

    @Override
    public void setInitializer(Consumer<E> after) {
        initializer = initializer.andThen(after);
    }

Regards,
Gleb

@durygin. Thanks for the correction. It works now!!