Subversion Repositories SmartDukaan

Rev

Rev 8901 | Rev 18048 | 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
 
8901 vikram.rag 36
	interface ItemFormUiBinder extends UiBinder<Widget, ItemForm> {}
37
	private static ItemFormUiBinder uiBinder = GWT.create(ItemFormUiBinder.class);
2105 ankur.sing 38
 
8901 vikram.rag 39
	private final static CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
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,
45
	TABLE_INDEX_TP = 4,
46
	TABLE_INDEX_NLC = 5;
2119 ankur.sing 47
 
8901 vikram.rag 48
	private static boolean entityIdMandatory = Utils.isEntityIdMandatory();
2105 ankur.sing 49
 
8901 vikram.rag 50
	@UiField TextBox productGroup, catalogItemId,existingItemId;
51
	@UiField TextBox brand, modelNumber, modelName, color, comments,holdInventory,defaultInventory;
52
	@UiField TextBox mrp, sellingPrice, weight, bestDealText, bestDealValue, bestSellingRank,bestDealsDetailsText,bestDealsDetailsLink, minStockLevel, numOfDaysStock, freebieItemId,asin;
53
	@UiField DateBox startDate, retireDate, comingSoonStartDate, expectedArrivalDate;
54
	@UiField Button addButton, cancelButton, comingSoonButton,searchButton;
55
	@UiField CheckBox defaultForEntity, risky, itemType, hasItemNo, clearance, showSellingPrice;
56
	@UiField FlexTable headerVendor, vendorTable;
57
	@UiField FlexTable headerVendorM, vendorTableM;
58
	@UiField ListBox preferredVendor,preferredInsurer;
59
 
60
	public ItemForm(){
61
		setText("Add New Item");
62
		setWidget(uiBinder.createAndBindUi(this));
63
		initVendorKeysHeader();
64
		initVendorPricesHeader();
65
		preferredVendor.addItem("select");
66
		for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
67
			preferredVendor.addItem(e.getValue());
68
		}
69
		if(!entityIdMandatory) {
70
			catalogItemId.setEnabled(false);
71
		}
72
		preferredInsurer.addItem("select", "0");
73
		for(Entry<Long, String> e : Utils.getAllInsurers().entrySet()){
74
			preferredInsurer.addItem(e.getValue(),e.getKey().toString());
75
		}
76
 
77
	}
78
 
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(){
85
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
86
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
87
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
88
		headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY + 1, "50px");
89
 
90
		headerVendorM.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
91
		headerVendorM.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
92
		headerVendorM.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
93
 
94
		headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
95
 
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) {
101
				VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
102
						modelNumber.getText().trim(), color.getText().trim());
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");
110
						vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "400px");
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);
118
 
119
						vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
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
	}
133
 
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(){
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");
145
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
146
		headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
147
 
148
		headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
149
		headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor");
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");
153
		headerVendor.setText(0, TABLE_INDEX_NLC, "NLC");
154
 
155
		headerVendor.getCellFormatter().setVisible(0, TABLE_INDEX_VENDORID, false);
156
 
157
		Button addButton = new Button("Add");
158
		headerVendor.setWidget(0, TABLE_INDEX_NLC + 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, double nlc, long vendorId) {
167
						if(!vendorExists(vendorId)) {
168
							Window.alert("Vendor already exists");
169
							return false;
170
						}
171
						/*if(!validateVendorPrices(mop, dp, tp)) {
2119 ankur.sing 172
                            return false;
173
                        }*/
8901 vikram.rag 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_NLC, "128px");
181
						vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
2105 ankur.sing 182
 
8901 vikram.rag 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 + "");
188
						vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
189
 
190
						vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
191
 
192
						Button removeButton = new Button("X");
193
						vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
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
		});
206
	}
207
	@UiHandler("searchButton")
208
	/**
209
	 * On search button click the item id is searched if it exists then the attributes of the item id are filled into the item form.
210
	 * Else an alert is displayed showing item id does not exists.
211
	 */
