Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2066 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
2105 ankur.sing 3
import in.shop2020.catalog.dashboard.shared.Item;
4
import in.shop2020.catalog.dashboard.shared.Utils;
2119 ankur.sing 5
import in.shop2020.catalog.dashboard.shared.VendorItemMapping;
6
import in.shop2020.catalog.dashboard.shared.VendorPricings;
2066 ankur.sing 7
 
2105 ankur.sing 8
import java.util.HashMap;
9
import java.util.Map;
2066 ankur.sing 10
 
2105 ankur.sing 11
import com.google.gwt.core.client.GWT;
2138 ankur.sing 12
import com.google.gwt.event.dom.client.ChangeEvent;
2105 ankur.sing 13
import com.google.gwt.event.dom.client.ClickEvent;
14
import com.google.gwt.event.dom.client.ClickHandler;
15
import com.google.gwt.uibinder.client.UiBinder;
16
import com.google.gwt.uibinder.client.UiField;
17
import com.google.gwt.uibinder.client.UiHandler;
18
import com.google.gwt.user.client.Window;
19
import com.google.gwt.user.client.rpc.AsyncCallback;
20
import com.google.gwt.user.client.ui.Button;
21
import com.google.gwt.user.client.ui.CheckBox;
22
import com.google.gwt.user.client.ui.DialogBox;
23
import com.google.gwt.user.client.ui.FlexTable;
24
import com.google.gwt.user.client.ui.ListBox;
25
import com.google.gwt.user.client.ui.TextBox;
26
import com.google.gwt.user.client.ui.Widget;
27
import com.google.gwt.user.datepicker.client.DateBox;
28
 
29
public class ItemForm extends DialogBox {
30
 
31
    interface ItemFormUiBinder extends UiBinder<Widget, ItemForm> {}
32
    private static ItemFormUiBinder uiBinder = GWT.create(ItemFormUiBinder.class);
33
 
2138 ankur.sing 34
    private final String HANDSETS = "Handsets",
35
                         ACCESSORIES = "Accessories";
36
 
2105 ankur.sing 37
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2119 ankur.sing 38
    private final int TABLE_INDEX_VENDORID = 0,
39
                      TABLE_INDEX_VENDOR_DESC = 1, 
40
                      TABLE_INDEX_ITEM_KEY = 2,
41
                      TABLE_INDEX_MOP = 2,
42
                      TABLE_INDEX_DP = 3,
43
                      TABLE_INDEX_TP = 4;
2105 ankur.sing 44
 
45
    @UiField ListBox vendorCategory;
46
    @UiField TextBox productGroup;
47
    @UiField TextBox brand, modelNumber, modelName, color, comments;
48
    @UiField TextBox mrp, sellingPrice, weight, bestDealText, bestDealValue, bestSellingRank;
49
    @UiField DateBox startDate, retireDate;
50
    @UiField Button addButton, cancelButton;
2252 ankur.sing 51
    @UiField CheckBox defaultForEntity, risky;
2105 ankur.sing 52
    @UiField FlexTable headerVendor, vendorTable;
2119 ankur.sing 53
    @UiField FlexTable headerVendorM, vendorTableM;
2105 ankur.sing 54
 
55
    public ItemForm(){
56
        setText("Add New Item");
57
        setWidget(uiBinder.createAndBindUi(this));
2119 ankur.sing 58
        initMappingHeader();
2105 ankur.sing 59
        initPricingHeader();
2138 ankur.sing 60
        vendorCategory.addItem(HANDSETS);
61
        vendorCategory.addItem(ACCESSORIES);
62
        productGroup.setText(HANDSETS);
2105 ankur.sing 63
    }
64
 
2119 ankur.sing 65
    private void initMappingHeader(){
66
        // Initialize the header.
67
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
68
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
2359 ankur.sing 69
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
2119 ankur.sing 70
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
71
 
72
        headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
2359 ankur.sing 73
        headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
2119 ankur.sing 74
        headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
75
 
2359 ankur.sing 76
        headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
77
 
2119 ankur.sing 78
        Button addButton = new Button("Add");
79
        headerVendorM.setWidget(0, TABLE_INDEX_ITEM_KEY + 1, addButton);
80
        addButton.addClickHandler(new ClickHandler() {
81
            @Override
82
            public void onClick(ClickEvent event) {
2126 ankur.sing 83
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
84
                        modelNumber.getText().trim(), color.getText().trim());
2119 ankur.sing 85
                vendorMappingDialog.updateButton.setText("Add");
86
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
87
                    @Override
88
                    public boolean onUpdate(String key, long vendorId) {
89
                        final int row = vendorTableM.getRowCount();
90
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
91
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
2359 ankur.sing 92
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
2119 ankur.sing 93
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
94
 
95
                        vendorTableM.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
96
                        vendorTableM.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
97
                        vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, key);
98
                        Button removeButton = new Button("X");
99
                        vendorTableM.setWidget(row, TABLE_INDEX_ITEM_KEY + 1, removeButton);
2359 ankur.sing 100
 
101
                        vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
2119 ankur.sing 102
                        removeButton.addClickHandler(new ClickHandler() {
103
                            @Override
104
                            public void onClick(ClickEvent event) {
105
                                vendorTableM.removeRow(row);
106
                            }
107
                        });
108
                        return true;
109
                    }
110
                });
111
                vendorMappingDialog.show();
112
            }
