Subversion Repositories SmartDukaan

Rev

Rev 5384 | Rev 5586 | 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;
5460 phani.kuma 57
    @UiField CheckBox defaultForEntity, risky, itemType, hasItemNo, clearance;
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());
5460 phani.kuma 322
        item.setClearance(clearance.getValue());
4762 phani.kuma 323
        if(preferredVendor.getSelectedIndex() == 0) {
324
        	Window.alert("Invalid Vendor Selected");
325
            return;
326
        }
327
    	else {
328
    		item.setPreferredVendor(Utils.getVendorId(preferredVendor.getItemText(preferredVendor.getSelectedIndex())));
329
    	}
2105 ankur.sing 330
 
2119 ankur.sing 331
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
2105 ankur.sing 332
          Add the instance to map and set the map to the item instance created above.*/
2119 ankur.sing 333
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
334
        VendorPricings v;
2105 ankur.sing 335
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 336
            v = new VendorPricings();
2105 ankur.sing 337
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
338
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
339
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
340
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
341
            vendorPrices.put(v.getVendorId(), v);
342
        }
2119 ankur.sing 343
        item.setVendorPricesMap(vendorPrices);
2105 ankur.sing 344
 
2119 ankur.sing 345
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
346
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 347
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2119 ankur.sing 348
        VendorItemMapping vMapping;
349
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
350
            vMapping = new VendorItemMapping();
351
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
352
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
2359 ankur.sing 353
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
2119 ankur.sing 354
        }
2359 ankur.sing 355
        item.setVendorKeysMap(vendorMappings);
2119 ankur.sing 356
 
357
        if(!Utils.validateItem(item)) {
358
            return;
359
        }
360
 
2427 ankur.sing 361
        /*Service method to add item. */
2105 ankur.sing 362
        catalogService.addItem(item, new AsyncCallback<Long>() {
363
            @Override
364
            public void onSuccess(Long result) {
365
                if(result == null || result == 0) {
366
                    Window.alert("Error while adding item");
367
                    return;
368
                }
369
                Window.alert("Item added successfully. Id = " + result);
370
                hide();
371
            }
372
            @Override
373
            public void onFailure(Throwable caught) {
2427 ankur.sing 374
                caught.printStackTrace();
2105 ankur.sing 375
                Window.alert("Error while adding item");
376
            }
377
        });
378
    }
379
 
380
    @UiHandler("cancelButton")
381
    void closeForm(ClickEvent event) {
382
        this.hide();
383
    }
2427 ankur.sing 384
 
2105 ankur.sing 385
    /**
386
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
387
     */
388
    private boolean vendorExists(long vendorId) {
389
        long id;
390
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
391
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
392
            if(vendorId == id) {
393
                return false;
394
            }
395
        }
396
        return true;
397
    }
398
 
399
    /**
2119 ankur.sing 400
     * validate vendor prices (MOP, DealerPrice, TransferPrice)
2105 ankur.sing 401
     */
2566 chandransh 402
    @SuppressWarnings("unused")
403
	private boolean validateVendorPrices(double mop, double dp, double tp) {
2105 ankur.sing 404
        double mrpValue;
405
        try {
406
            mrpValue = Double.parseDouble(mrp.getText().trim());
407
        } catch (NumberFormatException ex) {
408
            Window.alert("Invalid MRP value.");
409
            return false;
410
        }
411
        if(mrpValue < mop) {
412
            Window.alert("MOP cannot be more than MRP.");
413
            return false;
414
        }
415
        if(tp > mop) {
416
            Window.alert("Transfer Price cannot be more than MOP.");
417
            return false;
418
        }
419
        return true;
420
    }
5217 amit.gupta 421
 
422
	@UiHandler("comingSoonButton")
423
	void onComingSoonButtonClick(ClickEvent event) {
424
		ComingSoonDialog cd = new ComingSoonDialog(this);
425
		cd.show();
426
	}
427
 
428
	public void setComingSoonStartDate(Date date){
429
		this.comingSoonStartDate.setValue(date);
430
	}
431
	public void setBestDealsText(String bestDealsText){
432
		this.bestDealText.setText(bestDealsText);
433
	}
434
 
435
	public void setExpectedArrivalDate(Date date){
436
		this.expectedArrivalDate.setValue(date);
437
	}
438
 
439
	@Override
440
	public String getBestDealsText() {
441
		return this.bestDealText.getValue();
442
	}
443
 
444
	@Override
445
	public Date getComingSoonStartDate() {
446
		return this.comingSoonStartDate.getValue();
447
	}
448
 
449
	@Override
450
	public Date getExpectedArrivalDate() {
451
		return this.comingSoonStartDate.getValue();
452
	}
2066 ankur.sing 453
}