212
	void searchItem(ClickEvent event){
213
		preferredVendor.clear();
214
		preferredInsurer.clear();  
215
		catalogItemId.setText(null);
216
		productGroup.setText(null);
217
		brand.setText(null);
218
		modelNumber.setText(null);
219
		modelName.setText(null);
220
		comments.setText(null);
221
		mrp.setText(null);
222
		sellingPrice.setText(null);
223
		weight.setText(null);
224
		hasItemNo.setValue(null);
225
		itemType.setValue(null);
226
		showSellingPrice.setValue(null);
227
		startDate.setValue(null);
228
		this.comingSoonStartDate.setValue(null);
229
		this.expectedArrivalDate.setValue(null);
230
		bestDealText.setText(null);
231
		bestDealsDetailsText.setText(null);
232
		bestDealsDetailsLink.setText(null);
233
		bestDealValue.setText(null);
234
		showSellingPrice.setValue(null);
235
		bestSellingRank.setText(null);
236
		minStockLevel.setText(null);
237
		numOfDaysStock.setText(null);
238
		defaultForEntity.setValue(null);
239
		freebieItemId.setText(null);
240
		asin.setText(null);
241
		holdInventory.setText(null);
242
		defaultInventory.setText(null);
243
		color.setText(null);
244
		vendorTable.removeAllRows(); 
245
		if(!existingItemId.getText().trim().isEmpty()){
246
			long id = Long.parseLong(existingItemId.getText().trim());
247
 
248
			if(id <= 0) {
249
				throw new NumberFormatException("Invalid value of Item Id");
250
			}
251
			else{
252
				catalogService.getItem(id, new AsyncCallback<Item>() {
253
					@Override
254
					public void onFailure(Throwable caught) {
255
						Window.alert("Unable to fetch item details.");
256
					}
257
					@Override
258
					public void onSuccess(Item result) {
259
						if(result !=null){
260
							fillItemForm(result);
261
						}
262
						else{
263
							Window.alert("Invalid Item Id not found");
264
						}
265
					}
266
				});
267
 
268
			}
269
		}
270
		else{
271
			Window.alert("Please enter item id");
272
			GWT.log("Please enter an item id ");
273
		}
274
	}
275
 
276
 
277
	/**
278
	 * This method is called if the valid item is returned and it populates the item entries in Item Form.
279
	 * 
280
	 */
