Subversion Repositories SmartDukaan

Rev

Rev 2126 | Rev 2252 | 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;
51
    @UiField CheckBox defaultForEntity;
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());
293
 
2119 ankur.sing 294
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 295
          Add the instance to map and set the map to the item instance created above.*/
2119 ankur.sing 296
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
297
        VendorPricings v;
2105 ankur.sing 298
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 299
            v = new VendorPricings();
2105 ankur.sing 300
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
301
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
302
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
303
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
304
            vendorPrices.put(v.getVendorId(), v);
305
            item.setMop(v.getMop());
306
            item.setDealerPrice(v.getDealerPrice());
307
            item.setTransferPrice(v.getTransferPrice());
308
        }
2119 ankur.sing 309
        item.setVendorPricesMap(vendorPrices);
2105 ankur.sing 310
 
2119 ankur.sing 311
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
312
        Add the instance to map and set the map to the item instance created above.*/
313
        Map<Long, VendorItemMapping> vendorMappings = new HashMap<Long, VendorItemMapping>();
314
        VendorItemMapping vMapping;
315
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
316
            vMapping = new VendorItemMapping();
317
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
318
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
319
            vendorMappings.put(vMapping.getVendorId(), vMapping);
320
        }
321
        item.setVendorMappingsMap(vendorMappings);
322
 
323
        if(!Utils.validateItem(item)) {
324
            return;
325
        }
326
 
2105 ankur.sing 327
        /*Service method to add item to DB. */
328
        catalogService.addItem(item, new AsyncCallback<Long>() {
329
            @Override
330
            public void onSuccess(Long result) {
331
                if(result == null || result == 0) {
332
                    Window.alert("Error while adding item");
333
                    return;
334
                }
335
                Window.alert("Item added successfully. Id = " + result);
336
                hide();
337
            }
338
            @Override
339
            public void onFailure(Throwable caught) {
340
                Window.alert("Error while adding item");
341
            }
342
        });
343
 
344
    }
345
 
2138 ankur.sing 346
    @UiHandler("vendorCategory")
347
    void setProductGroup(ChangeEvent event) {
348
        if(HANDSETS.equals(vendorCategory.getItemText(vendorCategory.getSelectedIndex()))) {
349
            productGroup.setText(HANDSETS);
350
        } else {
351
            productGroup.setText("");
352
        }
353
    }
354
 
2105 ankur.sing 355
    @UiHandler("cancelButton")
356
    void closeForm(ClickEvent event) {
357
        this.hide();
358
    }
359
 
360
    /**
361
     * This will pop up a dialog box for adding vendor details. 
362
     * @param event
363
     */
2119 ankur.sing 364
    /*@UiHandler("addVendorPrices")
2105 ankur.sing 365
    void addVendorPrices(ClickEvent event) {
366
        VendorPricesDialog pricesDialog = new VendorPricesDialog();
367
        pricesDialog.updateButton.setText("Add");
2119 ankur.sing 368
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2105 ankur.sing 369
            @Override
2119 ankur.sing 370
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
2105 ankur.sing 371
                if(!vendorExists(vendorId)) {
372
                    Window.alert("Vendor already exists");
373
                    return false;
374
                }   
2119 ankur.sing 375
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 376
                    return false;
377
                }
378
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
379
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
380
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
381
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
382
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
383
 
384
                int row = vendorTable.getRowCount();
385
                vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId+"");
386
                vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
387
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
388
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
389
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
390
                Button removeButton = new Button("Remove");
391
                vendorTable.setWidget(row, TABLE_INDEX_TP + 1, removeButton);
392
                removeButton.addClickHandler(new ClickHandler() {
393
                    @Override
394
                    public void onClick(ClickEvent event) {
395
                        Cell cell = vendorTable.getCellForEvent(event);
396
                        int row = cell.getRowIndex();
397
                        vendorTable.removeRow(row);
398
                    }
399
                });
400
                return true;
401
            }
402
        });
403
        pricesDialog.center();
404
        pricesDialog.show();
2119 ankur.sing 405
    }*/
2105 ankur.sing 406
 
407
    /**
408
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
409
     */
410
    private boolean vendorExists(long vendorId) {
411
        long id;
412
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
413
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
414
            if(vendorId == id) {
415
                return false;
416
            }
417
        }
418
        return true;
419
    }
420
 
421
    /**
2119 ankur.sing 422
     * validate vendor prices (MOP, DealerPrice, TransferPrice)
2105 ankur.sing 423
     */
2119 ankur.sing 424
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2105 ankur.sing 425
        double mrpValue;
426
        try {
427
            mrpValue = Double.parseDouble(mrp.getText().trim());
428
        } catch (NumberFormatException ex) {
429
            Window.alert("Invalid MRP value.");
430
            return false;
431
        }
432
        if(mrpValue < mop) {
433
            Window.alert("MOP cannot be more than MRP.");
434
            return false;
435
        }
436
        if(tp > mop) {
437
            Window.alert("Transfer Price cannot be more than MOP.");
438
            return false;
439
        }
440
        return true;
441
    }
2066 ankur.sing 442
}