Subversion Repositories SmartDukaan

Rev

Rev 2489 | Rev 3850 | 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 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.ListDataProvider;
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 int selectedRow = -1;
    
    private ItemDetails itemDetails;
    
    // Create a data provider.
    ListDataProvider<Item> dataProvider = new ListDataProvider<Item>();
    
    public ItemList() {
        initWidget(uiBinder.createAndBindUi(this));
        initItemList();
    }
    
    private void initItemList() {
        TextColumn<Item> idColumn = new TextColumn<Item>() {
            @Override
            public String getValue(Item item) {
                return item.getId() + "";
            }
        };
        
        TextColumn<Item> pgColumn = new TextColumn<Item>() {
            @Override
            public String getValue(Item item) {
                return item.getProductGroup();
            }
        };
        
        TextColumn<Item> brandColumn = new TextColumn<Item>(){
            @Override
            public String getValue(Item item) {
                return item.getBrand();
            }
        };
        
        TextColumn<Item> modelNumberColumn = new TextColumn<Item>(){
            @Override
            public String getValue(Item item) {
                return item.getModelNumber();
            }
        };
        
        TextColumn<Item> modelNameColumn = new TextColumn<Item>(){
            @Override
            public String getValue(Item item) {
                return item.getModelName();
            }
        };
        
        TextColumn<Item> colorColumn = new TextColumn<Item>(){
            @Override
            public String getValue(Item item) {
                return item.getColor();
            }
        };
        
        TextColumn<Item> categoryColumn = new TextColumn<Item>(){
            @Override
            public String getValue(Item item) {
                return item.getContentCategory()+"";
            }
        };

        // 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);

        //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();
    }
    
    private void updateItemDescriptionTable(List<Item> items){
        // Add the data to the data provider, which automatically pushes it to the
        // widget.
        List<Item> list = dataProvider.getList();
        list.clear();
        list.addAll(items);
        
        pager.firstPage();
    }
    
    /**
     * On click of an item in the list, a fresh call is made to the service to fetch item details, vendor prices
     * and vendor item keys. The item object is then passed to ItemDetails to set the values in UI fields 
     * @param event
     */
//    @UiHandler("itemDescriptionTable")
//    void onClick(ClickEvent event) {
//
//        Cell cell = itemDescriptionTable.getCellForEvent(event);
//        int newRowIndex = cell.getRowIndex();
//        selectRow(newRowIndex);
//        String itemId = itemDescriptionTable.getText(newRowIndex, INDEX_ID);
//        
//        catalogService.getItem(Long.parseLong(itemId), 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.");
//            }
//        });
//    }

    private void selectRow(int row) {
        //TODO: Change the style of the selected row
//        String style = selectionStyle.selectedRow();
//        if(selectedRow != -1){
//            itemDescriptionTable.getRowElement(row)owFormatter().removeStyleName(selectedRow, style);
//        }
//            
//        itemDescriptionTable.getRowFormatter().addStyleName(row, style);
//        selectedRow = row;
    }
    
    /* For testing
      private HashMap map;
      private List<Item> items;
      private void loadDummyItems() {
        Item i = new Item(1, "Handset", "Spice", "mi310", "phone", "White", "Business Phone",1, "comments", 1, 1, 
                "", 3000.50, 3000.00, 3000, 3000, 12, 12345, 12345, 12345, 12345, "ACTIVE", 1, "This item is active",
                null, "best", 2990, 1, true, true, null, null, null);
        List<Item> items = new ArrayList<Item>();
        items.add(i);
        this.items = items;
        //itemsMap.put(i.getId(), i);
    }*/
    
    
    public void loadAllItems() {
        catalogService.getAllItems(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not get all items...");
            }
            public void onSuccess(List<Item> result) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "All Items"); 
            }
        });
    }
    
    public void loadAllActiveItems() {
        catalogService.getAllActiveItems(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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Active Items");
            }
        });
    }
    
    public void loadAllPhasedOutItems() {
        catalogService.getAllPhasedOutItems(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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Phased Out Items");
            }
        });
    }
    
    public void loadAllPausedItems() {
        catalogService.getAllPausedItems(new AsyncCallback<List<Item>>() {
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Could not load paused items...");
            }
            public void onSuccess(List<Item> result) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Paused Items");
            }
        });
    }
    
    public void loadAllInProcessItems() {
        catalogService.getAllInProcessItems(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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "In Process Items");
            }
        });
    }
    
    public void loadAllContentCompleteItems() {
        catalogService.getAllContentCompleteItems(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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Content Complete Items");
            }
        });
    }
    
    public void loadAllRiskyItems() {
        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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Risky Items");
            }
        });
    }
    
    public void loadBestDeals() {
        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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Best Deals");
            }
        });
    }
    
    public void loadLatestArrivals() {
        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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Latest Arrivals");
            }
        });
    }
    
    public void loadBestSellers() {
        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) {
                updateItemDescriptionTable(result);
                currentlyShowing.setText(LEGEND + "Best Sellers");
            }
        });
    }

    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());
    }
}