Subversion Repositories SmartDukaan

Rev

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.Item;
import in.shop2020.catalog.dashboard.shared.ItemStatus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * List of items. List contains item Id, product group, brand, model number, model name, color and category
 * Dashboard user can select an item in this list and its details are shown in ItemDetails widget.
 *
 */
public class ItemList extends ResizeComposite{

    private static String LEGEND = "Currently Showing: ";
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
    
    interface ItemListUiBinder extends UiBinder<Widget, ItemList> { }
    private static final ItemListUiBinder uiBinder = GWT.create(ItemListUiBinder.class);
    
    interface SelectionStyle extends CssResource{
        String selectedRow();
        String alertsRow();
    }

    @UiField CellTable<Item> itemDescriptionTable;
    // Create paging controls.
    @UiField SimplePager pager = new SimplePager();

    @UiField SelectionStyle selectionStyle;
    @UiField Label currentlyShowing;
    
    private ItemDetails itemDetails;
    
    private String currentTreeItemSelection = null;
    
    private String searchText = "";
    
    private List<String> searchTerms = new ArrayList<String>();
    
    private TextColumn<Item> idColumn = new TextColumn<Item>() {
        @Override
        public String getValue(Item item) {
            return item.getId() + "";
        }
    };
    
    private TextColumn<Item> pgColumn = new TextColumn<Item>() {
        @Override
        public String getValue(Item item) {
            return item.getProductGroup();
        }
    };
    
    private TextColumn<Item> brandColumn = new TextColumn<Item>(){
        @Override
        public String getValue(Item item) {
            return item.getBrand();
        }
    };
    
    private TextColumn<Item> modelNumberColumn = new TextColumn<Item>(){
        @Override
        public String getValue(Item item) {
            return item.getModelNumber();
        }
    };
    
    private TextColumn<Item> modelNameColumn = new TextColumn<Item>(){
        @Override
        public String getValue(Item item) {
            return item.getModelName();
        }
    };
    
    private TextColumn<Item> colorColumn = new TextColumn<Item>(){
        @Override
        public String getValue(Item item) {
            return item.getColor();
        }
    };
    
    private TextColumn<Item> categoryColumn = new TextColumn<Item>(){
        @Override
        public String getValue(Item item) {
            return item.getContentCategory()+"";
        }
    };
    
    // Create a data provider.
    ListDataProvider<Item> dataProvider = new ListDataProvider<Item>();
    
