Subversion Repositories SmartDukaan

Rev

Rev 7291 | Rev 8904 | 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
		asin.setText(item.getAsin());
353
		holdInventory.setText(Long.toString(item.getHoldInventory()));
354
		defaultInventory.setText(Long.toString(item.getDefaultInventory()));
355
		Map<Long, VendorPricings> map = item.getVendorPricesMap();
356
		for (Map.Entry<Long, VendorPricings> vendorItem : map.entrySet()) {
357
			final int row = vendorTable.getRowCount();
358
			long vendorId = vendorItem.getKey();
359
			double mop = vendorItem.getValue().getMop();
360
			double dp  = vendorItem.getValue().getDealerPrice();
361
			double tp =  vendorItem.getValue().getTransferPrice();
362
			double nlc = vendorItem.getValue().getNlc();
363
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
364
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
365
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
366
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
367
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
368
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC, "128px");
369
			vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_NLC + 1, "50px");
370
			vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId + "");
371
			vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
372
			vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
373
			vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
374
			vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
375
			vendorTable.setText(row, TABLE_INDEX_NLC, nlc + "");
376
			vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_VENDORID, false);
377
			Button removeButton = new Button("X");
378
			vendorTable.setWidget(row, TABLE_INDEX_NLC + 1, removeButton);
379
			removeButton.addClickHandler(new ClickHandler() {
380
				@Override
381
				public void onClick(ClickEvent event) {
382
					vendorTable.removeRow(row);
383
				}
384
			});
385
		}
386
	}
387
 
388
 
389
	@UiHandler("addButton")
390
	/**
391
	 * On add button click a new Item instance is created and all the information from UI fields is dumped in this item instance.
392
	 * This item instance is then passed to the service to be added to the database.
393
	 */
394
	void addItem(ClickEvent event) {
395
		if(entityIdMandatory) {
396
			long entityId = 0;
397
			try {
398
				if(!catalogItemId.getText().trim().isEmpty()) {
399
					entityId = Long.parseLong(catalogItemId.getText().trim());
400
					if(entityId <= 0) {
401
						throw new NumberFormatException("Invalid value of Catalog Item Id");
402
					}
403
				}
404
				else {
405
					Window.alert("Catalog Item Id can not be empty");
406
					return;
407
				}
408
			} catch(NumberFormatException ex) {
409
				Window.alert("Invalid value of Catalog Item Id");
410
				return;
411
			}
412
			catalogService.checkEntityId(entityId, new AsyncCallback<Boolean>() {
413
				@Override
414
				public void onFailure(Throwable caught) {
415
					GWT.log("Error checking Catalog Item Id");
416
					Window.alert("Error checking Catalog Item Id");
417
					return;
418
				}
419
				@Override
420
				public void onSuccess(Boolean result) {
421
					if(!result) {
422
						Window.alert("Invalid value of Catalog Item Id");
423
						return;
424
					}
425
					addNewItem();
426
				}
427
			});
428
		}
429
		else {
430
			catalogService.checkSimilarItem(brand.getText().trim(), modelNumber.getText().trim(),
431
					modelName.getText().trim(), color.getText().trim(), new AsyncCallback<Long>() {
432
				@Override
433
				public void onFailure(Throwable caught) {
434
					GWT.log("Error checking similar item");
435
					Window.alert("Error checking similar item");
436
					return;
437
				}
438
				@Override
439
				public void onSuccess(Long result) {
440
					if(result != 0) {
441
						Window.alert("Similar product exists with Item Id " + result);
442
						return;
443
					}
444
					addNewItem();
445
				}
446
			});
447
		}
448
	}
449
 
