Subversion Repositories SmartDukaan

Rev

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