Subversion Repositories SmartDukaan

Rev

Rev 2427 | 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;

/**
 * This dialog is created when PushToProduction button is pressed.
 * It authenticates the PushToProduction username and password and calls 
 * CatalogService.updateItemOnProduction if authentication is successful.
 * 
 */
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;
    
    /**
     * Instantiates this dialog from ItemActions.java on PushToProduction button press.
     * @param - item object which needs to be updated on production server.
     */
    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) {
        //authenticate username & password with production role.
        loginService.authenticateUser(userId.getText(), password.getText(), new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(result != null && Long.parseLong(result) == Utils.ROLE_PRODUCTION) {
                    //Authentication successful. Call update on production.
                        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;
        }
        // returns string. If there is some error while updating on production, string will be the error message
        // otherwise it will be success message.
        catalogService.updateItemOnProduction(item, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Window.alert(result);
                hide();
            }
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error while updating item on production");
            }
        });
    }
    
    @UiHandler("cancelButton")
    void cancel(ClickEvent event) {
        hide();
    }
}