Subversion Repositories SmartDukaan

Rev

Rev 2427 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2359 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
3
import in.shop2020.catalog.dashboard.shared.Item;
4
import in.shop2020.catalog.dashboard.shared.Utils;
5
 
6
import com.google.gwt.core.client.GWT;
7
import com.google.gwt.event.dom.client.ClickEvent;
8
import com.google.gwt.uibinder.client.UiBinder;
9
import com.google.gwt.uibinder.client.UiField;
10
import com.google.gwt.uibinder.client.UiHandler;
11
import com.google.gwt.user.client.Window;
12
import com.google.gwt.user.client.rpc.AsyncCallback;
13
import com.google.gwt.user.client.ui.Button;
14
import com.google.gwt.user.client.ui.DialogBox;
15
import com.google.gwt.user.client.ui.PasswordTextBox;
16
import com.google.gwt.user.client.ui.TextBox;
17
import com.google.gwt.user.client.ui.Widget;
18
 
2427 ankur.sing 19
/**
20
 * This dialog is created when PushToProduction button is pressed.
21
 * It authenticates the PushToProduction username and password and calls 
22
 * CatalogService.updateItemOnProduction if authentication is successful.
23
 * 
24
 */
2359 ankur.sing 25
public class AuthenticateDialog extends DialogBox {
26
 
27
    interface Binder extends UiBinder<Widget, AuthenticateDialog> { }
28
    private static final Binder binder = GWT.create(Binder.class);
29
 
30
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);
31
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
32
 
33
    @UiField TextBox userId;
34
    @UiField PasswordTextBox password;
35
    @UiField Button cancelButton, submitButton;
36
 
37
    private final Item item;
38
 
2427 ankur.sing 39
    /**
40
     * Instantiates this dialog from ItemActions.java on PushToProduction button press.
41
     * @param - item object which needs to be updated on production server.
42
     */
2359 ankur.sing 43
    public AuthenticateDialog(Item item) {
44
        setText("Authentication for Production Update");
45
        setWidget(binder.createAndBindUi(this));
46
        setAnimationEnabled(true);
47
        center();
48
        this.item = item;
49
    }
50
 
51
    @UiHandler("submitButton")
52
    void authenticate(ClickEvent event) {
2427 ankur.sing 53
        //authenticate username & password with production role.
6788 rajveer 54
        loginService.authenticateUser(userId.getText(), password.getText(), new AsyncCallback<String>() {
2359 ankur.sing 55
            @Override
56
            public void onSuccess(String result) {
6788 rajveer 57
                if(result != null && Long.parseLong(result) == Utils.ROLE_PRODUCTION) {
2427 ankur.sing 58
                    //Authentication successful. Call update on production.
6788 rajveer 59
               		pushToProduction();
2359 ankur.sing 60
                } else {
61
                    Window.alert("Authentication failed");
62
                }
63
            }
64
            @Override
65
            public void onFailure(Throwable caught) {
66
                Window.alert("Authentication failed");
67
            }
68
        });
69
    }
70
 
71
    private void pushToProduction() {
72
        if(item == null) {
73
            Window.alert("Please select an item to push to production");
74
            return;
75
        }
2427 ankur.sing 76
        // returns string. If there is some error while updating on production, string will be the error message
77
        // otherwise it will be success message.
78
        catalogService.updateItemOnProduction(item, new AsyncCallback<String>() {
2359 ankur.sing 79
            @Override
2427 ankur.sing 80
            public void onSuccess(String result) {
81
                Window.alert(result);
82
                hide();
2359 ankur.sing 83
            }
84
            @Override
85
            public void onFailure(Throwable caught) {
86
                Window.alert("Error while updating item on production");
87
            }
88
        });
89
    }
90
 
91
    @UiHandler("cancelButton")
92
    void cancel(ClickEvent event) {
93
        hide();
94
    }
95
}