281
	public void fillItemForm(Item item){
282
		int index = 0;
283
		preferredVendor.addItem("select");
284
        if (item.getPreferredVendor() == null) {
285
        	preferredVendor.setSelectedIndex(index);
286
        }
2105 ankur.sing 287
 
8901 vikram.rag 288
        if((item.getVendorPricesMap() == null || item.getVendorPricesMap().isEmpty())) {
289
        	if(item.getPreferredVendor() != null) {
290
        		index++;
291
        		preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
292
        	}
4762 phani.kuma 293
        }
294
        else {
8901 vikram.rag 295
        	for(VendorPricings vendorDetail : item.getVendorPricesMap().values()){
296
        		index++;
297
        		preferredVendor.addItem(Utils.getVendorDesc(vendorDetail.getVendorId()));
298
        		if(item.getPreferredVendor() != null && item.getPreferredVendor() == vendorDetail.getVendorId()) {
299
	        		preferredVendor.setSelectedIndex(index);
300
	        	}
301
        	}
302
 
303
        	if(item.getPreferredVendor() != null && !item.getVendorPricesMap().containsKey(item.getPreferredVendor())){
304
        		index++;
305
            	preferredVendor.addItem(Utils.getVendorDesc(item.getPreferredVendor()));
306
            	preferredVendor.setSelectedIndex(index);
307
        	}
4762 phani.kuma 308
        }
8901 vikram.rag 309
        index=0;
310
        preferredInsurer.addItem("select","0");
311
        preferredInsurer.setSelectedIndex(0);
312
    	for (Map.Entry<Long, String> entry : Utils.getAllInsurers().entrySet()){
313
    		index++;
314
    		preferredInsurer.addItem(entry.getValue(),entry.getKey().toString());
315
    		if(item.getPreferredInsurer()==entry.getKey()){
316
    			preferredInsurer.setSelectedIndex(index);  
317
    		}
4762 phani.kuma 318
    	}
8901 vikram.rag 319
		catalogItemId.setText(Long.toString(item.getCatalogItemId()));
320
		productGroup.setText(item.getProductGroup());
321
		brand.setText(item.getBrand());
322
		modelNumber.setText(item.getModelNumber());
323
		modelName.setText(item.getModelName());
324
		comments.setText(item.getComments());
325
		mrp.setText(item.getMrp() != null ? item.getMrp()+"" : "");
326
		sellingPrice.setText(item.getSellingPrice() != null ? item.getSellingPrice()+"" : "");
327
		weight.setText(item.getWeight() != null ? item.getWeight()+"" : "");
328
		hasItemNo.setValue(item.isHasItemNo());
329
		itemType.setValue(item.isItemType());
330
		showSellingPrice.setValue(item.isShowSellingPrice());
331
		startDate.setValue(new Date(item.getStartDate()));
332
		if(item.getComingSoonStartDate() != null){
333
			this.comingSoonStartDate.setValue(new Date(item.getComingSoonStartDate()));
334
		}else {
335
			this.comingSoonStartDate.setValue(null);
336
		}
337
		if(item.getExpectedArrivalDate() != null){
338
			this.expectedArrivalDate.setValue(new Date(item.getExpectedArrivalDate()));
339
		}else {
340
			this.expectedArrivalDate.setValue(null);
341
		}
342
		bestDealText.setText(item.getBestDealsText());
343
		bestDealsDetailsText.setText(item.getBestDealsDetailsText());
344
		bestDealsDetailsLink.setText(item.getBestDealsDetailsLink());
345
		bestDealValue.setText(item.getBestDealsValue() != null ? item.getBestDealsValue()+"" : "");
346
		showSellingPrice.setValue(item.isShowSellingPrice());
347
		bestSellingRank.setText(item.getBestSellingRank() != null ? item.getBestSellingRank()+"" : "");
348
		minStockLevel.setText(item.getMinStockLevel() != null ? item.getMinStockLevel()+"" : "");
349
		numOfDaysStock.setText(item.getNumOfDaysStock() != null ? item.getNumOfDaysStock()+"" : "");
350
		defaultForEntity.setValue(item.isDefaultForEntity());
351
		freebieItemId.setText(item.getFreebieItemId().toString());
352
		holdInventory.setText(Long.toString(item.getHoldInventory()));
353
		defaultInventory.setText(Long.toString(item.getDefaultInventory()));
354
		Map<Long, VendorPricings> map = item.getVendorPricesMap();
355
		for (Map.Entry<Long, VendorPricings> vendorItem : map.entrySet()) {
356
			final int row = vendorTable.getRowCount();
357
			long vendorId = vendorItem.getKey();
358
			double mop = vendorItem.getValue().getMop();
359
			double dp  = vendorItem.getValue().getDealerPrice();
360
			double tp =  vendorItem.getValue().getTransferPrice();
361
			double nlc = vendorItem.getValue().getNlc();
362
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
363
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
364
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
365
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
366
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
367
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
368
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
369
			vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
370
			vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
371
			vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
372
			vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
373
			vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
374
			vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
375
			vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
376
			Button removeButton = new Button("X");
377
			vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
378
			removeButton.addClickHandler(new ClickHandler() {
379
				@Override
380
				public void onClick(ClickEvent event) {
381
					vendorTable.removeRow(row);
382
				}
383
			});
384
		}
385
	}
386
 
387
 
388
	@UiHandler("addButton")
389
	/**
390
	 * On add button click a new Item instance is created and all the information from UI fields is dumped in this item instance.
391
	 * This item instance is then passed to the service to be added to the database.
392
	 */
393
	void addItem(ClickEvent event) {
394
		if(entityIdMandatory) {
395
			long entityId = 0;
396
			try {
397
				if(!catalogItemId.getText().trim().isEmpty()) {
398
					entityId = Long.parseLong(catalogItemId.getText().trim());
399
					if(entityId <= 0) {
400
						throw new NumberFormatException("Invalid value of Catalog Item Id");
401
					}
402
				}
403
				else {
404
					Window.alert("Catalog Item Id can not be empty");
405
					return;
406
				}
407
			} catch(NumberFormatException ex) {
408
				Window.alert("Invalid value of Catalog Item Id");
409
				return;
410
			}
411
			catalogService.checkEntityId(entityId, new AsyncCallback<Boolean>() {
412
				@Override
413
				public void onFailure(Throwable caught) {
414
					GWT.log("Error checking Catalog Item Id");
415
					Window.alert("Error checking Catalog Item Id");
416
					return;
417
				}
418
				@Override
419
				public void onSuccess(Boolean result) {
420
					if(!result) {
421
						Window.alert("Invalid value of Catalog Item Id");
422
						return;
423
					}
424
					addNewItem();
425
				}
426
			});
427
		}
428
		else {
429
			catalogService.checkSimilarItem(brand.getText().trim(), modelNumber.getText().trim(),
430
					modelName.getText().trim(), color.getText().trim(), new AsyncCallback<Long>() {
431
				@Override
432
				public void onFailure(Throwable caught) {
433
					GWT.log("Error checking similar item");
434
					Window.alert("Error checking similar item");
435
					return;
436
				}
437
				@Override
438
				public void onSuccess(Long result) {
439
					if(result != 0) {
440
						Window.alert("Similar product exists with Item Id " + result);
441
						return;
442
					}
443
					addNewItem();
444
				}
445
			});
446
		}
447
	}
