Subversion Repositories SmartDukaan

Rev

Rev 3921 | Rev 4762 | 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.Entry;

import in.shop2020.catalog.dashboard.shared.Item;
import in.shop2020.catalog.dashboard.shared.Utils;

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.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.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Panel containing buttons for different actions on item.
 * e.g. update item, add new item, change item status, mark/unmark risky, push prices to production etc.
 * @author ankur
 *
 */
public class ItemActions extends ResizeComposite{

    interface ItemActionsUiBinder extends UiBinder<Widget, ItemActions> {}
    private static ItemActionsUiBinder uiBinder = GWT.create(ItemActionsUiBinder.class);
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
    
    private ItemDetails itemDetails;
    
    @UiField Button addItem, updateItem, pushItemToProd, phaseoutItem, activateItem, markAliveItem, pauseItem;
    @UiField Button markRisky, unmarkRisky;
    @UiField Button generateMasterSheet;
    
    public ItemActions(){
        initWidget(uiBinder.createAndBindUi(this));
        //pushItemToProd.setEnabled(false);
    }
 
    
    @UiHandler("updateItem")
    void updateItem(ClickEvent event) {
        itemDetails.updateItem();
    }
    
    /**
     * creates a new form to fill up item details and displays the form.
     * @param event
     */
    @UiHandler("addItem")
    void addItem(ClickEvent event) {
        ItemForm itemForm = new ItemForm();
        itemForm.center();
        itemForm.show();
    }
    
