Subversion Repositories SmartDukaan

Rev

Rev 7190 | Rev 7291 | 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;
5217 amit.gupta 7
import java.util.Date;
2105 ankur.sing 8
import java.util.HashMap;
9
import java.util.Map;
4725 phani.kuma 10
import java.util.Map.Entry;
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.ListBox;
24
import com.google.gwt.user.client.ui.TextBox;
25
import com.google.gwt.user.client.ui.Widget;
26
import com.google.gwt.user.datepicker.client.DateBox;
27
 
2427 ankur.sing 28
/**
29
 * ItemForm is a DialogBox which is created on the click of Add Item button in ItemActions.
30
 * This contains UI fields to fill up item attributes. It also contains tables for adding
31
 * vendor prices and vendor mapping keys.
32
 *
33
 */
5217 amit.gupta 34
public class ItemForm extends DialogBox implements ComingSoon{
2105 ankur.sing 35
 
36
    interface ItemFormUiBinder extends UiBinder<Widget, ItemForm> {}
37
    private static ItemFormUiBinder uiBinder = GWT.create(ItemFormUiBinder.class);
38
 
5586 phani.kuma 39
    private final static CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2119 ankur.sing 40
    private final int TABLE_INDEX_VENDORID = 0,
41
                      TABLE_INDEX_VENDOR_DESC = 1, 
42
                      TABLE_INDEX_ITEM_KEY = 2,
43
                      TABLE_INDEX_MOP = 2,
44
                      TABLE_INDEX_DP = 3,
6759 amar.kumar 45
                      TABLE_INDEX_TP = 4,
46
                      TABLE_INDEX_NLC = 5;
5586 phani.kuma 47
 
48
    private static boolean entityIdMandatory = Utils.isEntityIdMandatory();
2105 ankur.sing 49
 
5586 phani.kuma 50
    @UiField TextBox productGroup, catalogItemId;
2105 ankur.sing 51
    @UiField TextBox brand, modelNumber, modelName, color, comments;
7190 amar.kumar 52
    @UiField TextBox mrp, sellingPrice, weight, bestDealText, bestDealValue, bestSellingRank,bestDealsDetailsText,bestDealsDetailsLink, minStockLevel, numOfDaysStock, freebieItemId;
5217 amit.gupta 53
    @UiField DateBox startDate, retireDate, comingSoonStartDate, expectedArrivalDate;
54
    @UiField Button addButton, cancelButton, comingSoonButton;
6241 amit.gupta 55
    @UiField CheckBox defaultForEntity, risky, itemType, hasItemNo, clearance, showSellingPrice;
2105 ankur.sing 56
    @UiField FlexTable headerVendor, vendorTable;
2119 ankur.sing 57
    @UiField FlexTable headerVendorM, vendorTableM;
6838 vikram.rag 58
    @UiField ListBox preferredVendor,preferredInsurer;
2105 ankur.sing 59
 
60
    public ItemForm(){
61
        setText("Add New Item");
62
        setWidget(uiBinder.createAndBindUi(this));
2427 ankur.sing 63
        initVendorKeysHeader();
64
        initVendorPricesHeader();
4762 phani.kuma 65
        preferredVendor.addItem("select");
4725 phani.kuma 66
        for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
67
        	preferredVendor.addItem(e.getValue());
68
        }
5586 phani.kuma 69
        if(!entityIdMandatory) {
70
        	catalogItemId.setEnabled(false);
71
        }
6904 amit.gupta 72
        preferredInsurer.addItem("select", "0");
6838 vikram.rag 73
        for(Entry<Long, String> e : Utils.getAllInsurers().entrySet()){
74
        	preferredInsurer.addItem(e.getValue(),e.getKey().toString());
75
        }
76
 
2105 ankur.sing 77
    }
78
 
2427 ankur.sing 79
    /**
80
     * initialises vendor item key table header. Creates an Add button and
81
     * adds click event listener to it to create and pop up a dialog for adding 
82
     * a new vendor item key. 
83
     */
