Subversion Repositories SmartDukaan

Rev

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