450
	private void addNewItem() {
451
		Item item = new Item();
452
		if(!productGroup.getText().trim().isEmpty()) {
453
			item.setProductGroup(productGroup.getText().trim());
454
		}
455
		else {
456
			Window.alert("Product Group can not be empty.");
457
			return;
458
		}
459
 
460
		if(entityIdMandatory) {
461
			try {
462
				if(!catalogItemId.getText().trim().isEmpty()) {
463
					long entityId = Long.parseLong(catalogItemId.getText().trim());
464
					if(entityId <= 0) {
465
						throw new NumberFormatException("Invalid value of Catalog Item Id");
466
					}
467
					item.setCatalogItemId(entityId);
468
				}
469
				else {
470
					Window.alert("Catalog Item Id can not be empty");
471
					return;
472
				}
473
			} catch(NumberFormatException ex) {
474
				Window.alert("Invalid value of Catalog Item Id");
475
				return;
476
			}
477
		}
478
		item.setBrand(brand.getText().trim());
479
		item.setModelNumber(modelNumber.getText().trim());
480
		item.setModelName(modelName.getText().trim());
481
		item.setColor(color.getText().trim());
482
		item.setComments(comments.getText().trim());
483
		try {
484
			if(!mrp.getText().trim().isEmpty()) {
485
				double mrpValue = Double.parseDouble(mrp.getText().trim());
486
				if(mrpValue <= 0) {
487
					throw new NumberFormatException("Negative value of MRP");
488
				}
489
				item.setMrp(mrpValue);
490
			}
491
		} catch(NumberFormatException ex) {
492
			Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
493
			return;
494
		}
495
		try {
496
			if(!sellingPrice.getText().trim().isEmpty()) {
497
				double spValue = Double.parseDouble(sellingPrice.getText().trim());
498
				if(spValue <= 0) {
499
					throw new NumberFormatException("Negative value of Selling price");
500
				}
501
				item.setSellingPrice(spValue);
502
			}
503
		} catch(NumberFormatException ex) {
504
			Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
505
			return;
506
		}
507
		try {
508
			if(!weight.getText().trim().isEmpty()) {
509
				double wtValue = Double.parseDouble(weight.getText().trim());
510
				if(wtValue <= 0) {
511
					throw new NumberFormatException("Negative value of Weight");
512
				}
513
				item.setWeight(wtValue);
514
			}
515
		} catch(NumberFormatException ex) {
516
			Window.alert("Invalid weight format/value. Value shoule be greater than zero");
517
			return;
518
		}
519
		try {
520
			if(!startDate.getTextBox().getText().trim().equals("")) {
521
				item.setStartDate(startDate.getValue().getTime());
522
			}
523
		} catch(Exception ex) {
524
			Window.alert("Invalid start date format");
525
			return;
526
		}
527
		if(comingSoonStartDate.getValue()==null){
528
			item.setComingSoonStartDate(null);
529
		}else {
530
			item.setComingSoonStartDate(comingSoonStartDate.getValue().getTime());
531
		}
532
		if(expectedArrivalDate.getValue()==null){
533
			item.setExpectedArrivalDate(null);
534
		}else {
535
			item.setExpectedArrivalDate(expectedArrivalDate.getValue().getTime());
536
		}
537
		item.setBestDealsText(bestDealText.getText().trim());
538
		item.setBestDealsDetailsText(bestDealsDetailsText.getText().trim());
539
		item.setBestDealsDetailsLink(bestDealsDetailsLink.getText().trim());
540
		try {
541
			if(!bestDealValue.getText().trim().equals("")) {
542
				double bdValue = Double.parseDouble(bestDealValue.getText().trim());
543
				if(bdValue < 0) {
544
					throw new NumberFormatException("Negative value of BestDealValue");
545
				}
546
				item.setBestDealsValue(bdValue);
547
			}
548
		} catch(NumberFormatException ex) {
549
			Window.alert("Invalid best deal value format");
550
			return;
551
		}
552
		try {
553
			if(!bestSellingRank.getText().trim().equals("")) {
554
				long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
555
				if(bsrValue < 0) {
556
					throw new NumberFormatException("Negative value of Best Selling Rank");
557
				}
558
				item.setBestSellingRank(bsrValue);
559
			}
560
		} catch(NumberFormatException ex) {
561
			Window.alert("Invalid best selling rank format");
562
			return;
563
		}
564
 
565
		try {
566
			if(!minStockLevel.getText().trim().equals("")) {
567
				long msl = Long.parseLong(minStockLevel.getText().trim());
568
				if(msl < 0) {
569
					throw new NumberFormatException("Negative value of Min Stock Value");
570
				}
571
				item.setMinStockLevel(msl);
572
			}else {
573
				item.setMinStockLevel(0L);
574
			}
575
		} catch(NumberFormatException ex) {
576
			item.setMinStockLevel(0L);
577
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
578
			return;
579
		}
580
 
581
		try {
582
			if(!numOfDaysStock.getText().trim().equals("")) {
583
				Long nds = Long.parseLong(numOfDaysStock.getText().trim());
584
				if(nds < 0) {
585
					throw new NumberFormatException("Negative value of Num of Days Of Stock");
586
				}
587
				item.setNumOfDaysStock(nds.intValue());
588
			} else {
589
				item.setNumOfDaysStock(0);
590
			}
591
		} catch(NumberFormatException ex) {
592
			item.setNumOfDaysStock(0);
593
			Window.alert("Invalid min Stock Level. Setting it to zero(0)");
594
			return;
595
		}
596
 
597
		try {
598
			if(!freebieItemId.getText().trim().equals("")) {
599
				long freeItemId = Long.parseLong(freebieItemId.getText().trim());
600
				if(freeItemId < 0) {
601
					throw new NumberFormatException("Negative value of freebieItemId ");
602
				}
603
				item.setFreebieItemId(new Long(freeItemId));
604
			}
605
		} catch(NumberFormatException ex) {
606
			Window.alert("Invalid freebie ItemId");
607
			return;
608
		}
609
		item.setAsin(asin.getText().trim());
610
 
611
		try {
612
			if(!holdInventory.getText().trim().equals("")) {
613
				long hold_inventory = Long.parseLong(holdInventory.getText().trim());
614
				if(hold_inventory < 0) {
615
					throw new NumberFormatException("Negative value of Hold Inventory");
616
				}
617
				item.setHoldInventory(hold_inventory);
618
			}
619
		} catch(NumberFormatException ex) {
620
			Window.alert("Invalid Hold Inventory Value");
621
			return;
622
		}
623
 
624
		try {
625
			if(!defaultInventory.getText().trim().equals("")) {
626
				long default_inventory = Long.parseLong(defaultInventory.getText().trim());
627
				if(default_inventory < 0) {
628
					throw new NumberFormatException("Negative value of Default Inventory");
629
				}
630
				item.setDefaultInventory(default_inventory);
631
			}
632
		} catch(NumberFormatException ex) {
633
			Window.alert("Invalid Default Inventory Value");
634
			return;
635
		}
636
		item.setDefaultForEntity(defaultForEntity.getValue());
637
		item.setRisky(risky.getValue());
638
		item.setHasItemNo(hasItemNo.getValue());
639
		item.setItemType(itemType.getValue());
640
		if(preferredVendor.getSelectedIndex() == 0) {
641
			Window.alert("Invalid Vendor Selected");
642
			return;
643
		}
644
		else {
645
			item.setPreferredVendor(Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex())));
