Subversion Repositories SmartDukaan

Rev

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