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

org.kie.uberfire.apps.client.home.AppsHomePresenter Maven / Gradle / Ivy

There is a newer version: 6.2.0.CR2
Show newest version
package org.kie.uberfire.apps.client.home;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import com.google.gwt.user.client.Window;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.ErrorCallback;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.kie.uberfire.apps.api.AppsPersistenceAPI;
import org.kie.uberfire.apps.api.Directory;
import org.kie.uberfire.apps.api.DirectoryBreadCrumb;
import org.uberfire.client.annotations.WorkbenchPartTitle;
import org.uberfire.client.annotations.WorkbenchPartView;
import org.uberfire.client.annotations.WorkbenchScreen;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.client.mvp.UberView;
import org.uberfire.lifecycle.OnOpen;
import org.uberfire.mvp.ParameterizedCommand;

@ApplicationScoped
@WorkbenchScreen(identifier = "AppsHomePresenter")
public class AppsHomePresenter {

    public interface View extends UberView {

        void setupBreadCrumbs( List breadcrumbs,
                               ParameterizedCommand breadCrumbAction );

        void setupAddDir( ParameterizedCommand clickCommand );

        void setupChildsDirectories( List childsDirectories,
                                     ParameterizedCommand clickCommand, ParameterizedCommand deleteCommand  );

        void clear();

        void setupChildComponents( List childComponents,
                                   ParameterizedCommand stringParameterizedCommand );
    }

    @Inject
    private View view;

    @Inject
    private PlaceManager placeManager;

    @Inject
    private Caller appService;

    private Directory currentDirectory;

    private Directory root;

    @PostConstruct
    public void init() {
    }

    @OnOpen
    public void loadContent() {
        view.clear();

        appService.call( new RemoteCallback() {
            public void callback( Directory root_ ) {
                root = root_;
                currentDirectory = root_;
                setupView();
            }
        }, new ErrorCallback() {
            @Override
            public boolean error( Object o,
                                  Throwable throwable ) {
                return false;
            }
        } ).getRootDirectory();
    }

    private ParameterizedCommand generateBreadCrumbViewCommand() {
        return new ParameterizedCommand() {
            @Override
            public void execute( String parameter ) {
                currentDirectory = searchForDirectory( parameter, root );
                setupView();
            }
        };
    }

    private Directory searchForDirectory( String parameter,
                                          Directory candidate ) {
        if ( candidate.getURI().equalsIgnoreCase( parameter ) ) {
            return candidate;
        }
        Directory target = null;
        for ( Directory directory : candidate.getChildsDirectories() ) {
            target =  searchForDirectory( parameter, directory );
            if ( target != null ) {
                break;
            }
        }
        return target;
    }


    private ParameterizedCommand generateDeleteDirectoryViewCommand() {
        return new ParameterizedCommand() {
            @Override
            public void execute( final String uri ) {
                appService.call( new RemoteCallback() {
                    public void callback( Boolean deleted ) {
                        currentDirectory.removeChildDirectoryByURI( uri );
                        view.clear();
                        view.setupChildsDirectories( currentDirectory.getChildsDirectories(), generateDirectoryViewCommand(), generateDeleteDirectoryViewCommand() );
                        view.setupChildComponents( currentDirectory.getChildComponents(), generateComponentViewCommand() );
                        view.setupAddDir( generateAddDirCommand() );
                    }
                }, new ErrorCallback() {
                    @Override
                    public boolean error( Object o,
                                          Throwable throwable ) {
                        return false;
                    }
                } ).deleteDirectory( uri );
            }
        };
    }


    private ParameterizedCommand generateDirectoryViewCommand() {
        return new ParameterizedCommand() {
            @Override
            public void execute( String parameter ) {
                for ( Directory candidate : currentDirectory.getChildsDirectories() ) {
                    if ( candidate.getURI().equalsIgnoreCase( parameter ) ) {
                        currentDirectory = candidate;
                        setupView();
                    }
                }
            }
        };
    }

    private void setupView() {
        view.clear();
        view.setupBreadCrumbs( DirectoryBreadCrumb.getBreadCrumbs( currentDirectory ), generateBreadCrumbViewCommand() );
        view.setupChildsDirectories( currentDirectory.getChildsDirectories(), generateDirectoryViewCommand(), generateDeleteDirectoryViewCommand() );
        view.setupChildComponents( currentDirectory.getChildComponents(), generateComponentViewCommand() );
        view.setupAddDir( generateAddDirCommand() );
    }

    private ParameterizedCommand generateComponentViewCommand() {

        return new ParameterizedCommand() {
            @Override
            public void execute( String parameter ) {
                placeManager.goTo( parameter );
            }
        };
    }

    private ParameterizedCommand generateAddDirCommand() {
        return new ParameterizedCommand() {

            @Override
            public void execute( final String directoryName ) {
                appService.call( new RemoteCallback() {
                    public void callback( Directory
                                                  newDir ) {

                        currentDirectory.addChildDirectory( newDir );
                        view.clear();
                        view.setupChildsDirectories( currentDirectory.getChildsDirectories(), generateDirectoryViewCommand(), generateDeleteDirectoryViewCommand() );
                        view.setupChildComponents( currentDirectory.getChildComponents(), generateComponentViewCommand() );
                        view.setupAddDir( generateAddDirCommand() );
                    }
                }, new ErrorCallback() {
                    @Override
                    public boolean error( Object o,
                                          Throwable throwable ) {
                        return false;
                    }
                } ).createDirectory( currentDirectory, directoryName );
            }
        };
    }

    @WorkbenchPartTitle
    public String getTitle() {
        return "Apps Home";
    }

    @WorkbenchPartView
    public UberView getView() {
        return view;
    }

}