Subversion Repositories SmartDukaan

Rev

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