646
		}
647
		item.setPreferredInsurer(Long.parseLong(preferredInsurer.getValue(preferredInsurer.getSelectedIndex())));
648
		/*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 649
          Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 650
		Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
651
		VendorPricings v;
652
		for(int row = 0; row < vendorTable.getRowCount(); row++) {
653
			v = new VendorPricings();
654
			v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
655
			v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
656
			v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
657
			v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
658
			v.setNlc(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_NLC)));
659
			vendorPrices.put(v.getVendorId(), v);
660
		}
661
		item.setVendorPricesMap(vendorPrices);
662
 
663
		/*Create an instance of VendorKeys for each row in vendor pricing table. Set the vendor prices to the instance.
2119 ankur.sing 664
        Add the instance to map and set the map to the item instance created above.*/
8901 vikram.rag 665
		Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
666
		VendorItemMapping vMapping;
667
		for(int row = 0; row < vendorTableM.getRowCount(); row++) {
668
			vMapping = new VendorItemMapping();
669
			vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
670
			vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
671
			vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
672
		}
673
		item.setVendorKeysMap(vendorMappings);
674
		if(!Utils.validateItem(item)) {
675
			return;
676
		}
2427 ankur.sing 677
 
8901 vikram.rag 678
		/*Service method to add item. */
679
		catalogService.addItem(item, new AsyncCallback<Long>() {
680
			@Override
681
			public void onSuccess(Long result) {
682
				if(result == null || result == 0) {
683
					Window.alert("Error while adding item");
684
					return;
685
				}
686
				Window.alert("Item added successfully. Id = " + result);
687
				hide();
688
			}
689
			@Override
690
			public void onFailure(Throwable caught) {
691
				caught.printStackTrace();
692
				Window.alert("Error while adding item");
693
			}
694
		});