84
    private void initVendorKeysHeader(){
2119 ankur.sing 85
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
86
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
2359 ankur.sing 87
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
2119 ankur.sing 88
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
89
 
90
        headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
2359 ankur.sing 91
        headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
2119 ankur.sing 92
        headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
93
 
2359 ankur.sing 94
        headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
95
 
2119 ankur.sing 96
        Button addButton = new Button("Add");
97
        headerVendorM.setWidget(0, TABLE_INDEX_ITEM_KEY + 1, addButton);
98
        addButton.addClickHandler(new ClickHandler() {
99
            @Override
100
            public void onClick(ClickEvent event) {
2126 ankur.sing 101
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
102
                        modelNumber.getText().trim(), color.getText().trim());
2119 ankur.sing 103
                vendorMappingDialog.updateButton.setText("Add");
104
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
105
                    @Override
106
                    public boolean onUpdate(String key, long vendorId) {
107
                        final int row = vendorTableM.getRowCount();
108
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
109
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
2359 ankur.sing 110
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
2119 ankur.sing 111
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
112
 
113
                        vendorTableM.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
114
                        vendorTableM.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
115
                        vendorTableM.setText(row, TABLE_INDEX_ITEM_KEY, key);
116
                        Button removeButton = new Button("X");
117
                        vendorTableM.setWidget(row, TABLE_INDEX_ITEM_KEY + 1, removeButton);
2359 ankur.sing 118
 
119
                        vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
2119 ankur.sing 120
                        removeButton.addClickHandler(new ClickHandler() {
121
                            @Override
122
                            public void onClick(ClickEvent event) {
123
                                vendorTableM.removeRow(row);
124
                            }
125
                        });
126
                        return true;
127
                    }
128
                });
129
                vendorMappingDialog.show();
130
            }
131
        });
132
    }
2105 ankur.sing 133
 
2427 ankur.sing 134
    /**
135
     * initialises vendor prices table header. Creates an Add button and
136
     * adds click event listener to it to create and pop up a dialog for adding 
137
     * a prices for a new vendor 
138
     */
139
    private void initVendorPricesHeader(){
2105 ankur.sing 140
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
141
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
142
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
143
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
144
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
6759 amar.kumar 145
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
146
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
2105 ankur.sing 147
 
148
        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
2359 ankur.sing 149
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
2105 ankur.sing 150
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
151
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
152
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
6759 amar.kumar 153
        headerVendor.setText(0, TABLE_INDEX_NLC, "NLC");
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");
6759 amar.kumar 158
        headerVendor.setWidget(0, TABLE_INDEX_NLC + 1, addButton);
2119 ankur.sing 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
6759 amar.kumar 166
                    public boolean onUpdate(double mop, double dp, double tp, double nlc, long vendorId) {
2119 ankur.sing 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");
6759 amar.kumar 180
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
181
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
2119 ankur.sing 182
 
183
                        vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
184
                        vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
185
                        vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
186
                        vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
187
                        vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
6759 amar.kumar 188
                        vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
2359 ankur.sing 189
 
190
                        vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
191
 
2119 ankur.sing 192
                        Button removeButton = new Button("X");
6759 amar.kumar 193
                        vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
2119 ankur.sing 194
                        removeButton.addClickHandler(new ClickHandler() {
195
                            @Override
196
                            public void onClick(ClickEvent event) {
197
                                vendorTable.removeRow(row);
198
                            }
199
                        });
200
                        return true;
201
                    }
202
                });
203
                vendorPricesDialog.show();
204
            }
205
        });
2105 ankur.sing 206
    }
207
 
208
    @UiHandler("addButton")
209
    /**
2359 ankur.sing 210
     * 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 211
     * This item instance is then passed to the service to be added to the database.
2105 ankur.sing 212
     */
213
    void addItem(ClickEvent event) {
5586 phani.kuma 214
    	if(entityIdMandatory) {
215
    		long entityId = 0;
216
    		try {
217
                if(!catalogItemId.getText().trim().isEmpty()) {
218
                    entityId = Long.parseLong(catalogItemId.getText().trim());
219
                    if(entityId <= 0) {
220
                        throw new NumberFormatException("Invalid value of Catalog Item Id");
221
                    }
222
                }
223
                else {
224
                	Window.alert("Catalog Item Id can not be empty");
225
                	return;
226
                }
227
            } catch(NumberFormatException ex) {
228
                Window.alert("Invalid value of Catalog Item Id");
2119 ankur.sing 229
                return;
230
            }
5586 phani.kuma 231
	    	catalogService.checkEntityId(entityId, new AsyncCallback<Boolean>() {
232
	            @Override
233
	            public void onFailure(Throwable caught) {
234
	                GWT.log("Error checking Catalog Item Id");
235
	                Window.alert("Error checking Catalog Item Id");
236
	                return;
237
	            }
238
	            @Override
239
	            public void onSuccess(Boolean result) {
240
		        	if(!result) {
241
	                    Window.alert("Invalid value of Catalog Item Id");
242
	                    return;
243
	                }
244
	                addNewItem();
245
		        }
246
	        });
247
    	}
248
    	else {
249
	        catalogService.checkSimilarItem(brand.getText().trim(), modelNumber.getText().trim(),
250
	        		modelName.getText().trim(), color.getText().trim(), new AsyncCallback<Long>() {
251
	            @Override
252
	            public void onFailure(Throwable caught) {
253
	                GWT.log("Error checking similar item");
254
	                Window.alert("Error checking similar item");
255
	                return;
256
	            }
257
	            @Override
258
	            public void onSuccess(Long result) {
259
	                if(result != 0) {
260
	                    Window.alert("Similar product exists with Item Id " + result);
261
	                    return;
262
	                }
263
	                addNewItem();
264
	            }
265
	        });
266
    	}
2119 ankur.sing 267
    }