    AsyncDataProvider<Item> asyncDataProvider = new AsyncDataProvider<Item>() {
        
        @Override
        protected void onRangeChanged(HasData<Item> display) {
            Range range = display.getVisibleRange();
            int start = range.getStart();
            int limit = range.getLength();
            if(currentTreeItemSelection == null)
                currentTreeItemSelection = CatalogTree.RISKY_ITEMS;
            if(currentTreeItemSelection.equals(CatalogTree.ALL_ITEMS))
                loadAllItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.ALL_ACTIVE_ITEMS))
                loadAllActiveItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.ALL_PAUSED_ITEMS))
                loadAllPausedItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.ALL_PHASED_OUT_ITEMS))
                loadAllPhasedOutItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.IN_PROCESS_ITEMS))
                loadAllInProcessItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.CONTENT_COMPLETE_ITEMS))
                loadAllContentCompleteItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.BEST_DEALS))
                loadBestDeals(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.BEST_SELLERS))
                loadBestSellers(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.LATEST_ARRIVALS))
                loadLatestArrivals(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.RISKY_ITEMS))
                loadAllRiskyItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.SEARCH))
                loadSearchItems(start, limit);
            else if(currentTreeItemSelection.equals(CatalogTree.INVENTORY_OUTOFSYNC_ITEM_LIST))
                loadInventoryOutofSyncItems(start, limit);
        
        }
    };
    
    public ItemList() {
        initWidget(uiBinder.createAndBindUi(this));
        initItemList();
    }

    protected void loadInventoryOutofSyncItems(final int offset, final int limit) {
        catalogService.getInventoryOutofSyncItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get Inventory Out of Sync Items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "Inventory Out-of-Sync Items");
            }
        });
        
        catalogService.getInventoryOutofSyncItemsCount(new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }


        private void initItemList() {
        // Add the columns.        
        itemDescriptionTable.addColumn(idColumn, "Item Id");
        itemDescriptionTable.addColumn(pgColumn, "Product Group");
        itemDescriptionTable.addColumn(brandColumn, "Brand");
        itemDescriptionTable.addColumn(modelNumberColumn, "Model Number");
        itemDescriptionTable.addColumn(modelNameColumn, "Model Name");
        itemDescriptionTable.addColumn(colorColumn, "Color");
        itemDescriptionTable.addColumn(categoryColumn, "Category");

        //Set the widths
        itemDescriptionTable.setWidth("100%");
        itemDescriptionTable.setColumnWidth(idColumn, 80.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(pgColumn, 128.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(brandColumn, 150.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(modelNumberColumn, 200.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(modelNameColumn, 200.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(colorColumn, 128.0, Unit.PX);
        itemDescriptionTable.setColumnWidth(categoryColumn, 220.0, Unit.PX);
        
        // Connect the table to the data provider.
        //dataProvider.addDataDisplay(itemDescriptionTable);
        asyncDataProvider.addDataDisplay(itemDescriptionTable);

        //Add paging support
        pager.setDisplay(itemDescriptionTable);
        
        // Add a selection model to handle item selection.
        final SingleSelectionModel<Item> selectionModel = new SingleSelectionModel<Item>();
        itemDescriptionTable.setSelectionModel(selectionModel);
        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            
            @Override
            public void onSelectionChange(SelectionChangeEvent event) {
                Item selectedItem = selectionModel.getSelectedObject();
                catalogService.getItem(selectedItem.getId(), new AsyncCallback<Item>() {
                    @Override
                    public void onSuccess(Item result) {
                        itemDetails.setItemDetails(result);
                    }
                    @Override
                    public void onFailure(Throwable caught) {
                        caught.printStackTrace();
                        Window.alert("Unable to fetch item details.");
                    }
                });
            }
        });

        loadAllRiskyItems();
    }
    
    public void loadAllItems() {
        currentTreeItemSelection = CatalogTree.ALL_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllActiveItems() {
        currentTreeItemSelection = CatalogTree.ALL_ACTIVE_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllPhasedOutItems() {
        currentTreeItemSelection = CatalogTree.ALL_PHASED_OUT_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllPausedItems() {
        currentTreeItemSelection = CatalogTree.ALL_PAUSED_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllInProcessItems() {
        currentTreeItemSelection = CatalogTree.IN_PROCESS_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllContentCompleteItems(){
        currentTreeItemSelection = CatalogTree.CONTENT_COMPLETE_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadAllRiskyItems() {
        currentTreeItemSelection = CatalogTree.RISKY_ITEMS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadBestDeals() {
        currentTreeItemSelection = CatalogTree.BEST_DEALS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadBestSellers() {
        currentTreeItemSelection = CatalogTree.BEST_SELLERS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void loadLatestArrivals() {
        currentTreeItemSelection = CatalogTree.LATEST_ARRIVALS;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    public void searchForItems(String searchText) {
        currentTreeItemSelection = CatalogTree.SEARCH;
        this.searchText = searchText.trim().replaceAll("\\s+", " ");
        searchTerms = Arrays.asList(this.searchText.split(" "));
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
    }
    
    private void updateItemDescriptionTableRowCount(Integer count){
        asyncDataProvider.updateRowCount(count, true);
    }
    
    private void updateAsyncItemDescriptionTable(int start, List<Item> items){
        // Add the data to the data provider, which automatically pushes it to the
        // widget.
        asyncDataProvider.updateRowData(start, items);
    }
    
    private void loadAllItems(final int offset, final int limit) {
        catalogService.getAllItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get all items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "All Items");
            }
        });
        
        catalogService.getItemCountByStatus(false, ItemStatus.ACTIVE, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllActiveItems(final int offset, final int limit) {
        catalogService.getAllActiveItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get all active items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "Active Items");
            }
        });
        
        catalogService.getItemCountByStatus(true, ItemStatus.ACTIVE, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllPhasedOutItems(final int offset, int limit){
        catalogService.getAllPhasedOutItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load phased out items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "Phased Out Items");
            }
        });
        
        catalogService.getItemCountByStatus(true, ItemStatus.PHASED_OUT, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllPausedItems(final int offset, int limit) {
        catalogService.getAllPausedItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load paused items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "Paused Items");
            }
        });
        
        catalogService.getItemCountByStatus(true, ItemStatus.PAUSED, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllInProcessItems(final int offset, int limit) {
        catalogService.getAllInProcessItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load IN_PROCESS items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "In Process Items");
            }
        });
        
        catalogService.getItemCountByStatus(true, ItemStatus.IN_PROCESS, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllContentCompleteItems(final int offset, int limit) {
        catalogService.getAllContentCompleteItems(offset, limit, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load CONTENT_COMPLETE items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(offset, result);
                currentlyShowing.setText(LEGEND + "Content Complete Items");
            }
        });
        
        catalogService.getItemCountByStatus(true, ItemStatus.CONTENT_COMPLETE, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    private void loadAllRiskyItems(final int start, final int limit) {
        catalogService.getRiskyItems(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load RISKY items...");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(start, result.subList(start, Math.min(start + limit, result.size())));
                updateItemDescriptionTableRowCount(result.size());
                currentTreeItemSelection = CatalogTree.RISKY_ITEMS;
                currentlyShowing.setText(LEGEND + "Risky Items");
            }
        });
    }

    private void loadBestDeals(final int start, final int limit) {
        catalogService.getBestDeals(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load best deals.");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(start, result.subList(start, Math.min(start + limit, result.size())));
                updateItemDescriptionTableRowCount(result.size());
                currentlyShowing.setText(LEGEND + "Best Deals");
            }
        });
    }

    private void loadBestSellers(final int start, final int limit) {
        catalogService.getBestSellers(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load best sellers.");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(start, result.subList(start, Math.min(start + limit, result.size())));
                updateItemDescriptionTableRowCount(result.size());
                currentlyShowing.setText(LEGEND + "Best Sellers");
            }
        });
    }

    private void loadLatestArrivals(final int start, final int limit) {
        catalogService.getLatestArrivals(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load latest arrivals.");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(start, result.subList(start, Math.min(start + limit, result.size())));
                updateItemDescriptionTableRowCount(result.size());
                currentlyShowing.setText(LEGEND + "Latest Arrivals");
            }
        });
        
    }

    private void loadSearchItems(final int start, final int limit) {
        catalogService.searchItems(start, limit, searchTerms, new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load the search results.");
            }
            public void onSuccess(List<Item> result) {
                updateAsyncItemDescriptionTable(start, result);
                currentlyShowing.setText(LEGEND + "Search results for " + searchText);
            }
        });
        
        
        catalogService.getSearchResultCount(searchTerms, new AsyncCallback<Integer>() {

            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get the count of items...");
            }

            @Override
            public void onSuccess(Integer count) {
                updateItemDescriptionTableRowCount(count);
            }
        });
    }
    
    public void setItemDetails(ItemDetails itemDetails) {
        this.itemDetails = itemDetails;
    }
    
    /**
     * This method is called when item is updated in ItemDetails.java to update 
     * attributes in the list also.
     * @param item
     */
    public void updateItem(Item item) {
        //TODO: Update the item in the list when its details are updated
//        itemDescriptionTable.setText(selectedRow, INDEX_PRODUCT_GROUP, item.getProductGroup());
//        itemDescriptionTable.setText(selectedRow, INDEX_BRAND, item.getBrand());
//        itemDescriptionTable.setText(selectedRow, INDEX_MODEL_NUMBER, item.getModelNumber());
//        itemDescriptionTable.setText(selectedRow, INDEX_MODEL_NAME, item.getModelName());
//        itemDescriptionTable.setText(selectedRow, INDEX_COLOR, item.getColor());
    }

        public void loadInventoryOutofSyncItems() {
                currentTreeItemSelection = CatalogTree.INVENTORY_OUTOFSYNC_ITEM_LIST;
        itemDescriptionTable.setVisibleRangeAndClearData(new Range(0, pager.getPageSize()), true);
        
        }
}