    @UiHandler("phaseoutItem")
    void phaseoutItem(ClickEvent event) {
        final long itemId = validateStatusChange(Utils.PHASED_OUT);
        if(itemId <= 0) {
            return;
        }
        catalogService.phaseoutItem(itemId, new AsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                Window.alert("Item marked as PHASED OUT");
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while marking item " + itemId + " PHASED OUT");
            }
        });
    }
    
    @UiHandler("activateItem")
    void activateItem(ClickEvent event) {
        final long itemId = validateStatusChange(Utils.ACTIVE);
        if(itemId <= 0) {
            return;
        }
        catalogService.activateItem(itemId, new AsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                Window.alert("Item marked as ACTIVE");
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while marking item " + itemId + " ACTIVE");
            }
        });
    }

    @UiHandler("pauseItem")
    void pauseItem(ClickEvent event) {
        final long itemId = validateStatusChange(Utils.PAUSED);
        if(itemId <= 0) {
            return;
        }
        catalogService.pauseItem(itemId, new AsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                Window.alert("Item marked as PAUSED");
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while marking item " + itemId + " PAUSED");
            }
        });
    }
    
    @UiHandler("markAliveItem")
    void markAsAlive(ClickEvent event) {
        final long itemId = validateStatusChange(Utils.IN_PROCESS);
        if(itemId <= 0) {
            return;
        }
        catalogService.markInProcess(itemId, new AsyncCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                Window.alert("Item marked as IN PROCESS");
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while marking item " + itemId + " IN PROCESS");
            }
        });
    }
    
    /**
     * Creates and pops up authentication dialog for push to production username and password
     * @param event
     */
    @UiHandler("pushItemToProd")
    void pushItemToProduction(ClickEvent event) {
        if(itemDetails.getItem() == null) {
            Window.alert("Please select an item to update");
            return;
        }
        AuthenticateDialog authDialog = new AuthenticateDialog(itemDetails.getItem());
        authDialog.show();
    }
    
    @UiHandler("markRisky")
    void markRisky(ClickEvent event) {
        changeRiskyFlag(true);
    }
    
    @UiHandler("unmarkRisky")
    void unmarkRisky(ClickEvent event) {
        changeRiskyFlag(false);
    }
    
    @UiHandler("generateMasterSheet")
    void generateMasterSheet(ClickEvent event)  {
        createDialog();
    }

    /**
     * Creates and pops up  dialog box containing following options. 
     * Categories: 1) Handsets 2) Accessories
     * Vendors: 1) Hotspot 2) Tulip 3) Sarvottam
     * Also creates submit and cancel buttons and add click event listeners to them.
     * On submit button click, request is sent to <code>in.shop2020.catalog.dashboard.server.FileDownloadServlet<code>
     */
    private void createDialog() {
        VerticalPanel vp = new VerticalPanel();
        final DialogBox db = new DialogBox();
        db.setText("Master Sheet Download");
        
        final Label categoryLabel = new Label("Category");
        vp.add(categoryLabel);
        
        final RadioButton handsetsRB = new RadioButton("vendorCategory", "Handsets");
        final RadioButton accRB = new RadioButton("vendorCategory", "Accessories");
        handsetsRB.setValue(true);
        
        HorizontalPanel hpRB = new HorizontalPanel();
        hpRB.add(handsetsRB);
        hpRB.add(accRB);
        vp.add(hpRB);
        
        final Label vendorLabel = new Label("Vendor");
        vp.add(vendorLabel);
        
        final ListBox vendor = new ListBox();
        for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
            vendor.addItem(e.getValue());
        }
        
        vp.add(vendor);
        
        Button submitButton = new Button("OK");
        Button cancelButton = new Button("Cancel");
        HorizontalPanel hpButtons = new HorizontalPanel();
        hpButtons.setSpacing(5);
        hpButtons.add(submitButton);
        hpButtons.add(cancelButton);
        
        vp.add(hpButtons);
        
        db.add(vp);
        submitButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                String vendorCategory;
                if(handsetsRB.getValue()) {
                    vendorCategory = "Handsets";
                } else {
                    vendorCategory = "Accessories";
                }
                
                long vendorId = Utils.getVendorId(vendor.getItemText(vendor.getSelectedIndex()));
                
                // Servlet mapping mastersheet/download is mapped to in.shop2020.catalog.dashboard.server.FileDownloadServlet
                String link = GWT.getHostPageBaseURL() + "mastersheet/download?vendorCategory=" + vendorCategory + "&vendorId=" + vendorId;
                Window.open(link, "_self", "Master Sheet Download");
                db.hide();
            }
        });
        cancelButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                db.hide();
            }
        });
        db.center();
        db.setAnimationEnabled(true);
        db.show();
    }
    
    /**
     * validates and then calls service method changeItemRiskyFlag to change risky flag.
     * @param risky
     */
    private void changeRiskyFlag(boolean risky) {
        Item item = itemDetails.getItem();
        if(item == null) {
            Window.alert("Please select an item to mark/unmark as risky");
            return;
        }
        if(risky && item.isRisky()) {
            Window.alert("Item already marked as risky");
            return;
        }
        if(!risky && !item.isRisky()) {
            Window.alert("Item not marked as risky");
            return;
        }
        catalogService.changeItemRiskyFlag(item.getId(), risky, new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if(result) {
                    Window.alert("Done!");
                } else {
                    Window.alert("Error while marking/unmarking risky flag on staging/production server.");
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error");
            }
        });
    }
    
    public void setItemDetails(ItemDetails itemDetails) {
        this.itemDetails = itemDetails;
    }

    /**
     * Validate item status change.
     * @param toStatus
     * @return item Id if validation is successful else -1
     */
    private long validateStatusChange(int toStatus) {
        final Item item = itemDetails.getItem();
        if(item == null) {
            Window.alert(getAlertString(toStatus)[0]);
            return -1;
        }
        if(!Utils.validateStatusChange(item.getItemStatusValue(), toStatus)) {
            Window.alert(getAlertString(toStatus)[1]);
            return -1;
        }
        return item.getId();
    }
    
    /**
     * @param toStatus
     * @return messages to be show to the user if item status change validation fails.
     *       <br>-first message will be shown if no item is selected in the list
     *       <br>-second message is shown if validation for FromStatus to ToStatus fails.
     */
    private String[] getAlertString(int toStatus) {
        String[] alertStr = {"", ""};
        switch(toStatus) {
        case Utils.PAUSED: {
            alertStr[0] = "Please select an ACTIVE item to mark PAUSED";
            alertStr[1] = "Cannot mark this item as PAUSED";
            break;
        }
        case Utils.IN_PROCESS: {
            alertStr[0] = "Please select a PHASED OUT item to mark IN PROCESS";
            alertStr[1] = "Cannot mark this item as IN PROCESS";
            break;
        }
        case Utils.ACTIVE: {
            alertStr[0] = "Please select a PAUSED item to mark ACTIVE";
            alertStr[1] = "Cannot mark this item as ACTIVE";
            break;
        }
        case Utils.PHASED_OUT: {
            alertStr[0] = "Please select an item to mark PHASED OUT";
            alertStr[1] = "Cannot mark this item as PHASED OUT";
            break;
        }
        }
        return alertStr;
    }
}