268
 
2126 ankur.sing 269
    private void addNewItem() {
2105 ankur.sing 270
        Item item = new Item();
271
 
4762 phani.kuma 272
        if(!productGroup.getText().trim().isEmpty()) {
273
        	item.setProductGroup(productGroup.getText().trim());
274
        }
275
        else {
276
        	Window.alert("Product Group can not be empty.");
277
            return;
278
        }
5586 phani.kuma 279
 
280
        if(entityIdMandatory) {
281
	        try {
282
	            if(!catalogItemId.getText().trim().isEmpty()) {
283
	                long entityId = Long.parseLong(catalogItemId.getText().trim());
284
	                if(entityId <= 0) {
285
	                    throw new NumberFormatException("Invalid value of Catalog Item Id");
286
	                }
287
	                item.setCatalogItemId(entityId);
288
	            }
289
	            else {
290
	            	Window.alert("Catalog Item Id can not be empty");
291
	            	return;
292
	            }
293
	        } catch(NumberFormatException ex) {
294
	            Window.alert("Invalid value of Catalog Item Id");
295
	            return;
296
	        }
297
        }
298
 
2105 ankur.sing 299
        item.setBrand(brand.getText().trim());
300
        item.setModelNumber(modelNumber.getText().trim());
301
        item.setModelName(modelName.getText().trim());
302
        item.setColor(color.getText().trim());
303
        item.setComments(comments.getText().trim());
304
        try {
2126 ankur.sing 305
            if(!mrp.getText().trim().isEmpty()) {
306
                double mrpValue = Double.parseDouble(mrp.getText().trim());
307
                if(mrpValue <= 0) {
308
                    throw new NumberFormatException("Negative value of MRP");
309
                }
310
                item.setMrp(mrpValue);
2105 ankur.sing 311
            }
312
        } catch(NumberFormatException ex) {
2126 ankur.sing 313
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
2105 ankur.sing 314
            return;
315
        }
316
        try {
2126 ankur.sing 317
            if(!sellingPrice.getText().trim().isEmpty()) {
2427 ankur.sing 318
                double spValue = Double.parseDouble(sellingPrice.getText().trim());
319
                if(spValue <= 0) {
320
                    throw new NumberFormatException("Negative value of Selling price");
321
                }
322
                item.setSellingPrice(spValue);
2126 ankur.sing 323
            }
2105 ankur.sing 324
        } catch(NumberFormatException ex) {
2126 ankur.sing 325
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
2105 ankur.sing 326
            return;
327
        }
328
        try {
2126 ankur.sing 329
            if(!weight.getText().trim().isEmpty()) {
2105 ankur.sing 330
                double wtValue = Double.parseDouble(weight.getText().trim());
2126 ankur.sing 331
                if(wtValue <= 0) {
2105 ankur.sing 332
                    throw new NumberFormatException("Negative value of Weight");
333
                }
334
                item.setWeight(wtValue);
335
            }
336
        } catch(NumberFormatException ex) {
2126 ankur.sing 337
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
2105 ankur.sing 338
            return;
339
        }
340
        try {
341
            if(!startDate.getTextBox().getText().trim().equals("")) {
342
                item.setStartDate(startDate.getValue().getTime());
343
            }
344
        } catch(Exception ex) {
345
            Window.alert("Invalid start date format");
346
            return;
347
        }
5217 amit.gupta 348
        if(comingSoonStartDate.getValue()==null){
349
        	item.setComingSoonStartDate(null);
350
        }else {
351
        	item.setComingSoonStartDate(comingSoonStartDate.getValue().getTime());
352
        }
353
        if(expectedArrivalDate.getValue()==null){
354
        	item.setExpectedArrivalDate(null);
355
        }else {
356
        	item.setExpectedArrivalDate(expectedArrivalDate.getValue().getTime());
357
        }
2105 ankur.sing 358
        item.setBestDealsText(bestDealText.getText().trim());
6777 vikram.rag 359
        item.setBestDealsDetailsText(bestDealsDetailsText.getText().trim());
360
        item.setBestDealsDetailsLink(bestDealsDetailsLink.getText().trim());
2105 ankur.sing 361
        try {
362
            if(!bestDealValue.getText().trim().equals("")) {
363
                double bdValue = Double.parseDouble(bestDealValue.getText().trim());
364
                if(bdValue < 0) {
365
                    throw new NumberFormatException("Negative value of BestDealValue");
366
                }
367
                item.setBestDealsValue(bdValue);
368
            }
369
        } catch(NumberFormatException ex) {
370
            Window.alert("Invalid best deal value format");
371
            return;
372
        }
373
        try {
374
            if(!bestSellingRank.getText().trim().equals("")) {
375
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
376
                if(bsrValue < 0) {
377
                    throw new NumberFormatException("Negative value of Best Selling Rank");
378
                }
379
                item.setBestSellingRank(bsrValue);
380
            }
381
        } catch(NumberFormatException ex) {
382
            Window.alert("Invalid best selling rank format");
383
            return;
384
        }
6813 amar.kumar 385
 
386
        try {
387
	        if(!minStockLevel.getText().trim().equals("")) {
388
	            long msl = Long.parseLong(minStockLevel.getText().trim());
389
	            if(msl < 0) {
390
	                throw new NumberFormatException("Negative value of Min Stock Value");
391
	            }
392
	            item.setMinStockLevel(msl);
6830 amar.kumar 393
	        }else {
394
	        	item.setMinStockLevel(0L);
6813 amar.kumar 395
	        }
396
	    } catch(NumberFormatException ex) {
397
	    	item.setMinStockLevel(0L);
398
	        Window.alert("Invalid min Stock Level. Setting it to zero(0)");
399
	        return;
400
	    }
401
 
402
	    try {
403
	        if(!numOfDaysStock.getText().trim().equals("")) {
404
	            Long nds = Long.parseLong(numOfDaysStock.getText().trim());
405
	            if(nds < 0) {
406
	                throw new NumberFormatException("Negative value of Num of Days Of Stock");
407
	            }
408
	            item.setNumOfDaysStock(nds.intValue());
6830 amar.kumar 409
	        } else {
410
	        	item.setNumOfDaysStock(0);
6813 amar.kumar 411
	        }
412
	    } catch(NumberFormatException ex) {
413
	    	item.setNumOfDaysStock(0);
414
	        Window.alert("Invalid min Stock Level. Setting it to zero(0)");
415
	        return;
416
	    }
417
 
7190 amar.kumar 418
	    try {
419
            if(!freebieItemId.getText().trim().equals("")) {
420
                long freeItemId = Long.parseLong(freebieItemId.getText().trim());
421
                if(freeItemId < 0) {
422
                    throw new NumberFormatException("Negative value of freebieItemId ");
423
                }
424
                item.setFreebieItemId(new Long(freeItemId));
425
            }
426
        } catch(NumberFormatException ex) {
427
            Window.alert("Invalid freebie ItemId");
428
            return;
429
        }
430
 
2105 ankur.sing 431
        item.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 432
        item.setRisky(risky.getValue());
5384 phani.kuma 433
        item.setHasItemNo(hasItemNo.getValue());
434
        item.setItemType(itemType.getValue());
4762 phani.kuma 435
        if(preferredVendor.getSelectedIndex() == 0) {
436
        	Window.alert("Invalid Vendor Selected");
437
            return;
438
        }
439
    	else {
440
    		item.setPreferredVendor(Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex())));