448
 
449
	private void addNewItem() {
450
		Item item = new Item();
451
		if(!productGroup.getText().trim().isEmpty()) {
452
			item.setProductGroup(productGroup.getText().trim());
453
		}
454
		else {
455
			Window.alert("Product Group can not be empty.");
456
			return;
457
		}
458
 
459
		if(entityIdMandatory) {
460
			try {
461
				if(!catalogItemId.getText().trim().isEmpty()) {
462
					long entityId = Long.parseLong(catalogItemId.getText().trim());
463
					if(entityId <= 0) {
464
						throw new NumberFormatException("Invalid value of Catalog Item Id");
465
					}
466
					item.setCatalogItemId(entityId);
467
				}
468
				else {
469
					Window.alert("Catalog Item Id can not be empty");
470
					return;
471
				}
472
			} catch(NumberFormatException ex) {
473
				Window.alert("Invalid value of Catalog Item Id");
474
				return;
475
			}
476
		}
477
		item.setBrand(brand.getText().trim());
478
		item.setModelNumber(modelNumber.getText().trim());
479
		item.setModelName(modelName.getText().trim());
480
		item.setColor(color.getText().trim());
481
		item.setComments(comments.getText().trim());
482
		try {
483
			if(!mrp.getText().trim().isEmpty()) {
484
				double mrpValue = Double.parseDouble(mrp.getText().trim());
485
				if(mrpValue <= 0) {
486
					throw new NumberFormatException("Negative value of MRP");
487
				}
488
				item.setMrp(mrpValue);
489
			}
490
		} catch(NumberFormatException ex) {
491
			Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
492
			return;
493
		}
494
		try {
495
			if(!sellingPrice.getText().trim().isEmpty()) {
496
				double spValue = Double.parseDouble(sellingPrice.getText().trim());
497
				if(spValue <= 0) {
498
					throw new NumberFormatException("Negative value of Selling price");
499
				}
500
				item.setSellingPrice(spValue);
501
			}
502
		} catch(NumberFormatException ex) {
503
			Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
504
			return;
505
		}
506
		try {
507
			if(!weight.getText().trim().isEmpty()) {
508
				double wtValue = Double.parseDouble(weight.getText().trim());
509
				if(wtValue <= 0) {
510
					throw new NumberFormatException("Negative value of Weight");
511
				}
512
				item.setWeight(wtValue);
513
			}
514
		} catch(NumberFormatException ex) {
515
			Window.alert("Invalid weight format/value. Value shoule be greater than zero");
516
			return;
517
		}
518
		try {
519
			if(!startDate.getTextBox().getText().trim().equals("")) {
520
				item.setStartDate(startDate.getValue().getTime());
521
			}
522
		} catch(Exception ex) {
523
			Window.alert("Invalid start date format");
524
			return;
525
		}
526
		if(comingSoonStartDate.getValue()==null){
527
			item.setComingSoonStartDate(null);
528
		}else {
529
			item.setComingSoonStartDate(comingSoonStartDate.getValue().getTime());
530
		}
531
		if(expectedArrivalDate.getValue()==null){
532
			item.setExpectedArrivalDate(null);
533
		}else {
534
			item.setExpectedArrivalDate(expectedArrivalDate.getValue().getTime());
535
		}
536
		item.setBestDealsText(bestDealText.getText().trim());
537
		item.setBestDealsDetailsText(bestDealsDetailsText.getText().trim());
538
		item.setBestDealsDetailsLink(bestDealsDetailsLink.getText().trim());
539
		try {
540
			if(!bestDealValue.getText().trim().equals("")) {
541
				double bdValue = Double.parseDouble(bestDealValue.getText().trim());
542
				if(bdValue < 0) {
543
					throw new NumberFormatException("Negative value of BestDealValue");
544
				}
545
				item.setBestDealsValue(bdValue);
546
			}
547
		} catch(NumberFormatException ex) {
548
			Window.alert("Invalid best deal value format");
549
			return;
550
		}
551
		try {
552
			if(!bestSellingRank.getText().trim().equals("")) {
553
				long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
554
				if(bsrValue < 0) {
555
					throw new NumberFormatException("Negative value of Best Selling Rank");
556
				}
557
				item.setBestSellingRank(bsrValue);
558
			}
559
		} catch(NumberFormatException ex) {
560
			Window.alert("Invalid best selling rank format");
561
			return;
562
		}
563
 
564
		try {
565
			if(!minStockLevel.getText().trim().equals("")) {
566
				long msl = Long.parseLong(minStockLevel.getText().trim());
567
				if(msl < 0) {
568
					throw new NumberFormatException("Negative value of Min Stock Value");
569
				}
570
				item.setMinStockLevel(msl);
571
			}else {
572
				item.setMinStockLevel(0L);
573
			}
574
		} catch(NumberFormatException ex) {
575
			item.setMinStockLevel(0L);
576
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
577
			return;
578
		}
579
 
580
		try {
581
			if(!numOfDaysStock.getText().trim().equals("")) {
582
				Long nds = Long.parseLong(numOfDaysStock.getText().trim());
583
				if(nds < 0) {
584
					throw new NumberFormatException("Negative value of Num of Days Of Stock");
585
				}
586
				item.setNumOfDaysStock(nds.intValue());
587
			} else {
588
				item.setNumOfDaysStock(0);
589
			}
590
		} catch(NumberFormatException ex) {
591
			item.setNumOfDaysStock(0);
592
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
593
			return;
594
		}
595
 
596
		try {
597
			if(!freebieItemId.getText().trim().equals("")) {
598
				long freeItemId = Long.parseLong(freebieItemId.getText().trim());
599
				if(freeItemId < 0) {
600
					throw new NumberFormatException("Negative value of freebieItemId ");
601
				}
602
				item.setFreebieItemId(new Long(freeItemId));
603
			}
604
		} catch(NumberFormatException ex) {
605
			Window.alert("Invalid freebie ItemId");
606
			return;
607
		}
608
		item.setAsin(asin.getText().trim());
609
 
610
		try {
611
			if(!holdInventory.getText().trim().equals("")) {
612
				long hold_inventory = Long.parseLong(holdInventory.getText().trim());
613
				if(hold_inventory < 0) {
614
					throw new NumberFormatException("Negative value of Hold Inventory");
615
				}
616
				item.setHoldInventory(hold_inventory);
617
			}
618
		} catch(NumberFormatException ex) {
619
			Window.alert("Invalid Hold Inventory Value");
620
			return;
621
		}
622
 
623
		try {
624
			if(!defaultInventory.getText().trim().equals("")) {
625
				long default_inventory = Long.parseLong(defaultInventory.getText().trim());
626
				if(default_inventory < 0) {
627
					throw new NumberFormatException("Negative value of Default Inventory");
628
				}
629
				item.setDefaultInventory(default_inventory);
630
			}
631
		} catch(NumberFormatException ex) {
632
			Window.alert("Invalid Default Inventory Value");
633
			return;
634
		}
635
		item.setDefaultForEntity(defaultForEntity.getValue());
636
		item.setRisky(risky.getValue());
637
		item.setHasItemNo(hasItemNo.getValue());
638
		item.setItemType(itemType.getValue());
639
		if(preferredVendor.getSelectedIndex() == 0) {
640
			Window.alert("Invalid Vendor Selected");
641
			return;
642
		}
643
		else {
644
			item.setPreferredVendor(Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex())));