113
        });
114
    }
2105 ankur.sing 115
 
116
    private void initPricingHeader(){
117
        // Initialize the header.
118
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
119
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
120
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
121
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
122
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
2119 ankur.sing 123
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");
2105 ankur.sing 124
 
125
        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
2359 ankur.sing 126
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
2105 ankur.sing 127
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
128
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
129
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
2119 ankur.sing 130
 
2359 ankur.sing 131
        headerVendor.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
132
 
2119 ankur.sing 133
        Button addButton = new Button("Add");
134
        headerVendor.setWidget(0, TABLE_INDEX_TP + 1, addButton);
135
        addButton.addClickHandler(new ClickHandler() {
136
            @Override
137
            public void onClick(ClickEvent event) {
138
                VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
139
                vendorPricesDialog.updateButton.setText("Add");
140
                vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
141
                    @Override
142
                    public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
143
                        if(!vendorExists(vendorId)) {
144
                            Window.alert("Vendor already exists");
145
                            return false;
146
                        }
147
                        /*if(!validateVendorPrices(mop, dp, tp)) {
148
                            return false;
149
                        }*/
150
                        final int row = vendorTable.getRowCount();
151
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
152
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
153
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
154
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
155
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
156
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP + 1, "50px");
157
 
158
                        vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
159
                        vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
160
                        vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
161
                        vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
162
                        vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
2359 ankur.sing 163
 
164
                        vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
165
 
2119 ankur.sing 166
                        Button removeButton = new Button("X");
167
                        vendorTable.setWidget(row, TABLE_INDEX_TP + 1, removeButton);
168
                        removeButton.addClickHandler(new ClickHandler() {
169
                            @Override
170
                            public void onClick(ClickEvent event) {
171
                                vendorTable.removeRow(row);
172
                            }
173
                        });
174
                        return true;
175
                    }
176
                });
177
                vendorPricesDialog.show();
178
            }
179
        });
2105 ankur.sing 180
    }
181
 
182
    @UiHandler("addButton")
183
    /**
2359 ankur.sing 184
     * On add button click a new Item instance is created and all the information from UI fields is dumped in this item instance.
2105 ankur.sing 185
     * This item instance is then passed to service implementation.
186
     */
187
    void addItem(ClickEvent event) {
188
        //TODO: validate UI fields
2119 ankur.sing 189
        catalogService.checkSimilarItem(productGroup.getText().trim(), brand.getText().trim(),
190
                modelNumber.getText().trim(), color.getText().trim(), new AsyncCallback<Long>() {
191
            @Override
192
            public void onFailure(Throwable caught) {
193
                GWT.log("Error checking similar item");
194
                Window.alert("Error checking similar item");
195
                return;
196
            }
197
            @Override
198
            public void onSuccess(Long result) {
199
                if(result != 0) {
200
                    Window.alert("Similar product exists with Item Id " + result);
201
                    return;
202
                }
203
                addNewItem();
204
            }
205
        });
206
    }
207
 
