Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.catalog.dashboard.client;

import java.util.Map;

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.Window;
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);
    public String uname;
    @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() {
        Map<Long, String> vendors = Utils.getAllVendors();      //do not delete this line it is done to prefetch the data
        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);
            }
        });
    }

    /**
     * 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) {
        uname = username;
        loginService.authenticateUser(username, password, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                        long role = Long.parseLong(result);
                    GWT.log(uname + " logged in catalog dashboard");
                    root.clear();
                    initMainDB(uname, role);
                    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, long role) {
        DockLayoutPanel dashboard = binder.createAndBindUi(this);
        itemActions.setItemDetails(itemDetails);
        itemList.setItemDetails(itemDetails);
        itemDetails.setCatalogDashboardPanel(this);
        topPanel.changeGreeting(username);
        if(role == Utils.ROLE_READONLY){
                itemActions.disableAllActions();
        }
        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();
                } else if(CatalogTree.SEARCH.equals(itemsType)) {
                    //Open the search dialog box
                    SearchItemBox box = new SearchItemBox(itemList);
                    box.center();
                } else if (CatalogTree.INACTIVE_ITEMS_WITH_INVENTORY.equals(itemsType)) {
                        String link = GWT.getHostPageBaseURL() + "inactivewithinventory/download";
                    Window.open(link, "_blank", "Inactive Items with Inventory");
                }else if (CatalogTree.BREAKEVEN_ITEMS.equals(itemsType)){
                        String link1 = GWT.getHostPageBaseURL() + "belowbreakeven/download";
                        Window.open(link1, "_blank", "Item below break even price.");
                }else if (CatalogTree.INVENTORY_OUTOFSYNC_ITEM_LIST.equals(itemsType)){
                        itemList.loadInventoryOutofSyncItems();
                        }
            }
        });
    }
    
    ItemList getItemListWidget() {
        return this.itemList;
    }
    
    ItemDetails getItemDetailsWidget() {
        return this.itemDetails;
    }
}