Subversion Repositories SmartDukaan

Rev

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