2126 ankur.sing 208
    private void addNewItem() {
2105 ankur.sing 209
        Item item = new Item();
210
 
211
        item.setProductGroup(productGroup.getText().trim());
212
        item.setVendorCategory(vendorCategory.getItemText(vendorCategory.getSelectedIndex()));
213
 
214
        item.setBrand(brand.getText().trim());
215
        item.setModelNumber(modelNumber.getText().trim());
216
        item.setModelName(modelName.getText().trim());
217
        item.setColor(color.getText().trim());
218
        item.setComments(comments.getText().trim());
219
        try {
2126 ankur.sing 220
            if(!mrp.getText().trim().isEmpty()) {
221
                double mrpValue = Double.parseDouble(mrp.getText().trim());
222
                if(mrpValue <= 0) {
223
                    throw new NumberFormatException("Negative value of MRP");
224
                }
225
                item.setMrp(mrpValue);
226
            } else {
227
                item.setMrp(-1);
2105 ankur.sing 228
            }
229
        } catch(NumberFormatException ex) {
2126 ankur.sing 230
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
2105 ankur.sing 231
            return;
232
        }
233
        try {
2126 ankur.sing 234
            if(!sellingPrice.getText().trim().isEmpty()) {
2105 ankur.sing 235
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
2126 ankur.sing 236
            if(spValue <= 0) {
2105 ankur.sing 237
                throw new NumberFormatException("Negative value of Selling price");
238
            }
239
            item.setSellingPrice(spValue);
2126 ankur.sing 240
            } else {
241
                item.setSellingPrice(-1);
242
            }
2105 ankur.sing 243
        } catch(NumberFormatException ex) {
2126 ankur.sing 244
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
2105 ankur.sing 245
            return;
246
        }
247
        try {
2126 ankur.sing 248
            if(!weight.getText().trim().isEmpty()) {
2105 ankur.sing 249
                double wtValue = Double.parseDouble(weight.getText().trim());
2126 ankur.sing 250
                if(wtValue <= 0) {
2105 ankur.sing 251
                    throw new NumberFormatException("Negative value of Weight");
252
                }
253
                item.setWeight(wtValue);
2126 ankur.sing 254
            } else {
255
                item.setWeight(-1);
2105 ankur.sing 256
            }
257
        } catch(NumberFormatException ex) {
2126 ankur.sing 258
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
2105 ankur.sing 259
            return;
260
        }
261
        try {
262
            if(!startDate.getTextBox().getText().trim().equals("")) {
263
                item.setStartDate(startDate.getValue().getTime());
264
            }
265
        } catch(Exception ex) {
266
            Window.alert("Invalid start date format");
267
            return;
268
        }
269
        item.setBestDealsText(bestDealText.getText().trim());
270
        try {
271
            if(!bestDealValue.getText().trim().equals("")) {
272
                double bdValue = Double.parseDouble(bestDealValue.getText().trim());
273
                if(bdValue < 0) {
274
                    throw new NumberFormatException("Negative value of BestDealValue");
275
                }
276
                item.setBestDealsValue(bdValue);
2126 ankur.sing 277
            } else {
278
                item.setBestDealsValue(-1);
2105 ankur.sing 279
            }
280
        } catch(NumberFormatException ex) {
281
            Window.alert("Invalid best deal value format");
282
            return;
283
        }
284
        try {
285
            if(!bestSellingRank.getText().trim().equals("")) {
286
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
287
                if(bsrValue < 0) {
288
                    throw new NumberFormatException("Negative value of Best Selling Rank");
289
                }
290
                item.setBestSellingRank(bsrValue);
2126 ankur.sing 291
            } else {
292
                item.setBestSellingRank(-1);
2105 ankur.sing 293
            }
294
 
295
        } catch(NumberFormatException ex) {
296
            Window.alert("Invalid best selling rank format");
297
            return;
298
        }
299
        item.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 300
        item.setRisky(risky.getValue());
2105 ankur.sing 301
 
2119 ankur.sing 302
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 303
          Add the instance to map and set the map to the item instance created above.*/
2119 ankur.sing 304
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
305
        VendorPricings v;
2105 ankur.sing 306
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 307
            v = new VendorPricings();
2105 ankur.sing 308
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
309
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
310
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
311
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
312
            vendorPrices.put(v.getVendorId(), v);
313
            item.setMop(v.getMop());
314
            item.setDealerPrice(v.getDealerPrice());
315
            item.setTransferPrice(v.getTransferPrice());
316
        }