645
		}
646
		item.setPreferredInsurer(Long.parseLong(preferredInsurer.getValue(preferredInsurer.getSelectedIndex())));
647
		/*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 648
          Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 649
		Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
650
		VendorPricings v;
651
		for(int row = 0; row < vendorTable.getRowCount(); row++) {
652
			v = new VendorPricings();
653
			v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
654
			v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
655
			v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
656
			v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
657
			v.setNlc(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_NLC)));
658
			vendorPrices.put(v.getVendorId(), v);
659
		}
660
		item.setVendorPricesMap(vendorPrices);
661
 
662
		/*Create an instance of VendorKeys for each row in vendor pricing table. Set the vendor prices to the instance.
2119 ankur.sing 663
        Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 664
		Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
665
		VendorItemMapping vMapping;
666
		for(int row = 0; row < vendorTableM.getRowCount(); row++) {
667
			vMapping = new VendorItemMapping();
668
			vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
669
			vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
670
			vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
671
		}
672
		item.setVendorKeysMap(vendorMappings);
673
		if(!Utils.validateItem(item)) {
674
			return;
675
		}
2427 ankur.sing 676
 
8901 vikram.rag 677
		/*Service method to add item. */
678
		catalogService.addItem(item, new AsyncCallback<Long>() {
679
			@Override
680
			public void onSuccess(Long result) {
681
				if(result == null || result == 0) {
682
					Window.alert("Error while adding item");
683
					return;
684
				}
685
				Window.alert("Item added successfully. Id = " + result);
686
				hide();
687
			}
688
			@Override
689
			public void onFailure(Throwable caught) {
690
				caught.printStackTrace();
691
				Window.alert("Error while adding item");
692
			}
693
		});
