Subversion Repositories SmartDukaan

Rev

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