Subversion Repositories SmartDukaan

Rev

Rev 2427 | 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 com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
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.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public class AuthenticateDialog extends DialogBox {

    interface Binder extends UiBinder<Widget, AuthenticateDialog> { }
    private static final Binder binder = GWT.create(Binder.class);
    
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);

    @UiField TextBox userId;
    @UiField PasswordTextBox password;
    @UiField Button cancelButton, submitButton;

    private final Item item;
    
    public AuthenticateDialog(Item item) {
        setText("Authentication for Production Update");
        setWidget(binder.createAndBindUi(this));
        setAnimationEnabled(true);
        center();
        this.item = item;
    }
    
    @UiHandler("submitButton")
    void authenticate(ClickEvent event) {
        loginService.authenticateUser(userId.getText(), password.getText(), Utils.ROLE_PRODUCTION, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(result != null) {
                    pushToProduction();
                } else {
                    Window.alert("Authentication failed");
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Authentication failed");
            }
        });

    }
    
    private void pushToProduction() {
        if(item == null) {
            Window.alert("Please select an item to push to production");
            return;
        }
        catalogService.updateItemOnProduction(item, new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                if(result) {
                    Window.alert("Item updated successfully on production");
                    hide();
                } else {
                    Window.alert("Error while updating item on production");
                }
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while updating item on production");
            }
        });
    }
    
    @UiHandler("cancelButton")
    void cancel(ClickEvent event) {
        hide();
    }
    
    public void hide() {
        //TODO check if super.hide() destroys the object otherwise
        // everytime push to production is called, a new object of AuthenticateDialog
        // is created.
        super.hide();
    }
}