Subversion Repositories SmartDukaan

Rev

Rev 3884 | Rev 4440 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2105 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
2126 ankur.sing 3
import in.shop2020.catalog.dashboard.shared.Item;
4
import in.shop2020.catalog.dashboard.shared.Utils;
5
 
2105 ankur.sing 6
import com.google.gwt.core.client.GWT;
7
import com.google.gwt.event.dom.client.ClickEvent;
2359 ankur.sing 8
import com.google.gwt.event.dom.client.ClickHandler;
2105 ankur.sing 9
import com.google.gwt.uibinder.client.UiBinder;
10
import com.google.gwt.uibinder.client.UiField;
11
import com.google.gwt.uibinder.client.UiHandler;
2126 ankur.sing 12
import com.google.gwt.user.client.Window;
13
import com.google.gwt.user.client.rpc.AsyncCallback;
2105 ankur.sing 14
import com.google.gwt.user.client.ui.Button;
2359 ankur.sing 15
import com.google.gwt.user.client.ui.DialogBox;
16
import com.google.gwt.user.client.ui.HorizontalPanel;
3884 chandransh 17
import com.google.gwt.user.client.ui.Label;
2359 ankur.sing 18
import com.google.gwt.user.client.ui.RadioButton;
2105 ankur.sing 19
import com.google.gwt.user.client.ui.ResizeComposite;
2359 ankur.sing 20
import com.google.gwt.user.client.ui.VerticalPanel;
2105 ankur.sing 21
import com.google.gwt.user.client.ui.Widget;
22
 
2427 ankur.sing 23
/**
24
 * Panel containing buttons for different actions on item.
25
 * e.g. update item, add new item, change item status, mark/unmark risky, push prices to production etc.
26
 * @author ankur
27
 *
28
 */
