Subversion Repositories SmartDukaan

Rev

Rev 5217 | Rev 5460 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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