2119 ankur.sing 317
        item.setVendorPricesMap(vendorPrices);
2105 ankur.sing 318
 
2119 ankur.sing 319
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
320
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 321
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2119 ankur.sing 322
        VendorItemMapping vMapping;
323
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
324
            vMapping = new VendorItemMapping();
325
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
326
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
2359 ankur.sing 327
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
2119 ankur.sing 328
        }
2359 ankur.sing 329
        item.setVendorKeysMap(vendorMappings);
2119 ankur.sing 330
 
331
        if(!Utils.validateItem(item)) {
332
            return;
333
        }
334
 
2105 ankur.sing 335
        /*Service method to add item to DB. */
336
        catalogService.addItem(item, new AsyncCallback<Long>() {
337
            @Override
338
            public void onSuccess(Long result) {
339
                if(result == null || result == 0) {
340
                    Window.alert("Error while adding item");
341
                    return;
342
                }
343
                Window.alert("Item added successfully. Id = " + result);
344
                hide();
345
            }
346
            @Override
347
            public void onFailure(Throwable caught) {
348
                Window.alert("Error while adding item");
349
            }
350
        });
351
    }
352
 
2138 ankur.sing 353
    @UiHandler("vendorCategory")
354
    void setProductGroup(ChangeEvent event) {
355
        if(HANDSETS.equals(vendorCategory.getItemText(vendorCategory.getSelectedIndex()))) {
356
            productGroup.setText(HANDSETS);
357
        } else {
358
            productGroup.setText("");
359
        }
360
    }
361
 
2105 ankur.sing 362
    @UiHandler("cancelButton")
363
    void closeForm(ClickEvent event) {
364
        this.hide();
365
    }
366
 
367
    /**
368
     * This will pop up a dialog box for adding vendor details. 
369
     * @param event
370
     */
2119 ankur.sing 371
    /*@UiHandler("addVendorPrices")
2105 ankur.sing 372
    void addVendorPrices(ClickEvent event) {
373
        VendorPricesDialog pricesDialog = new VendorPricesDialog();
374
        pricesDialog.updateButton.setText("Add");
2119 ankur.sing 375
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2105 ankur.sing 376
            @Override
2119 ankur.sing 377
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
2105 ankur.sing 378
                if(!vendorExists(vendorId)) {
379
                    Window.alert("Vendor already exists");
380
                    return false;
381
                }   
2119 ankur.sing 382
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 383
                    return false;
384
                }
385
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
386
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
387
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
388
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
389
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
390
 
391
                int row = vendorTable.getRowCount();
392
                vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId+"");
393
                vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
394
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
395
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
396
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
397
                Button removeButton = new Button("Remove");
398
                vendorTable.setWidget(row, TABLE_INDEX_TP + 1, removeButton);
399
                removeButton.addClickHandler(new ClickHandler() {
400
                    @Override
401
                    public void onClick(ClickEvent event) {
402
                        Cell cell = vendorTable.getCellForEvent(event);
403
                        int row = cell.getRowIndex();
404
                        vendorTable.removeRow(row);
405
                    }
406
                });
407
                return true;
408
            }
409
        });
410
        pricesDialog.center();
411
        pricesDialog.show();
2119 ankur.sing 412
    }*/
2105 ankur.sing 413
 
414
    /**
415
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
416
     */
417
    private boolean vendorExists(long vendorId) {
418
        long id;
419
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
420
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
421
            if(vendorId == id) {
422
                return false;
423
            }
424
        }
425
        return true;
426
    }
427
 
428
    /**
2119 ankur.sing 429
     * validate vendor prices (MOP, DealerPrice, TransferPrice)
2105 ankur.sing 430
     */
2119 ankur.sing 431
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2105 ankur.sing 432
        double mrpValue;
433
        try {
434
            mrpValue = Double.parseDouble(mrp.getText().trim());
435
        } catch (NumberFormatException ex) {
436
            Window.alert("Invalid MRP value.");
437
            return false;
438
        }
439
        if(mrpValue < mop) {
440
            Window.alert("MOP cannot be more than MRP.");
441
            return false;
442
        }
443
        if(tp > mop) {
444
            Window.alert("Transfer Price cannot be more than MOP.");
445
            return false;
446
        }
447
        return true;
448
    }
2066 ankur.sing 449
}