Subversion Repositories SmartDukaan

Rev

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