441
    	}
2105 ankur.sing 442
 
6838 vikram.rag 443
        item.setPreferredInsurer(Long.parseLong(preferredInsurer.getValue(preferredInsurer.getSelectedIndex())));
444
 
445
 
2119 ankur.sing 446
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 447
          Add the instance to map and set the map to the item instance created above.*/
2119 ankur.sing 448
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
449
        VendorPricings v;
2105 ankur.sing 450
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 451
            v = new VendorPricings();
2105 ankur.sing 452
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
453
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
454
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
455
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
6759 amar.kumar 456
            v.setNlc(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_NLC)));
2105 ankur.sing 457
            vendorPrices.put(v.getVendorId(), v);
458
        }
2119 ankur.sing 459
        item.setVendorPricesMap(vendorPrices);
2105 ankur.sing 460
 
2119 ankur.sing 461
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
462
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 463
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2119 ankur.sing 464
        VendorItemMapping vMapping;
465
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
466
            vMapping = new VendorItemMapping();
467
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
468
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
2359 ankur.sing 469
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
2119 ankur.sing 470
        }
2359 ankur.sing 471
        item.setVendorKeysMap(vendorMappings);
2119 ankur.sing 472
 
473
        if(!Utils.validateItem(item)) {
474
            return;
475
        }
476
 
