Subversion Repositories SmartDukaan

Rev

Rev 2359 | Rev 2489 | 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.event.dom.client.ClickEvent;
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.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;

/**
 * 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 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 FlexTable header;
    @UiField FlexTable itemDescriptionTable;
    @UiField SelectionStyle selectionStyle;
    private int selectedRow = -1;
    
    private ItemDetails itemDetails;
    
    public ItemList() {
        initWidget(uiBinder.createAndBindUi(this));
        initHeader();
        initItemList();
    }
    
    private void initItemList() {
        loadAllRiskyItems();
        
        /*For testing
        loadDummyItems();
        updateItemDescriptionTable(items);
        */
    }
    
    /**
     * Initialise header of item list table.
     */
    private void initHeader(){
        header.getColumnFormatter().setWidth(0, "80px");
        header.getColumnFormatter().setWidth(1, "128px");
        header.getColumnFormatter().setWidth(2, "150px");
        header.getColumnFormatter().setWidth(3, "200px");
        header.getColumnFormatter().setWidth(4, "200px");
        header.getColumnFormatter().setWidth(5, "128px");
        header.getColumnFormatter().setWidth(6, "220px");

        header.setText(0, 0, "Item Id");
        header.setText(0, 1, "Product Group");
        header.setText(0, 2, "Brand");
        header.setText(0, 3, "Model Number");
        header.setText(0, 4, "Model Name");
        header.setText(0, 5, "Color");
        header.setText(0, 6, "Category");
    }
    
    private void updateItemDescriptionTable(List<Item> items){
        itemDescriptionTable.removeAllRows();
        itemDescriptionTable.getColumnFormatter().setWidth(0, "80px");
        itemDescriptionTable.getColumnFormatter().setWidth(1, "128px");
        itemDescriptionTable.getColumnFormatter().setWidth(2, "150px");
        itemDescriptionTable.getColumnFormatter().setWidth(3, "200px");
        itemDescriptionTable.getColumnFormatter().setWidth(4, "200px");
        itemDescriptionTable.getColumnFormatter().setWidth(5, "128px");
        itemDescriptionTable.getColumnFormatter().setWidth(6, "220px");
        
        int i=0;
        for(final Item item : items){
            int col = 0;
            itemDescriptionTable.setText(i, col++, item.getId() + "");
            itemDescriptionTable.setText(i, col++, item.getProductGroup());
            itemDescriptionTable.setText(i, col++, item.getBrand());
            itemDescriptionTable.setText(i, col++, item.getModelNumber());
            itemDescriptionTable.setText(i, col++, item.getModelName());
            itemDescriptionTable.setText(i, col++, item.getColor());
            itemDescriptionTable.setText(i, col++, item.getContentCategory()+"");
            i++;
        }
    }
    
    /**
     * 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, 0);
        
        catalogService.getItem(Long.parseLong(itemId), new AsyncCallback<Item>() {
            @Override
            public void onSuccess(Item result) {
                itemDetails.setItemDetails(result);
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Unable to fetch item details.");
            }
        });
    }

    private void selectRow(int row) {
        String style = selectionStyle.selectedRow();
        if(selectedRow != -1){
            itemDescriptionTable.getRowFormatter().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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }
    
    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);
            }
        });
    }

    public void setItemDetails(ItemDetails itemDetails) {
        this.itemDetails = itemDetails;
    }
}