694
	}
695
 
696
	@UiHandler("cancelButton")
697
	void closeForm(ClickEvent event) {
698
		this.hide();
699
	}
700
 
701
	/**
702
	 * Checks if vendor details already exists corresponding to the vendor Id parameter. 
703
	 */
704
	private boolean vendorExists(long vendorId) {
705
		long id;
706
		for(int i = 0; i < vendorTable.getRowCount(); i++) {
707
			id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
708
			if(vendorId == id) {
709
				return false;
710
			}
711
		}
712
		return true;
713
	}
714
 
715
	/**
716
	 * validate vendor prices (MOP, DealerPrice, TransferPrice)
717
	 */
718
	@SuppressWarnings("unused")
2566 chandransh 719
	private boolean validateVendorPrices(double mop, double dp, double tp) {
8901 vikram.rag 720
		double mrpValue;
721
		try {
722
			mrpValue = Double.parseDouble(mrp.getText().trim());
723
		} catch (NumberFormatException ex) {
724
			Window.alert("Invalid MRP value.");
725
			return false;
726
		}
727
		if(mrpValue < mop) {
728
			Window.alert("MOP cannot be more than MRP.");
729
			return false;
730
		}
731
		if(tp > mop) {
732
			Window.alert("Transfer Price cannot be more than MOP.");
733
			return false;
734
		}
735
		return true;
736
	}
737
 
5217 amit.gupta 738
	@UiHandler("comingSoonButton")
739
	void onComingSoonButtonClick(ClickEvent event) {
740
		ComingSoonDialog cd = new ComingSoonDialog(this);
741
		cd.show();
742
	}
743
 
744
	public void setComingSoonStartDate(Date date){
745
		this.comingSoonStartDate.setValue(date);
746
	}
747
	public void setBestDealsText(String bestDealsText){
748
		this.bestDealText.setText(bestDealsText);
749
	}
750
 
751
	public void setExpectedArrivalDate(Date date){
752
		this.expectedArrivalDate.setValue(date);
753
	}
754
 
6241 amit.gupta 755
	public void setShowPrice(boolean b){
756
		this.showSellingPrice.setValue(b);
757
	}
758
 
759
	public boolean isShowPrice(){
760
		return this.showSellingPrice.getValue();
761
	}
762
 
5217 amit.gupta 763
	@Override
764
	public String getBestDealsText() {
765
		return this.bestDealText.getValue();
766
	}
767
 
768
	@Override
769
	public Date getComingSoonStartDate() {
770
		return this.comingSoonStartDate.getValue();
771
	}
772
 
773
	@Override
774
	public Date getExpectedArrivalDate() {
6241 amit.gupta 775
		return this.expectedArrivalDate.getValue();
5217 amit.gupta 776
	}
6777 vikram.rag 777
 
778
	public TextBox getBestDealsDetailsText() {
779
		return bestDealsDetailsText;
780
	}
781
 
782
	public void setBestDealsDetailsText(TextBox bestDealsDetailsText) {
783
		this.bestDealsDetailsText = bestDealsDetailsText;
784
	}
785
 
786
	public TextBox getBestDealsDetailsLink() {
787
		return bestDealsDetailsLink;
788
	}
789
 
790
	public void setBestDealsDetailsLink(TextBox bestDealsDetailsLink) {
791
		this.bestDealsDetailsLink = bestDealsDetailsLink;
792
	}
2066 ankur.sing 793
}