Subversion Repositories SmartDukaan

Rev

Rev 2427 | Rev 3872 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.catalog.dashboard.client;

import in.shop2020.catalog.dashboard.shared.Utils;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * This is the entry point of dashboard application.
 * 
 */
public class CatalogDashboard implements EntryPoint {

    @UiTemplate("CatalogDashboard.ui.xml")
    interface CatalogBinder extends UiBinder<DockLayoutPanel, CatalogDashboard> {
    }
    
    private static final CatalogBinder binder = GWT.create(CatalogBinder.class);
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);
    
    @UiField TopPanel topPanel;        // logo, welcome message and sign out link
    @UiField Shortcuts shortcuts;      // anchors to filter item list based on status 
    //TODO @UiField Filters filters;
    @UiField ItemList itemList;        // list of items
    @UiField ItemDetails itemDetails;  // details of an item selected in the list
    @UiField ItemActions itemActions;  // action buttons to update, add item or change item status

    RootLayoutPanel root;
    LoginScreen login;

    public void onModuleLoad() {
        root = RootLayoutPanel.get();
        root.getElement().setScrollLeft(10);
        login = new LoginScreen();
        //initMainDB("Testing");
        root.add(login);
        login.setLoginListener(new LoginScreen.LoginListener() {
            @Override
            public void onLoginPressed(String username, String password) {
                authenticateUser(username, password, Utils.ROLE_STAGING);
            }
        });
    }

    /**
     * Authenticates username and password and if successful, initialises 
     * main dashboard panel.
     * @param username 
     * @param password
     * @param role -- Utils.ROLE_STAGING (Login role for staging), another role 
     * is Utils.ROLE_PRODUCTION (for push to production username & password)
     */
    private void authenticateUser(String username, String password, int role) {
        final String uname = username;
        loginService.authenticateUser(username, password, role, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    GWT.log(result + " logged in catalog dashboard");
                    root.clear();
                    initMainDB(uname);
                    login.clearFields();
                } else {
                    login.setErrorText("Invalid username/password");    
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                login.setErrorText("Error in authentication");
            }
        });
    }

    private void initMainDB(String username) {
        DockLayoutPanel dashboard = binder.createAndBindUi(this);
        itemActions.setItemDetails(itemDetails);
        itemList.setItemDetails(itemDetails);
        itemDetails.setCatalogDashboardPanel(this);
        topPanel.changeGreeting(username);

        topPanel.getSignOutLink().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                root.clear();
                root.add(login);
            }
        });
        root.clear();
        root.add(dashboard);
        
        /**
         * listener for anchors to filter item list based on statuses and some other parameters.
         * On click event, string of the anchor is passed as argument and the same listener is
         * taking different actions based on the different string arguments.
         */
        shortcuts.getCatalogTree().setTreeListener(new CatalogTree.TreeListener() {
            @Override
            public void onTreeItemClicked(String itemsType) {
                if(CatalogTree.ALL_ITEMS.equals(itemsType)) {
                    itemList.loadAllItems();
                } else if(CatalogTree.ALL_PHASED_OUT_ITEMS.equals(itemsType)) { 
                    itemList.loadAllPhasedOutItems();
                } else if(CatalogTree.ALL_PAUSED_ITEMS.equals(itemsType)) { 
                    itemList.loadAllPausedItems();
                } else if(CatalogTree.ALL_ACTIVE_ITEMS.equals(itemsType)) { 
                    itemList.loadAllActiveItems();
                } else if(CatalogTree.IN_PROCESS_ITEMS.equals(itemsType)) { 
                    itemList.loadAllInProcessItems();
                } else if(CatalogTree.CONTENT_COMPLETE_ITEMS.equals(itemsType)) { 
                    itemList.loadAllContentCompleteItems();
                } else if(CatalogTree.RISKY_ITEMS.equals(itemsType)) { 
                    itemList.loadAllRiskyItems();
                } else if(CatalogTree.BEST_DEALS.equals(itemsType)) {
                    itemList.loadBestDeals();
                } else if(CatalogTree.BEST_SELLERS.equals(itemsType)) {
                    itemList.loadBestSellers();
                } else if(CatalogTree.LATEST_ARRIVALS.equals(itemsType)) {
                    itemList.loadLatestArrivals();
                }
            }
        });
    }
    
    ItemList getItemListWidget() {
        return this.itemList;
    }
    
    ItemDetails getItemDetailsWidget() {
        return this.itemDetails;
    }
}