Subversion Repositories SmartDukaan

Rev

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