2105 ankur.sing 29
public class ItemActions extends ResizeComposite{
30
 
31
    interface ItemActionsUiBinder extends UiBinder<Widget, ItemActions> {}
32
    private static ItemActionsUiBinder uiBinder = GWT.create(ItemActionsUiBinder.class);
2126 ankur.sing 33
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2105 ankur.sing 34
 
2126 ankur.sing 35
    private ItemDetails itemDetails;
2105 ankur.sing 36
 
2359 ankur.sing 37
    @UiField Button addItem, updateItem, pushItemToProd, phaseoutItem, activateItem, markAliveItem, pauseItem;
38
    @UiField Button markRisky, unmarkRisky;
39
    @UiField Button generateMasterSheet;
2105 ankur.sing 40
 
41
    public ItemActions(){
42
        initWidget(uiBinder.createAndBindUi(this));
2427 ankur.sing 43
        //pushItemToProd.setEnabled(false);
2105 ankur.sing 44
    }
2427 ankur.sing 45
 
2105 ankur.sing 46
 
2126 ankur.sing 47
    @UiHandler("updateItem")
48
    void updateItem(ClickEvent event) {
49
        itemDetails.updateItem();
50
    }
51
 
2427 ankur.sing 52
    /**
53
     * creates a new form to fill up item details and displays the form.
54
     * @param event
55
     */
2105 ankur.sing 56
    @UiHandler("addItem")
57
    void addItem(ClickEvent event) {
2126 ankur.sing 58
        ItemForm itemForm = new ItemForm();
59
        itemForm.center();
60
        itemForm.show();
2105 ankur.sing 61
    }
62
 
63
    @UiHandler("phaseoutItem")
64
    void phaseoutItem(ClickEvent event) {
2126 ankur.sing 65
        final long itemId = validateStatusChange(Utils.PHASED_OUT);
66
        if(itemId <= 0) {
67
            return;
68
        }
69
        catalogService.phaseoutItem(itemId, new AsyncCallback<Void>() {
70
            @Override
71
            public void onSuccess(Void result) {
72
                Window.alert("Item marked as PHASED OUT");
73
            }
74
            @Override
75
            public void onFailure(Throwable caught) {
76
                Window.alert("Error while marking item " + itemId + " PHASED OUT");
77
            }
78
        });
2105 ankur.sing 79
    }
80
 
81
    @UiHandler("activateItem")
82
    void activateItem(ClickEvent event) {
2126 ankur.sing 83
        final long itemId = validateStatusChange(Utils.ACTIVE);
84
        if(itemId <= 0) {
85
            return;
86
        }
87
        catalogService.activateItem(itemId, new AsyncCallback<Void>() {
88
            @Override
89
            public void onSuccess(Void result) {
90
                Window.alert("Item marked as ACTIVE");
91
            }
92
            @Override
93
            public void onFailure(Throwable caught) {
94
                Window.alert("Error while marking item " + itemId + " ACTIVE");
95
            }
96
        });
2105 ankur.sing 97
    }
98
 
2126 ankur.sing 99
    @UiHandler("pauseItem")
100
    void pauseItem(ClickEvent event) {
101
        final long itemId = validateStatusChange(Utils.PAUSED);
102
        if(itemId <= 0) {
103
            return;
104
        }
105
        catalogService.pauseItem(itemId, new AsyncCallback<Void>() {
106
            @Override
107
            public void onSuccess(Void result) {
108
                Window.alert("Item marked as PAUSED");
109
            }
110
            @Override
111
            public void onFailure(Throwable caught) {
112
                Window.alert("Error while marking item " + itemId + " PAUSED");
113
            }
114
        });
2105 ankur.sing 115
    }
2126 ankur.sing 116
 
117
    @UiHandler("markAliveItem")
118
    void markAsAlive(ClickEvent event) {
119
        final long itemId = validateStatusChange(Utils.IN_PROCESS);
120
        if(itemId <= 0) {
121
            return;
122
        }
123
        catalogService.markInProcess(itemId, new AsyncCallback<Void>() {
124
            @Override
125
            public void onSuccess(Void result) {
126
                Window.alert("Item marked as IN PROCESS");
127
            }
128
            @Override
129
            public void onFailure(Throwable caught) {
130
                Window.alert("Error while marking item " + itemId + " IN PROCESS");
131
            }
132
        });
2105 ankur.sing 133
    }
2126 ankur.sing 134
 
2427 ankur.sing 135
    /**
136
     * Creates and pops up authentication dialog for push to production username and password
137
     * @param event
138
     */
2359 ankur.sing 139
    @UiHandler("pushItemToProd")
140
    void pushItemToProduction(ClickEvent event) {
141
        if(itemDetails.getItem() == null) {
142
            Window.alert("Please select an item to update");
143
            return;
144
        }
145
        AuthenticateDialog authDialog = new AuthenticateDialog(itemDetails.getItem());
146
        authDialog.show();
147
    }
148
 
149
    @UiHandler("markRisky")
150
    void markRisky(ClickEvent event) {
151
        changeRiskyFlag(true);
152
    }
153
 
154
    @UiHandler("unmarkRisky")
155
    void unmarkRisky(ClickEvent event) {
156
        changeRiskyFlag(false);
157
    }
158
 
159
    @UiHandler("generateMasterSheet")
160
    void generateMasterSheet(ClickEvent event)  {
161
        createDialog();
162
    }
2427 ankur.sing 163
 
164
    /**
3921 chandransh 165
     * Creates and pops up  dialog box containing following options. 
166
     * Categories: 1) Handsets 2) Accessories
167
     * Vendors: 1) Hotspot 2) Tulip 3) Sarvottam
2427 ankur.sing 168
     * Also creates submit and cancel buttons and add click event listeners to them.
169
     * On submit button click, request is sent to <code>in.shop2020.catalog.dashboard.server.FileDownloadServlet<code>
170
     */
2359 ankur.sing 171
    private void createDialog() {
172
        VerticalPanel vp = new VerticalPanel();
173
        final DialogBox db = new DialogBox();
174
        db.setText("Master Sheet Download");
3884 chandransh 175
 
176
        final Label categoryLabel = new Label("Category");
177
        vp.add(categoryLabel);
178
 
2359 ankur.sing 179
        final RadioButton handsetsRB = new RadioButton("vendorCategory", "Handsets");
180
        final RadioButton accRB = new RadioButton("vendorCategory", "Accessories");
181
        handsetsRB.setValue(true);
3884 chandransh 182
 
2359 ankur.sing 183
        HorizontalPanel hpRB = new HorizontalPanel();
184
        hpRB.add(handsetsRB);
185
        hpRB.add(accRB);
186
        vp.add(hpRB);
187
 
3884 chandransh 188
        final Label vendorLabel = new Label("Vendor");
189
        vp.add(vendorLabel);
190
 
3921 chandransh 191
        //TODO: Get the list of all vendors from the database and populate the choices accordingly
3884 chandransh 192
        final RadioButton hotspotRB = new RadioButton("vendor", "Hotspot");
193
        final RadioButton sarvottamRB = new RadioButton("vendor", "Sarvottam");
194
        final RadioButton tulipRB = new RadioButton("vendor", "Tulip");
195
        hotspotRB.setValue(true);
196
 
197
        HorizontalPanel vendorPanel = new HorizontalPanel();
198
        vendorPanel.add(hotspotRB);
199
        vendorPanel.add(tulipRB);
200
        vendorPanel.add(sarvottamRB);
201
 
202
        vp.add(vendorPanel);
203
 
2359 ankur.sing 204
        Button submitButton = new Button("OK");
205
        Button cancelButton = new Button("Cancel");
206
        HorizontalPanel hpButtons = new HorizontalPanel();
207
        hpButtons.setSpacing(5);
208
        hpButtons.add(submitButton);
209
        hpButtons.add(cancelButton);
210
 
211
        vp.add(hpButtons);
3884 chandransh 212
 
2359 ankur.sing 213
        db.add(vp);
214
        submitButton.addClickHandler(new ClickHandler() {
215
            @Override
216
            public void onClick(ClickEvent event) {
217
                String vendorCategory;
218
                if(handsetsRB.getValue()) {
219
                    vendorCategory = "Handsets";
220
                } else {
221
                    vendorCategory = "Accessories";
222
                }
3884 chandransh 223
 
224
                long vendorId = 1;
225
                if(hotspotRB.getValue()) {
226
                    vendorId = 1;
227
                } else if(sarvottamRB.getValue()) {
228
                    vendorId = 3;
229
                } else if(tulipRB.getValue()) {
230
                    vendorId = 2;
231
                }
232
 
2427 ankur.sing 233
                // Servlet mapping mastersheet/download is mapped to in.shop2020.catalog.dashboard.server.FileDownloadServlet
3884 chandransh 234
                String link = GWT.getHostPageBaseURL() + "mastersheet/download?vendorCategory=" + vendorCategory + "&vendorId=" + vendorId;
2359 ankur.sing 235
                Window.open(link, "_self", "Master Sheet Download");
236
                db.hide();
237
            }
238
        });
239
        cancelButton.addClickHandler(new ClickHandler() {
240
            @Override
241
            public void onClick(ClickEvent event) {
242
                db.hide();
243
            }
244
        });
245
        db.center();
246
        db.setAnimationEnabled(true);
247
        db.show();
248
    }
249
 
2427 ankur.sing 250
    /**
251
     * validates and then calls service method changeItemRiskyFlag to change risky flag.
252
     * @param risky
253
     */
2359 ankur.sing 254
    private void changeRiskyFlag(boolean risky) {
255
        Item item = itemDetails.getItem();
256
        if(item == null) {
257
            Window.alert("Please select an item to mark/unmark as risky");
258
            return;
259
        }
260
        if(risky && item.isRisky()) {
261
            Window.alert("Item already marked as risky");
262
            return;
263
        }
264
        if(!risky && !item.isRisky()) {
265
            Window.alert("Item not marked as risky");
266
            return;
267
        }
268
        catalogService.changeItemRiskyFlag(item.getId(), risky, new AsyncCallback<Boolean>() {
269
            @Override
270
            public void onSuccess(Boolean result) {
271
                if(result) {
272
                    Window.alert("Done!");
273
                } else {
274
                    Window.alert("Error while marking/unmarking risky flag on staging/production server.");
275
                }
276
            }
277
            @Override
278
            public void onFailure(Throwable caught) {
279
                Window.alert("Error");
280
            }
281
        });
282
    }
283
 
2126 ankur.sing 284
    public void setItemDetails(ItemDetails itemDetails) {
285
        this.itemDetails = itemDetails;
2105 ankur.sing 286
    }
2427 ankur.sing 287
 
288
    /**
289
     * Validate item status change.
290
     * @param toStatus
291
     * @return item Id if validation is successful else -1
292
     */
2126 ankur.sing 293
    private long validateStatusChange(int toStatus) {
294
        final Item item = itemDetails.getItem();
295
        if(item == null) {
296
            Window.alert(getAlertString(toStatus)[0]);
297
            return -1;
298
        }
299
        if(!Utils.validateStatusChange(item.getItemStatusValue(), toStatus)) {
300
            Window.alert(getAlertString(toStatus)[1]);
301
            return -1;
302
        }
303
        return item.getId();
304
    }
305
 
2427 ankur.sing 306
    /**
307
     * @param toStatus
308
     * @return messages to be show to the user if item status change validation fails.
309
     *       <br>-first message will be shown if no item is selected in the list
310
     *       <br>-second message is shown if validation for FromStatus to ToStatus fails.
311
     */
2126 ankur.sing 312
    private String[] getAlertString(int toStatus) {
313
        String[] alertStr = {"", ""};
314
        switch(toStatus) {
315
        case Utils.PAUSED: {
316
            alertStr[0] = "Please select an ACTIVE item to mark PAUSED";
317
            alertStr[1] = "Cannot mark this item as PAUSED";
318
            break;
319
        }
320
        case Utils.IN_PROCESS: {
321
            alertStr[0] = "Please select a PHASED OUT item to mark IN PROCESS";
322
            alertStr[1] = "Cannot mark this item as IN PROCESS";
323
            break;
324
        }
325
        case Utils.ACTIVE: {
326
            alertStr[0] = "Please select a PAUSED item to mark ACTIVE";
327
            alertStr[1] = "Cannot mark this item as ACTIVE";
328
            break;
329
        }
330
        case Utils.PHASED_OUT: {
331
            alertStr[0] = "Please select an item to mark PHASED OUT";
332
            alertStr[1] = "Cannot mark this item as PHASED OUT";
333
            break;
334
        }
335
        }
336
        return alertStr;
337
    }
2105 ankur.sing 338
}
2126 ankur.sing 339