2427 ankur.sing 477
        /*Service method to add item. */
2105 ankur.sing 478
        catalogService.addItem(item, new AsyncCallback<Long>() {
479
            @Override
480
            public void onSuccess(Long result) {
481
                if(result == null || result == 0) {
482
                    Window.alert("Error while adding item");
483
                    return;
484
                }
485
                Window.alert("Item added successfully. Id = " + result);
486
                hide();
487
            }
488
            @Override
489
            public void onFailure(Throwable caught) {
2427 ankur.sing 490
                caught.printStackTrace();
2105 ankur.sing 491
                Window.alert("Error while adding item");
492
            }
493
        });
494
    }
495
 
496
    @UiHandler("cancelButton")
497
    void closeForm(ClickEvent event) {
498
        this.hide();
499
    }
2427 ankur.sing 500
 
2105 ankur.sing 501
    /**
502
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
503
     */
504
    private boolean vendorExists(long vendorId) {
505
        long id;
506
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
507
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
508
            if(vendorId == id) {
509
                return false;
510
            }
511
        }
512
        return true;
513
    }
514
 
515
    /**
2119 ankur.sing 516
     * validate vendor prices (MOP, DealerPrice, TransferPrice)
2105 ankur.sing 517
     */
2566 chandransh 518
    @SuppressWarnings("unused")
519
	private boolean validateVendorPrices(double mop, double dp, double tp) {
2105 ankur.sing 520
        double mrpValue;
521
        try {
522
            mrpValue = Double.parseDouble(mrp.getText().trim());
523
        } catch (NumberFormatException ex) {
524
            Window.alert("Invalid MRP value.");
525
            return false;
526
        }
527
        if(mrpValue < mop) {
528
            Window.alert("MOP cannot be more than MRP.");
529
            return false;
530
        }
531
        if(tp > mop) {
532
            Window.alert("Transfer Price cannot be more than MOP.");
533
            return false;
534
        }
535
        return true;
536
    }
5217 amit.gupta 537
 
538
	@UiHandler("comingSoonButton")
539
	void onComingSoonButtonClick(ClickEvent event) {
540
		ComingSoonDialog cd = new ComingSoonDialog(this);
541
		cd.show();
542
	}
543
 
544
	public void setComingSoonStartDate(Date date){
545
		this.comingSoonStartDate.setValue(date);
546
	}
547
	public void setBestDealsText(String bestDealsText){
548
		this.bestDealText.setText(bestDealsText);
549
	}
550
 
551
	public void setExpectedArrivalDate(Date date){
552
		this.expectedArrivalDate.setValue(date);
553
	}
554
 
6241 amit.gupta 555
	public void setShowPrice(boolean b){
556
		this.showSellingPrice.setValue(b);
557
	}
558
 
559
	public boolean isShowPrice(){
560
		return this.showSellingPrice.getValue();
561
	}
562
 
5217 amit.gupta 563
	@Override
564
	public String getBestDealsText() {
565
		return this.bestDealText.getValue();
566
	}
567
 
568
	@Override
569
	public Date getComingSoonStartDate() {
570
		return this.comingSoonStartDate.getValue();
571
	}
572
 
573
	@Override
574
	public Date getExpectedArrivalDate() {
6241 amit.gupta 575
		return this.expectedArrivalDate.getValue();
5217 amit.gupta 576
	}
6777 vikram.rag 577
 
578
	public TextBox getBestDealsDetailsText() {
579
		return bestDealsDetailsText;
580
	}
581
 
582
	public void setBestDealsDetailsText(TextBox bestDealsDetailsText) {
583
		this.bestDealsDetailsText = bestDealsDetailsText;
584
	}
585
 
586
	public TextBox getBestDealsDetailsLink() {
587
		return bestDealsDetailsLink;
588
	}
589
 
590
	public void setBestDealsDetailsLink(TextBox bestDealsDetailsLink) {
591
		this.bestDealsDetailsLink = bestDealsDetailsLink;
592
	}
2066 ankur.sing 593
}