Subversion Repositories SmartDukaan

Rev

Rev 2566 | Rev 4725 | 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
        }
2119 ankur.sing 317
        item.setVendorPricesMap(vendorPrices);
2105 ankur.sing 318
 
2119 ankur.sing 319
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
320
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 321
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2119 ankur.sing 322
        VendorItemMapping vMapping;
323
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
324
            vMapping = new VendorItemMapping();
325
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_ITEM_KEY));
326
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_VENDORID)));
2359 ankur.sing 327
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vMapping.getItemKey(), vMapping);
2119 ankur.sing 328
        }
2359 ankur.sing 329
        item.setVendorKeysMap(vendorMappings);
2119 ankur.sing 330
 
331
        if(!Utils.validateItem(item)) {
332
            return;
333
        }
334
 
2427 ankur.sing 335
        /*Service method to add item. */
2105 ankur.sing 336
        catalogService.addItem(item, new AsyncCallback<Long>() {
337
            @Override
338
            public void onSuccess(Long result) {
339
                if(result == null || result == 0) {
340
                    Window.alert("Error while adding item");
341
                    return;
342
                }
343
                Window.alert("Item added successfully. Id = " + result);
344
                hide();
345
            }
346
            @Override
347
            public void onFailure(Throwable caught) {
2427 ankur.sing 348
                caught.printStackTrace();
2105 ankur.sing 349
                Window.alert("Error while adding item");
350
            }
351
        });
352
    }
353
 
2138 ankur.sing 354
    @UiHandler("vendorCategory")
355
    void setProductGroup(ChangeEvent event) {
356
        if(HANDSETS.equals(vendorCategory.getItemText(vendorCategory.getSelectedIndex()))) {
357
            productGroup.setText(HANDSETS);
358
        } else {
359
            productGroup.setText("");
360
        }
361
    }
362
 
2105 ankur.sing 363
    @UiHandler("cancelButton")
364
    void closeForm(ClickEvent event) {
365
        this.hide();
366
    }
2427 ankur.sing 367
 
2105 ankur.sing 368
    /**
369
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
370
     */
371
    private boolean vendorExists(long vendorId) {
372
        long id;
373
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
374
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
375
            if(vendorId == id) {
376
                return false;
377
            }
378
        }
379
        return true;
380
    }
381
 
382
    /**
2119 ankur.sing 383
     * validate vendor prices (MOP, DealerPrice, TransferPrice)
2105 ankur.sing 384
     */
2566 chandransh 385
    @SuppressWarnings("unused")
386
	private boolean validateVendorPrices(double mop, double dp, double tp) {
2105 ankur.sing 387
        double mrpValue;
388
        try {
389
            mrpValue = Double.parseDouble(mrp.getText().trim());
390
        } catch (NumberFormatException ex) {
391
            Window.alert("Invalid MRP value.");
392
            return false;
393
        }
394
        if(mrpValue < mop) {
395
            Window.alert("MOP cannot be more than MRP.");
396
            return false;
397
        }
398
        if(tp > mop) {
399
            Window.alert("Transfer Price cannot be more than MOP.");
400
            return false;
401
        }
402
        return true;
403
    }
2066 ankur.sing 404
}