695
	}
696
 
697
	@UiHandler("cancelButton")
698
	void closeForm(ClickEvent event) {
699
		this.hide();
700
	}
701
 
702
	/**
703
	 * Checks if vendor details already exists corresponding to the vendor Id parameter. 
704
	 */
705
	private boolean vendorExists(long vendorId) {
706
		long id;
707
		for(int i = 0; i < vendorTable.getRowCount(); i++) {
708
			id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
709
			if(vendorId == id) {
710
				return false;
711
			}
712
		}
713
		return true;
714
	}
715
 
716
	/**
717
	 * validate vendor prices (MOP, DealerPrice, TransferPrice)
718
	 */
719
	@SuppressWarnings("unused")
2566 chandransh 720
	private boolean validateVendorPrices(double mop, double dp, double tp) {
8901 vikram.rag 721
		double mrpValue;
722
		try {
723
			mrpValue = Double.parseDouble(mrp.getText().trim());
724
		} catch (NumberFormatException ex) {
725
			Window.alert("Invalid MRP value.");
726
			return false;
727
		}
728
		if(mrpValue < mop) {
729
			Window.alert("MOP cannot be more than MRP.");
730
			return false;
731
		}
732
		if(tp > mop) {
733
			Window.alert("Transfer Price cannot be more than MOP.");
734
			return false;
735
		}
736
		return true;
737
	}
738
 
5217 amit.gupta 739
	@UiHandler("comingSoonButton")
740
	void onComingSoonButtonClick(ClickEvent event) {
741
		ComingSoonDialog cd = new ComingSoonDialog(this);
742
		cd.show();
743
	}
744
 
745
	public void setComingSoonStartDate(Date date){
746
		this.comingSoonStartDate.setValue(date);
747
	}
748
	public void setBestDealsText(String bestDealsText){
749
		this.bestDealText.setText(bestDealsText);
750
	}
751
 
752
	public void setExpectedArrivalDate(Date date){
753
		this.expectedArrivalDate.setValue(date);
754
	}
755
 
6241 amit.gupta 756
	public void setShowPrice(boolean b){
757
		this.showSellingPrice.setValue(b);
758
	}
759
 
760
	public boolean isShowPrice(){
761
		return this.showSellingPrice.getValue();
762
	}
763
 
5217 amit.gupta 764
	@Override
765
	public String getBestDealsText() {
766
		return this.bestDealText.getValue();
767
	}
768
 
769
	@Override
770
	public Date getComingSoonStartDate() {
771
		return this.comingSoonStartDate.getValue();
772
	}
773
 
774
	@Override
775
	public Date getExpectedArrivalDate() {
6241 amit.gupta 776
		return this.expectedArrivalDate.getValue();
5217 amit.gupta 777
	}
6777 vikram.rag 778
 
779
	public TextBox getBestDealsDetailsText() {
780
		return bestDealsDetailsText;
781
	}
782
 
783
	public void setBestDealsDetailsText(TextBox bestDealsDetailsText) {
784
		this.bestDealsDetailsText = bestDealsDetailsText;
785
	}
786
 
787
	public TextBox getBestDealsDetailsLink() {
788
		return bestDealsDetailsLink;
789
	}
790
 
791
	public void setBestDealsDetailsLink(TextBox bestDealsDetailsLink) {
792
		this.bestDealsDetailsLink = bestDealsDetailsLink;
793
	}
2066 ankur.sing 794
}