Subversion Repositories SmartDukaan

Rev

Rev 2126 | Rev 2252 | 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.Utils;
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
import in.shop2020.catalog.dashboard.shared.VendorPricings;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

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.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.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class ItemDetails extends ResizeComposite {

    private final int TABLE_INDEX_VENDORID = 0, 
                      TABLE_INDEX_VENDOR_DESC = 1, 
                      TABLE_INDEX_ITEM_KEY = 2,
                      TABLE_INDEX_MOP = 2,
                      TABLE_INDEX_DP = 3,
                      TABLE_INDEX_TP = 4;
    private final int TABLE_INDEX_WAREHOUSE_ID = 0,
                      TABLE_INDEX_WAREHOUSE_DESC = 1,
                      TABLE_INDEX_WAREHOUSE_INV = 2;

    interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
    private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);


    interface ItemDetailStyle extends CssResource{
        String greenLabel();
        String fieldChanged();
    }

    private Item item, newItem;

    @UiField ItemDetailStyle style;
    @UiField Label itemId;
    @UiField TextBox productGroup, brand, modelNumber, modelName, color;
    @UiField Label contentCategory, catalogItemId;
    @UiField TextBox comments;
    @UiField TextBox sellingPrice, mrp, weight;
    @UiField Label addedOn, retireDate, updatedOn;
    @UiField Label itemStatus;
    @UiField TextBox bestDealsText, bestDealsValue; 
    @UiField FlexTable headerAvailability, availabilityTable;
    @UiField FlexTable headerVendorM, vendorTableM;
    @UiField FlexTable headerVendor, vendorTable;
    @UiField TextBox bestSellingRank;
    @UiField CheckBox defaultForEntity;
    //@UiField Button submit;
    @UiField DateBox startDate;

    public ItemDetails(Item item){
        this();
        setItemDetails(item);
    }

    public ItemDetails() {
        initWidget(uiBinder.createAndBindUi(this));
        initAvailabilityHeader();
        initMappingHeader();
        initPricingHeader();
        
        headerAvailability.setVisible(false);
        availabilityTable.setVisible(false);
    }

    public void setItemDetails(Item item){
        this.item = item;
        itemId.setText(item.getId()+"");
        productGroup.setText(item.getProductGroup());
        brand.setText(item.getBrand());
        modelNumber.setText(item.getModelNumber());
        modelName.setText(item.getModelName());
        color.setText(item.getColor());

        contentCategory.setText(item.getContentCategory()+"");
        comments.setText(item.getComments());
        catalogItemId.setText(item.getCatalogItemId() + "");

        mrp.setText(item.getMrp() != -1 ? item.getMrp()+"" : "");
        sellingPrice.setText(item.getSellingPrice() != -1 ? item.getSellingPrice()+"" : "");
        weight.setText(item.getWeight() != -1 ? item.getWeight()+"" : "");
        
        startDate.setValue(new Date(item.getStartDate()));
        addedOn.setText(Utils.getDisplayableDate(item.getAddedOn()));
        retireDate.setText(Utils.getDisplayableDate(item.getRetireDate()));
        updatedOn.setText(Utils.getDisplayableDate(item.getUpdatedOn()));

        bestDealsText.setText(item.getBestDealsText());
        bestDealsValue.setText(item.getBestDealsValue() != -1 ? item.getBestDealsValue()+"" : "");
        bestSellingRank.setText(item.getBestSellingRank() != -1 ? item.getBestSellingRank()+"" : "");
        defaultForEntity.setValue(item.isDefaultForEntity());
        
        itemStatus.setText(item.getItemStatus());

        updateAvailabilityTable(item.getAvailability());
        updateMappingTable(item.getVendorMappingsMap());
        updatePricingTable(item.getVendorPricesMap());
    }

    private void initAvailabilityHeader(){
        // Initialize the header.
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
        
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_ID, "Warehouse Id");
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_DESC, "Warehouse Desc");
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INV, "Availability");

    }

    private void initMappingHeader(){
        // Initialize the header.
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");

        headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
        headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
        headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
        
        Button addButton = new Button("Add");
        headerVendorM.setWidget(0, TABLE_INDEX_ITEM_KEY + 1, addButton);
        addButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
                        modelNumber.getText().trim(), color.getText().trim());
                vendorMappingDialog.updateButton.setText("Add");
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
                    @Override
                    public boolean onUpdate(String key, long vendorId) {
                        int row = vendorTableM.getRowCount();
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
                        
                        vendorTableM.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
                        vendorTableM.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
                        vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, key);
                        return true;
                    }
                });
                vendorMappingDialog.show();
            }
        });
    }

    
    private void initPricingHeader(){
        // Initialize the header.
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");

        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
        headerVendor.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
        
        Button addButton = new Button("Add");
        headerVendor.setWidget(0, TABLE_INDEX_TP + 1, addButton);
        addButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
                vendorPricesDialog.updateButton.setText("Add");
                vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
                    @Override
                    public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
                        if(!vendorExists(vendorId)) {
                            Window.alert("Vendor already exists");
                            return false;
                        }
                        /*if(!validateVendorPrices(mop, dp, tp)) {
                            return false;
                        }*/
                        int row = vendorTable.getRowCount();
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
                        
                        vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
                        vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
                        vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
                        vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
                        vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
                        return true;
                    }
                });
                vendorPricesDialog.show();
            }
        });
    }
    
    private void updateAvailabilityTable(Map<Long, Long> availabilityMap){
        availabilityTable.removeAllRows();
        if(availabilityMap == null) {
            return;
        }
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");


        int i=0;
        for(Entry<Long, Long> availability : availabilityMap.entrySet()){
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_ID, availability.getKey() + "");
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_DESC, Utils.getWarehouseDesc(availability.getKey()));
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INV, availability.getValue() + "");
            i++;
        }
    }
    
    private void updateMappingTable(Map<Long, VendorItemMapping> vendorMappingsMap){
        vendorTableM.removeAllRows();
        
        if(vendorMappingsMap == null || vendorMappingsMap.isEmpty()) {
            return;
        }
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");

        int i=0;
        for(VendorItemMapping vendorMapping : vendorMappingsMap.values()){
            vendorTableM.setText(i, TABLE_INDEX_VENDORID, vendorMapping.getVendorId() + "");
            vendorTableM.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorMapping.getVendorId()));
            vendorTableM.setText(i, TABLE_INDEX_ITEM_KEY, vendorMapping.getItemKey());
            Button editButton = new Button("Edit");
            vendorTableM.setWidget(i, TABLE_INDEX_ITEM_KEY + 1, editButton);
            i++;
            editButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    Cell cell = vendorTableM.getCellForEvent(event);
                    int row = cell.getRowIndex();
                    long vendorId = Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID));
                    updateVendorMapping(vendorId, row);
                }
            });
        }
    }

    private void updatePricingTable(Map<Long, VendorPricings> vendorPricingMap){
        vendorTable.removeAllRows();
        
        if(vendorPricingMap == null || vendorPricingMap.isEmpty()) {
            return;
        }
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");


        int i=0;
        for(VendorPricings vendorDetail : vendorPricingMap.values()){
            vendorTable.setText(i, TABLE_INDEX_VENDORID, vendorDetail.getVendorId() + "");
            vendorTable.setText(i, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorDetail.getVendorId()));
            vendorTable.setText(i, TABLE_INDEX_MOP, vendorDetail.getMop() + "");
            vendorTable.setText(i, TABLE_INDEX_DP, vendorDetail.getDealerPrice() + "");
            vendorTable.setText(i, TABLE_INDEX_TP, vendorDetail.getTransferPrice() + "");
            Button editButton = new Button("Edit");
            vendorTable.setWidget(i, TABLE_INDEX_TP + 1, editButton);
            i++;
            editButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    Cell cell = vendorTable.getCellForEvent(event);
                    int row = cell.getRowIndex();
                    long vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
                    updateVendorPrices(vendorId, row);
                }
            });
        }
    }

    
    void updateItem() {
        if(item == null) {
            Window.alert("Please select an item to update.");
            return;
        }
        try {
            if(!createNewItem()) {
                return;
            }
            if(!isItemChanged()) {
                Window.alert("Nothing to update. Please change intended item parameters and try again.");
                return;
            }
        } catch(NumberFormatException ex) {
            ex.printStackTrace();
            GWT.log("Number format exception");
        }
        if(!Utils.validateItem(newItem)) {
            return;
        }
        
        /*if(!validatePrices()) {
            return;
        }*/
        //updateItem();  -- Calling this method above. A new item is created with updated data.
        catalogService.updateItem(newItem, new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if(result) {
                    item = newItem;
                    GWT.log("Item updated. Id = " + item.getId());
                    Window.alert("Item updated successfully.");
                }
                else {
                    GWT.log("Error updating item");
                    Window.alert("Error updating item");
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
                Window.alert("Error while updating item");
            }
        });
    }
    
    private boolean createNewItem() {
        newItem = new Item();
        newItem.setId(Long.parseLong(itemId.getText()));
        newItem.setProductGroup(productGroup.getText().trim());
        newItem.setBrand(brand.getText().trim());
        newItem.setModelNumber(modelNumber.getText().trim());
        newItem.setModelName(modelName.getText().trim());
        newItem.setColor(color.getText().trim());
        newItem.setContentCategory(contentCategory.getText());
        newItem.setComments(comments.getText().trim());
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
        
        try {
            if(!mrp.getText().trim().isEmpty()) {
                double mrpValue = Double.parseDouble(mrp.getText().trim());
                if(mrpValue <= 0) {
                    throw new NumberFormatException("Negative value of MRP");
                }
                newItem.setMrp(mrpValue);
            } else {
                newItem.setMrp(-1);
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
            return false;
        }
        try {
            if(!sellingPrice.getText().trim().isEmpty()) {
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
            if(spValue <= 0) {
                throw new NumberFormatException("Negative value of Selling price");
            }
            newItem.setSellingPrice(spValue);
            } else {
                newItem.setSellingPrice(-1);
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
            return false;
        }
        try {
            if(!weight.getText().trim().isEmpty()) {
                double wtValue = Double.parseDouble(weight.getText().trim());
                if(wtValue <= 0) {
                    throw new NumberFormatException("Negative value of Weight");
                }
                newItem.setWeight(wtValue);
            } else {
                newItem.setWeight(-1);
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
            return false;
        }
        try {
            if(!startDate.getTextBox().getText().trim().equals("")) {
                newItem.setStartDate(startDate.getValue().getTime());
            }
        } catch(Exception ex) {
            Window.alert("Invalid start date format");
            return false;
        }
        newItem.setBestDealsText(bestDealsText.getText().trim());
        try {
            if(!bestDealsValue.getText().trim().equals("")) {
                double bdValue = Double.parseDouble(bestDealsValue.getText().trim());
                if(bdValue < 0) {
                    throw new NumberFormatException("Negative value of BestDealValue");
                }
                newItem.setBestDealsValue(bdValue);
            } else {
                newItem.setBestDealsValue(-1);
            }
        } catch(NumberFormatException ex) {
            Window.alert("Invalid best deal value format");
            return false;
        }
        try {
            if(!bestSellingRank.getText().trim().equals("")) {
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
                if(bsrValue < 0) {
                    throw new NumberFormatException("Negative value of Best Selling Rank");
                }
                newItem.setBestSellingRank(bsrValue);
            } else {
                newItem.setBestSellingRank(-1);
            }
            
        } catch(NumberFormatException ex) {
            Window.alert("Invalid best selling rank format");
            return false;
        }
        newItem.setDefaultForEntity(defaultForEntity.getValue());
        
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
          Add the instance to map and set the map to the item instance created above.*/
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
        VendorPricings v;
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
            v = new VendorPricings();
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
            vendorPrices.put(v.getVendorId(), v);
            newItem.setMop(v.getMop());
            newItem.setDealerPrice(v.getDealerPrice());
            newItem.setTransferPrice(v.getTransferPrice());
        }
        newItem.setVendorPricesMap(vendorPrices);
        
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
        Add the instance to map and set the map to the item instance created above.*/
        Map<Long, VendorItemMapping> vendorMappings = new HashMap<Long, VendorItemMapping>();
        VendorItemMapping vMapping;
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
            vMapping = new VendorItemMapping();
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
            vendorMappings.put(vMapping.getVendorId(), vMapping);
        }
        newItem.setVendorMappingsMap(vendorMappings);
        
        newItem.setContentCategoryId(item.getContentCategoryId());
        newItem.setFeatureId(item.getFeatureId());
        newItem.setFeatureDescription(item.getFeatureDescription());
        newItem.setAddedOn(item.getAddedOn());
        newItem.setRetireDate(item.getRetireDate());
        newItem.setUpdatedOn(item.getUpdatedOn());
        newItem.setItemStatus(item.getItemStatus());
        newItem.setOtherInfo(item.getOtherInfo());
        newItem.setAvailability(item.getAvailability());
        
        return true;
    }

    private void updateVendorPrices(final long vendorId, final int row) {
        String mop = vendorTable.getText(row, TABLE_INDEX_MOP);
        String dp = vendorTable.getText(row, TABLE_INDEX_DP);
        String tp = vendorTable.getText(row, TABLE_INDEX_TP);
        String key = vendorTable.getText(row, TABLE_INDEX_ITEM_KEY);
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
        pricesDialog.updateButton.setText("Update");
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
            @Override
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
                if(!validateVendorPrices(mop, dp, tp)) {
                    return false;
                }
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
                return true;
            }
        });
        pricesDialog.show();
    }
    
    private void updateVendorMapping(final long vendorId, final int row) {
        String key = vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY);
        VendorMappingDialog mappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
                modelNumber.getText().trim(), color.getText().trim(), key);
        mappingDialog.updateButton.setText("Update");
        mappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
            @Override
            public boolean onUpdate(String itemKey, long vendorId) {
                if(itemKey == null || itemKey.equals("")) {
                    Window.alert("Item key cannot be empty.");
                    return false;
                }
                vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, itemKey);
                return true;
            }
        });
        mappingDialog.show();
    }

    private boolean isItemChanged() {
        if(!productGroup.getText().trim().equals(item.getProductGroup())) {
            return true;
        }
        if(!brand.getText().trim().equals(item.getBrand())) {
            return true;
        }
        if(!modelNumber.getText().trim().equals(item.getModelNumber())) {
            return true;
        }
        if(!modelName.getText().trim().equals(item.getModelName())) {
            return true;
        }
        if(!color.getText().trim().equals(item.getColor())) {
            return true;
        }
        if(!comments.getText().trim().equals(item.getComments())) {
            return true;
        }
        if(newItem.getMrp() != item.getMrp()) {
            return true;
        }
        if(newItem.getSellingPrice() != item.getSellingPrice()) {
            return true;
        }
        if(newItem.getWeight() != item.getWeight()) {
            return true;
        }
        if(!bestDealsText.getText().trim().equals(item.getBestDealsText())) {
            return true;
        }
        if(newItem.getBestDealsValue() != item.getBestDealsValue()) {
            return true;
        }
        if(newItem.getBestSellingRank() != item.getBestSellingRank()) {
            return true;
        }
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
            return true;
        }
        if(newItem.getStartDate() != item.getStartDate()) {
            return true;
        }
        VendorPricings vendorPricings;
        long vendorId;
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
            vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID));
            vendorPricings = item.getVendorPricesMap().get(vendorId);
            if(vendorPricings.getMop() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP))) {
                return true;
            }
            if(vendorPricings.getDealerPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP))) {
                return true;
            }
            if(vendorPricings.getTransferPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP))) {
                return true;
            }
        }
        return false;
    }
    
    private boolean validatePrices() {
        if(newItem.getSellingPrice() > newItem.getMrp()) {
            Window.alert("Selling price cannot be more than MRP");
            return false;
        }
        for(VendorPricings v : newItem.getVendorPricesMap().values()) {
            if(newItem.getMrp() < v.getMop()) {
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
                return false;
            }
            if(v.getTransferPrice() > v.getMop()) {
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
                return false;
            }
        }
        return true;
    }
    
    private boolean validateVendorPrices(double mop, double dp, double tp) {
        if(item.getMrp() != -1 && item.getMrp() < mop) {
            Window.alert("MOP cannot be more than MRP.");
            return false;
        }
        if(tp > mop) {
            Window.alert("Transfer Price cannot be more than MOP.");
            return false;
        }
        return true;
    }
    
    public long getItemId() {
        return item == null ? 0 : item.getId();
    }
    
    public Item getItem() {
        return item;
    }
    
    private boolean vendorExists(long vendorId) {
        long id;
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
            if(vendorId == id) {
                return false;
            }
        }
        return true;
    }
}