Subversion Repositories SmartDukaan

Rev

Rev 2359 | Rev 2427 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1961 ankur.sing 1
package in.shop2020.catalog.dashboard.client;
2
 
3
import in.shop2020.catalog.dashboard.shared.Item;
2066 ankur.sing 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;
1961 ankur.sing 7
 
8
import java.util.Date;
2066 ankur.sing 9
import java.util.HashMap;
1992 ankur.sing 10
import java.util.Map;
11
import java.util.Map.Entry;
1961 ankur.sing 12
 
13
import com.google.gwt.core.client.GWT;
14
import com.google.gwt.event.dom.client.ClickEvent;
1992 ankur.sing 15
import com.google.gwt.event.dom.client.ClickHandler;
16
import com.google.gwt.resources.client.CssResource;
1961 ankur.sing 17
import com.google.gwt.uibinder.client.UiBinder;
18
import com.google.gwt.uibinder.client.UiField;
19
import com.google.gwt.uibinder.client.UiHandler;
20
import com.google.gwt.user.client.Window;
2126 ankur.sing 21
import com.google.gwt.user.client.rpc.AsyncCallback;
1961 ankur.sing 22
import com.google.gwt.user.client.ui.Button;
2066 ankur.sing 23
import com.google.gwt.user.client.ui.CheckBox;
1992 ankur.sing 24
import com.google.gwt.user.client.ui.FlexTable;
25
import com.google.gwt.user.client.ui.HTMLTable.Cell;
1961 ankur.sing 26
import com.google.gwt.user.client.ui.Label;
27
import com.google.gwt.user.client.ui.ResizeComposite;
28
import com.google.gwt.user.client.ui.TextBox;
29
import com.google.gwt.user.client.ui.Widget;
2068 ankur.sing 30
import com.google.gwt.user.datepicker.client.DateBox;
1961 ankur.sing 31
 
32
public class ItemDetails extends ResizeComposite {
33
 
2359 ankur.sing 34
    private final int TABLE_INDEX_MAPPING_VENDOR_DESC = 0, 
35
                      TABLE_INDEX_MAPPING_ITEM_KEY = 1,
36
                      TABLE_INDEX_MAPPING_BUTTON = 2,
37
                      TABLE_INDEX_MAPPING_VENDORID = 3, 
38
                      TABLE_INDEX_MAPPING_ITEM_KEY_OLD = 4;
39
 
40
    private final int TABLE_INDEX_PRICING_VENDOR_DESC = 0, 
41
                      TABLE_INDEX_PRICING_MOP = 1,
42
                      TABLE_INDEX_PRICING_DP = 2,
43
                      TABLE_INDEX_PRICING_TP = 3,
44
                      TABLE_INDEX_PRICING_BUTTON = 4,
45
                      TABLE_INDEX_PRICING_VENDORID = 5;
46
 
2119 ankur.sing 47
    private final int TABLE_INDEX_WAREHOUSE_ID = 0,
48
                      TABLE_INDEX_WAREHOUSE_DESC = 1,
49
                      TABLE_INDEX_WAREHOUSE_INV = 2;
2359 ankur.sing 50
 
51
    private final String PRICE_WIDTH = "100px", VENDOR_DESC_WIDTH = "130px",
52
                         VENDOR_ID_WIDTH = "100px", ITEM_KEY_WIDTH = "200px",
53
                         BUTTON_WIDTH = "50px";
2066 ankur.sing 54
 
55
    interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
56
    private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);
2126 ankur.sing 57
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2066 ankur.sing 58
 
1961 ankur.sing 59
 
2066 ankur.sing 60
    interface ItemDetailStyle extends CssResource{
61
        String greenLabel();
62
        String fieldChanged();
63
    }
1961 ankur.sing 64
 
2105 ankur.sing 65
    private Item item, newItem;
2066 ankur.sing 66
 
67
    @UiField ItemDetailStyle style;
68
    @UiField Label itemId;
69
    @UiField TextBox productGroup, brand, modelNumber, modelName, color;
2105 ankur.sing 70
    @UiField Label contentCategory, catalogItemId;
2359 ankur.sing 71
    @UiField TextBox statusDesc, comments;
2105 ankur.sing 72
    @UiField TextBox sellingPrice, mrp, weight;
73
    @UiField Label addedOn, retireDate, updatedOn;
2066 ankur.sing 74
    @UiField Label itemStatus;
75
    @UiField TextBox bestDealsText, bestDealsValue; 
76
    @UiField FlexTable headerAvailability, availabilityTable;
2105 ankur.sing 77
    @UiField FlexTable headerVendorM, vendorTableM;
78
    @UiField FlexTable headerVendor, vendorTable;
2066 ankur.sing 79
    @UiField TextBox bestSellingRank;
2252 ankur.sing 80
    @UiField CheckBox defaultForEntity, risky;
2126 ankur.sing 81
    //@UiField Button submit;
2068 ankur.sing 82
    @UiField DateBox startDate;
2066 ankur.sing 83
 
84
    public ItemDetails(Item item){
2105 ankur.sing 85
        this();
2066 ankur.sing 86
        setItemDetails(item);
87
    }
88
 
89
    public ItemDetails() {
90
        initWidget(uiBinder.createAndBindUi(this));
91
        initAvailabilityHeader();
2119 ankur.sing 92
        initMappingHeader();
2066 ankur.sing 93
        initPricingHeader();
2119 ankur.sing 94
 
2105 ankur.sing 95
        headerAvailability.setVisible(false);
96
        availabilityTable.setVisible(false);
2066 ankur.sing 97
    }
98
 
99
    public void setItemDetails(Item item){
100
        this.item = item;
101
        itemId.setText(item.getId()+"");
102
        productGroup.setText(item.getProductGroup());
103
        brand.setText(item.getBrand());
104
        modelNumber.setText(item.getModelNumber());
105
        modelName.setText(item.getModelName());
106
        color.setText(item.getColor());
107
 
2359 ankur.sing 108
        statusDesc.setText(item.getItemStatusDesc());
2105 ankur.sing 109
        contentCategory.setText(item.getContentCategory()+"");
2066 ankur.sing 110
        comments.setText(item.getComments());
111
        catalogItemId.setText(item.getCatalogItemId() + "");
112
 
2126 ankur.sing 113
        mrp.setText(item.getMrp() != -1 ? item.getMrp()+"" : "");
114
        sellingPrice.setText(item.getSellingPrice() != -1 ? item.getSellingPrice()+"" : "");
2138 ankur.sing 115
        weight.setText(item.getWeight() != -1 ? item.getWeight()+"" : "");
2068 ankur.sing 116
 
117
        startDate.setValue(new Date(item.getStartDate()));
118
        addedOn.setText(Utils.getDisplayableDate(item.getAddedOn()));
119
        retireDate.setText(Utils.getDisplayableDate(item.getRetireDate()));
120
        updatedOn.setText(Utils.getDisplayableDate(item.getUpdatedOn()));
1992 ankur.sing 121
 
2066 ankur.sing 122
        bestDealsText.setText(item.getBestDealsText());
2126 ankur.sing 123
        bestDealsValue.setText(item.getBestDealsValue() != -1 ? item.getBestDealsValue()+"" : "");
124
        bestSellingRank.setText(item.getBestSellingRank() != -1 ? item.getBestSellingRank()+"" : "");
2066 ankur.sing 125
        defaultForEntity.setValue(item.isDefaultForEntity());
2252 ankur.sing 126
        risky.setValue(item.isRisky());
1992 ankur.sing 127
 
2066 ankur.sing 128
        itemStatus.setText(item.getItemStatus());
129
 
130
        updateAvailabilityTable(item.getAvailability());
2359 ankur.sing 131
        updateMappingTable(item.getVendorKeysMap());
2119 ankur.sing 132
        updatePricingTable(item.getVendorPricesMap());
1992 ankur.sing 133
    }
2066 ankur.sing 134
 
135
    private void initAvailabilityHeader(){
1992 ankur.sing 136
        // Initialize the header.
2066 ankur.sing 137
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
2105 ankur.sing 138
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
2066 ankur.sing 139
        headerAvailability.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
140
 
141
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_ID, "Warehouse Id");
142
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_DESC, "Warehouse Desc");
143
        headerAvailability.setText(0, TABLE_INDEX_WAREHOUSE_INV, "Availability");
1992 ankur.sing 144
 
145
    }
2066 ankur.sing 146
 
2119 ankur.sing 147
    private void initMappingHeader(){
148
        // Initialize the header.
2359 ankur.sing 149
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
150
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
151
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
152
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
153
        headerVendorM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_BUTTON, BUTTON_WIDTH);
2119 ankur.sing 154
 
2359 ankur.sing 155
        headerVendorM.setText(0, TABLE_INDEX_MAPPING_VENDORID, "Vendor Id");
156
        headerVendorM.setText(0, TABLE_INDEX_MAPPING_VENDOR_DESC, "Vendor");
157
        headerVendorM.setText(0, TABLE_INDEX_MAPPING_ITEM_KEY, "Item Key");
158
        headerVendorM.setText(0, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, "Prev Item Key");
2119 ankur.sing 159
 
2359 ankur.sing 160
        headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_MAPPING_VENDORID, false);
161
        headerVendorM.getCellFormatter().setVisible(0, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
162
 
2119 ankur.sing 163
        Button addButton = new Button("Add");
2359 ankur.sing 164
        headerVendorM.setWidget(0, TABLE_INDEX_MAPPING_BUTTON, addButton);
2119 ankur.sing 165
        addButton.addClickHandler(new ClickHandler() {
166
            @Override
167
            public void onClick(ClickEvent event) {
2126 ankur.sing 168
                VendorMappingDialog vendorMappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
169
                        modelNumber.getText().trim(), color.getText().trim());
2119 ankur.sing 170
                vendorMappingDialog.updateButton.setText("Add");
171
                vendorMappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
172
                    @Override
173
                    public boolean onUpdate(String key, long vendorId) {
174
                        int row = vendorTableM.getRowCount();
2359 ankur.sing 175
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
176
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
177
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
178
                        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
2119 ankur.sing 179
 
2359 ankur.sing 180
                        vendorTableM.setText(row, TABLE_INDEX_MAPPING_VENDORID, vendorId + "");
181
                        vendorTableM.setText(row, TABLE_INDEX_MAPPING_VENDOR_DESC, Utils.getVendorDesc(vendorId));
182
                        vendorTableM.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY, key);
183
                        vendorTableM.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, key);
184
 
185
                        vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_MAPPING_VENDORID, false);
186
                        vendorTableM.getCellFormatter().setVisible(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
2119 ankur.sing 187
                        return true;
188
                    }
189
                });
190
                vendorMappingDialog.show();
191
            }
192
        });
193
    }
194
 
195
 
2066 ankur.sing 196
    private void initPricingHeader(){
197
        // Initialize the header.
2359 ankur.sing 198
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
199
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
200
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
201
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
202
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
203
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 204
 
2359 ankur.sing 205
        headerVendor.setText(0, TABLE_INDEX_PRICING_VENDORID, "Vendor Id");
206
        headerVendor.setText(0, TABLE_INDEX_PRICING_VENDOR_DESC, "Vendor");
207
        headerVendor.setText(0, TABLE_INDEX_PRICING_MOP, "MOP");
208
        headerVendor.setText(0, TABLE_INDEX_PRICING_DP, "Dealer Price");
209
        headerVendor.setText(0, TABLE_INDEX_PRICING_TP, "Transfer Price");
2105 ankur.sing 210
 
2359 ankur.sing 211
        headerVendor.getCellFormatter().setVisible(0, TABLE_INDEX_PRICING_VENDORID, false);
212
 
2105 ankur.sing 213
        Button addButton = new Button("Add");
2359 ankur.sing 214
        headerVendor.setWidget(0, TABLE_INDEX_PRICING_BUTTON, addButton);
2105 ankur.sing 215
        addButton.addClickHandler(new ClickHandler() {
216
            @Override
217
            public void onClick(ClickEvent event) {
2119 ankur.sing 218
                VendorPricesDialog vendorPricesDialog = new VendorPricesDialog();
219
                vendorPricesDialog.updateButton.setText("Add");
220
                vendorPricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2105 ankur.sing 221
                    @Override
2119 ankur.sing 222
                    public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
2105 ankur.sing 223
                        if(!vendorExists(vendorId)) {
224
                            Window.alert("Vendor already exists");
225
                            return false;
226
                        }
2119 ankur.sing 227
                        /*if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 228
                            return false;
2119 ankur.sing 229
                        }*/
2105 ankur.sing 230
                        int row = vendorTable.getRowCount();
2359 ankur.sing 231
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
232
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
233
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
234
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
235
                        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
2105 ankur.sing 236
 
2359 ankur.sing 237
                        vendorTable.setText(row, TABLE_INDEX_PRICING_VENDORID, vendorId + "");
238
                        vendorTable.setText(row, TABLE_INDEX_PRICING_VENDOR_DESC, Utils.getVendorDesc(vendorId));
239
                        vendorTable.setText(row, TABLE_INDEX_PRICING_MOP, mop + "");
240
                        vendorTable.setText(row, TABLE_INDEX_PRICING_DP, dp + "");
241
                        vendorTable.setText(row, TABLE_INDEX_PRICING_TP, tp + "");
242
 
243
                        vendorTable.getCellFormatter().setVisible(row, TABLE_INDEX_PRICING_VENDORID, false);
2105 ankur.sing 244
                        return true;
245
                    }
246
                });
2119 ankur.sing 247
                vendorPricesDialog.show();
2105 ankur.sing 248
            }
249
        });
2066 ankur.sing 250
    }
2105 ankur.sing 251
 
1992 ankur.sing 252
    private void updateAvailabilityTable(Map<Long, Long> availabilityMap){
253
        availabilityTable.removeAllRows();
254
        if(availabilityMap == null) {
255
            return;
256
        }
2066 ankur.sing 257
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_ID, "128px");
2105 ankur.sing 258
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_DESC, "300px");
2066 ankur.sing 259
        availabilityTable.getColumnFormatter().setWidth(TABLE_INDEX_WAREHOUSE_INV, "128px");
260
 
1992 ankur.sing 261
        int i=0;
262
        for(Entry<Long, Long> availability : availabilityMap.entrySet()){
2066 ankur.sing 263
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_ID, availability.getKey() + "");
264
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_DESC, Utils.getWarehouseDesc(availability.getKey()));
265
            availabilityTable.setText(i, TABLE_INDEX_WAREHOUSE_INV, availability.getValue() + "");
1992 ankur.sing 266
            i++;
267
        }
268
    }
2119 ankur.sing 269
 
2359 ankur.sing 270
    private void updateMappingTable(Map<String, VendorItemMapping> vendorMappingsMap){
2119 ankur.sing 271
        vendorTableM.removeAllRows();
272
 
273
        if(vendorMappingsMap == null || vendorMappingsMap.isEmpty()) {
274
            return;
275
        }
2359 ankur.sing 276
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDORID, VENDOR_ID_WIDTH);
277
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_VENDOR_DESC, VENDOR_DESC_WIDTH);
278
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY, ITEM_KEY_WIDTH);
279
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_ITEM_KEY_OLD, ITEM_KEY_WIDTH);
280
        vendorTableM.getColumnFormatter().setWidth(TABLE_INDEX_MAPPING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 281
 
2119 ankur.sing 282
        int i=0;
2359 ankur.sing 283
        for(Entry<String, VendorItemMapping> e : vendorMappingsMap.entrySet()){
284
            VendorItemMapping vendorMapping = e.getValue();
285
            vendorTableM.setText(i, TABLE_INDEX_MAPPING_VENDORID, vendorMapping.getVendorId() + "");
286
            vendorTableM.setText(i, TABLE_INDEX_MAPPING_VENDOR_DESC, Utils.getVendorDesc(vendorMapping.getVendorId()));
287
            vendorTableM.setText(i, TABLE_INDEX_MAPPING_ITEM_KEY, vendorMapping.getItemKey());
288
            vendorTableM.setText(i, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, e.getKey().substring(e.getKey().indexOf(Item.KEY_SEPARATOR)+1));
2119 ankur.sing 289
            Button editButton = new Button("Edit");
2359 ankur.sing 290
            vendorTableM.setWidget(i, TABLE_INDEX_MAPPING_BUTTON, editButton);
2119 ankur.sing 291
            editButton.addClickHandler(new ClickHandler() {
292
                @Override
293
                public void onClick(ClickEvent event) {
294
                    Cell cell = vendorTableM.getCellForEvent(event);
295
                    int row = cell.getRowIndex();
2359 ankur.sing 296
                    long vendorId = Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_MAPPING_VENDORID));
2119 ankur.sing 297
                    updateVendorMapping(vendorId, row);
298
                }
299
            });
2359 ankur.sing 300
            vendorTableM.getCellFormatter().setVisible(i, TABLE_INDEX_MAPPING_VENDORID, false);
301
            vendorTableM.getCellFormatter().setVisible(i, TABLE_INDEX_MAPPING_ITEM_KEY_OLD, false);
302
            i++;
2119 ankur.sing 303
        }
304
    }
305
 
306
    private void updatePricingTable(Map<Long, VendorPricings> vendorPricingMap){
2105 ankur.sing 307
        vendorTable.removeAllRows();
308
 
2119 ankur.sing 309
        if(vendorPricingMap == null || vendorPricingMap.isEmpty()) {
1992 ankur.sing 310
            return;
311
        }
2359 ankur.sing 312
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDORID, VENDOR_ID_WIDTH);
313
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_VENDOR_DESC, VENDOR_DESC_WIDTH);
314
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_MOP, PRICE_WIDTH);
315
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_DP, PRICE_WIDTH);
316
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_TP, PRICE_WIDTH);
317
        vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_PRICING_BUTTON, BUTTON_WIDTH);
2066 ankur.sing 318
 
319
 
1992 ankur.sing 320
        int i=0;
2119 ankur.sing 321
        for(VendorPricings vendorDetail : vendorPricingMap.values()){
2359 ankur.sing 322
            vendorTable.setText(i, TABLE_INDEX_PRICING_VENDORID, vendorDetail.getVendorId() + "");
323
            vendorTable.setText(i, TABLE_INDEX_PRICING_VENDOR_DESC, Utils.getVendorDesc(vendorDetail.getVendorId()));
324
            vendorTable.setText(i, TABLE_INDEX_PRICING_MOP, vendorDetail.getMop() + "");
325
            vendorTable.setText(i, TABLE_INDEX_PRICING_DP, vendorDetail.getDealerPrice() + "");
326
            vendorTable.setText(i, TABLE_INDEX_PRICING_TP, vendorDetail.getTransferPrice() + "");
1992 ankur.sing 327
            Button editButton = new Button("Edit");
2359 ankur.sing 328
            vendorTable.setWidget(i, TABLE_INDEX_PRICING_BUTTON, editButton);
1992 ankur.sing 329
            editButton.addClickHandler(new ClickHandler() {
330
                @Override
331
                public void onClick(ClickEvent event) {
2105 ankur.sing 332
                    Cell cell = vendorTable.getCellForEvent(event);
1992 ankur.sing 333
                    int row = cell.getRowIndex();
2359 ankur.sing 334
                    long vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_PRICING_VENDORID));
1992 ankur.sing 335
                    updateVendorPrices(vendorId, row);
336
                }
337
            });
2359 ankur.sing 338
            vendorTable.getCellFormatter().setVisible(i, TABLE_INDEX_PRICING_VENDORID, false);
339
            i++;
1992 ankur.sing 340
        }
341
    }
2066 ankur.sing 342
 
2126 ankur.sing 343
 
344
    void updateItem() {
345
        if(item == null) {
346
            Window.alert("Please select an item to update.");
347
            return;
348
        }
2066 ankur.sing 349
        try {
2126 ankur.sing 350
            if(!createNewItem()) {
351
                return;
352
            }
2252 ankur.sing 353
            String paramsChanged = isItemChanged();
354
            if(paramsChanged.equals("")) {
2066 ankur.sing 355
                Window.alert("Nothing to update. Please change intended item parameters and try again.");
356
                return;
2252 ankur.sing 357
            } else {
358
                paramsChanged = "You have changed following items.\n" + paramsChanged;
359
                Window.alert(paramsChanged);
2066 ankur.sing 360
            }
361
        } catch(NumberFormatException ex) {
2126 ankur.sing 362
            ex.printStackTrace();
363
            GWT.log("Number format exception");
364
        }
365
        if(!Utils.validateItem(newItem)) {
2066 ankur.sing 366
            return;
367
        }
2126 ankur.sing 368
 
369
        /*if(!validatePrices()) {
370
            return;
371
        }*/
372
        //updateItem();  -- Calling this method above. A new item is created with updated data.
373
        catalogService.updateItem(newItem, new AsyncCallback<Boolean>() {
374
            @Override
375
            public void onSuccess(Boolean result) {
376
                if(result) {
377
                    item = newItem;
378
                    GWT.log("Item updated. Id = " + item.getId());
379
                    Window.alert("Item updated successfully.");
380
                }
381
                else {
382
                    GWT.log("Error updating item");
383
                    Window.alert("Error updating item");
384
                }
385
            }
386
            @Override
387
            public void onFailure(Throwable caught) {
388
                caught.printStackTrace();
389
                Window.alert("Error while updating item");
390
            }
391
        });
2066 ankur.sing 392
    }
2126 ankur.sing 393
 
394
    private boolean createNewItem() {
2066 ankur.sing 395
        newItem = new Item();
396
        newItem.setId(Long.parseLong(itemId.getText()));
397
        newItem.setProductGroup(productGroup.getText().trim());
398
        newItem.setBrand(brand.getText().trim());
399
        newItem.setModelNumber(modelNumber.getText().trim());
400
        newItem.setModelName(modelName.getText().trim());
401
        newItem.setColor(color.getText().trim());
2119 ankur.sing 402
        newItem.setContentCategory(contentCategory.getText());
2066 ankur.sing 403
        newItem.setComments(comments.getText().trim());
404
        newItem.setCatalogItemId(Long.parseLong(catalogItemId.getText()));
2126 ankur.sing 405
 
2068 ankur.sing 406
        try {
2126 ankur.sing 407
            if(!mrp.getText().trim().isEmpty()) {
408
                double mrpValue = Double.parseDouble(mrp.getText().trim());
409
                if(mrpValue <= 0) {
410
                    throw new NumberFormatException("Negative value of MRP");
411
                }
412
                newItem.setMrp(mrpValue);
413
            } else {
414
                newItem.setMrp(-1);
415
            }
2068 ankur.sing 416
        } catch(NumberFormatException ex) {
2126 ankur.sing 417
            Window.alert("Invalid MRP format/value. Value shoule be greater than zero");
418
            return false;
2068 ankur.sing 419
        }
420
        try {
2126 ankur.sing 421
            if(!sellingPrice.getText().trim().isEmpty()) {
422
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
423
            if(spValue <= 0) {
424
                throw new NumberFormatException("Negative value of Selling price");
425
            }
426
            newItem.setSellingPrice(spValue);
427
            } else {
428
                newItem.setSellingPrice(-1);
429
            }
430
        } catch(NumberFormatException ex) {
431
            Window.alert("Invalid Selling Price format/value. Value shoule be greater than zero");
432
            return false;
2068 ankur.sing 433
        }
434
        try {
2126 ankur.sing 435
            if(!weight.getText().trim().isEmpty()) {
436
                double wtValue = Double.parseDouble(weight.getText().trim());
437
                if(wtValue <= 0) {
438
                    throw new NumberFormatException("Negative value of Weight");
439
                }
440
                newItem.setWeight(wtValue);
441
            } else {
442
                newItem.setWeight(-1);
443
            }
2068 ankur.sing 444
        } catch(NumberFormatException ex) {
2126 ankur.sing 445
            Window.alert("Invalid weight format/value. Value shoule be greater than zero");
446
            return false;
2068 ankur.sing 447
        }
2126 ankur.sing 448
        try {
449
            if(!startDate.getTextBox().getText().trim().equals("")) {
450
                newItem.setStartDate(startDate.getValue().getTime());
451
            }
452
        } catch(Exception ex) {
453
            Window.alert("Invalid start date format");
454
            return false;
455
        }
2066 ankur.sing 456
        newItem.setBestDealsText(bestDealsText.getText().trim());
2068 ankur.sing 457
        try {
2126 ankur.sing 458
            if(!bestDealsValue.getText().trim().equals("")) {
459
                double bdValue = Double.parseDouble(bestDealsValue.getText().trim());
460
                if(bdValue < 0) {
461
                    throw new NumberFormatException("Negative value of BestDealValue");
462
                }
463
                newItem.setBestDealsValue(bdValue);
464
            } else {
465
                newItem.setBestDealsValue(-1);
466
            }
2068 ankur.sing 467
        } catch(NumberFormatException ex) {
2126 ankur.sing 468
            Window.alert("Invalid best deal value format");
469
            return false;
2068 ankur.sing 470
        }
471
        try {
2126 ankur.sing 472
            if(!bestSellingRank.getText().trim().equals("")) {
473
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
474
                if(bsrValue < 0) {
475
                    throw new NumberFormatException("Negative value of Best Selling Rank");
476
                }
477
                newItem.setBestSellingRank(bsrValue);
478
            } else {
479
                newItem.setBestSellingRank(-1);
480
            }
481
 
482
        } catch(NumberFormatException ex) {
483
            Window.alert("Invalid best selling rank format");
484
            return false;
2068 ankur.sing 485
        }
2066 ankur.sing 486
        newItem.setDefaultForEntity(defaultForEntity.getValue());
2252 ankur.sing 487
        newItem.setRisky(risky.getValue());
2119 ankur.sing 488
 
2126 ankur.sing 489
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
490
          Add the instance to map and set the map to the item instance created above.*/
491
        Map<Long, VendorPricings> vendorPrices = new HashMap<Long, VendorPricings>();
2119 ankur.sing 492
        VendorPricings v;
2105 ankur.sing 493
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2119 ankur.sing 494
            v = new VendorPricings();
2359 ankur.sing 495
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_MOP)));
496
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_DP)));
497
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_TP)));
498
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_PRICING_VENDORID)));
2126 ankur.sing 499
            vendorPrices.put(v.getVendorId(), v);
2105 ankur.sing 500
            newItem.setMop(v.getMop());
501
            newItem.setDealerPrice(v.getDealerPrice());
502
            newItem.setTransferPrice(v.getTransferPrice());
2066 ankur.sing 503
        }
2126 ankur.sing 504
        newItem.setVendorPricesMap(vendorPrices);
2359 ankur.sing 505
        newItem.setItemStatusDesc(statusDesc.getText().trim());
2126 ankur.sing 506
        /*Create an instance of VendorPricings for each row in vendor pricing table. Set the vendor prices to the instance.
507
        Add the instance to map and set the map to the item instance created above.*/
2359 ankur.sing 508
        Map<String, VendorItemMapping> vendorMappings = new HashMap<String, VendorItemMapping>();
2126 ankur.sing 509
        VendorItemMapping vMapping;
2119 ankur.sing 510
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
2126 ankur.sing 511
            vMapping = new VendorItemMapping();
2359 ankur.sing 512
            vMapping.setItemKey(vendorTableM.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY));
513
            vMapping.setVendorId(Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_MAPPING_VENDORID)));
514
            vendorMappings.put(vMapping.getVendorId() + Item.KEY_SEPARATOR + vendorTableM.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD), vMapping);
2119 ankur.sing 515
        }
2359 ankur.sing 516
        newItem.setVendorKeysMap(vendorMappings);
2119 ankur.sing 517
 
2126 ankur.sing 518
        newItem.setContentCategoryId(item.getContentCategoryId());
519
        newItem.setFeatureId(item.getFeatureId());
520
        newItem.setFeatureDescription(item.getFeatureDescription());
521
        newItem.setAddedOn(item.getAddedOn());
522
        newItem.setRetireDate(item.getRetireDate());
523
        newItem.setUpdatedOn(item.getUpdatedOn());
524
        newItem.setItemStatus(item.getItemStatus());
525
        newItem.setOtherInfo(item.getOtherInfo());
526
        newItem.setAvailability(item.getAvailability());
2119 ankur.sing 527
 
2126 ankur.sing 528
        return true;
2066 ankur.sing 529
    }
530
 
531
    private void updateVendorPrices(final long vendorId, final int row) {
2359 ankur.sing 532
        String mop = vendorTable.getText(row, TABLE_INDEX_PRICING_MOP);
533
        String dp = vendorTable.getText(row, TABLE_INDEX_PRICING_DP);
534
        String tp = vendorTable.getText(row, TABLE_INDEX_PRICING_TP);
2119 ankur.sing 535
        VendorPricesDialog pricesDialog = new VendorPricesDialog(mop, dp, tp);
2105 ankur.sing 536
        pricesDialog.updateButton.setText("Update");
2119 ankur.sing 537
        pricesDialog.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
2066 ankur.sing 538
            @Override
2119 ankur.sing 539
            public boolean onUpdate(double mop, double dp, double tp, long vendorId) {
540
                if(!validateVendorPrices(mop, dp, tp)) {
2105 ankur.sing 541
                    return false;
542
                }
2359 ankur.sing 543
                vendorTable.setText(row, TABLE_INDEX_PRICING_MOP, mop + "");
544
                vendorTable.setText(row, TABLE_INDEX_PRICING_DP, dp + "");
545
                vendorTable.setText(row, TABLE_INDEX_PRICING_TP, tp + "");
2105 ankur.sing 546
                return true;
2066 ankur.sing 547
            }
548
        });
549
        pricesDialog.show();
550
    }
2119 ankur.sing 551
 
552
    private void updateVendorMapping(final long vendorId, final int row) {
2359 ankur.sing 553
        String key = vendorTableM.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
2126 ankur.sing 554
        VendorMappingDialog mappingDialog = new VendorMappingDialog(productGroup.getText().trim(), brand.getText().trim(), 
555
                modelNumber.getText().trim(), color.getText().trim(), key);
2119 ankur.sing 556
        mappingDialog.updateButton.setText("Update");
557
        mappingDialog.setVendorMappingUpdateListener(new VendorMappingDialog.VendorMappingUpdateListener() {
558
            @Override
559
            public boolean onUpdate(String itemKey, long vendorId) {
560
                if(itemKey == null || itemKey.equals("")) {
561
                    Window.alert("Item key cannot be empty.");
562
                    return false;
563
                }
2359 ankur.sing 564
                vendorTableM.setText(row, TABLE_INDEX_MAPPING_ITEM_KEY, itemKey);
2119 ankur.sing 565
                return true;
566
            }
567
        });
568
        mappingDialog.show();
569
    }
2066 ankur.sing 570
 
2252 ankur.sing 571
    private String isItemChanged() {
572
        StringBuilder sb = new StringBuilder("");
2387 ankur.sing 573
        if(!checkStringsIfEqual(productGroup.getText().trim(), item.getProductGroup())) {
574
            sb.append("\n-Product Group");
2066 ankur.sing 575
        }
2387 ankur.sing 576
        if(!checkStringsIfEqual(brand.getText().trim(), item.getBrand())) {
577
            sb.append("\n-Brand");
2066 ankur.sing 578
        }
2387 ankur.sing 579
        if(!checkStringsIfEqual(modelNumber.getText().trim(), item.getModelNumber())) {
580
            sb.append("\n-Model Number");
2066 ankur.sing 581
        }
2387 ankur.sing 582
        if(!checkStringsIfEqual(modelName.getText().trim(), item.getModelName())) {
583
            sb.append("\n-Model Name");
2066 ankur.sing 584
        }
2387 ankur.sing 585
        if(!checkStringsIfEqual(color.getText().trim(), item.getColor())) {
586
            sb.append("\n-Color");
2066 ankur.sing 587
        }
2387 ankur.sing 588
        if(!checkStringsIfEqual(statusDesc.getText().trim(), item.getItemStatusDesc())) {
589
            sb.append("\n-Status Description");
2359 ankur.sing 590
        }
2387 ankur.sing 591
        if(!checkStringsIfEqual(comments.getText().trim(), item.getComments())) {
592
            sb.append("\n-Comments");
2066 ankur.sing 593
        }
2126 ankur.sing 594
        if(newItem.getMrp() != item.getMrp()) {
2387 ankur.sing 595
            sb.append("\n-MRP");
2066 ankur.sing 596
        }
2126 ankur.sing 597
        if(newItem.getSellingPrice() != item.getSellingPrice()) {
2387 ankur.sing 598
            sb.append("\n-Selling Price");
2027 ankur.sing 599
        }
2126 ankur.sing 600
        if(newItem.getWeight() != item.getWeight()) {
2387 ankur.sing 601
            sb.append("\n-Weight");
2027 ankur.sing 602
        }
2387 ankur.sing 603
        if(!checkStringsIfEqual(bestDealsText.getText().trim(), item.getBestDealsText())) {
604
            sb.append("\n-Best Deal Text");
2066 ankur.sing 605
        }
2126 ankur.sing 606
        if(newItem.getBestDealsValue() != item.getBestDealsValue()) {
2387 ankur.sing 607
            sb.append("\n-Best Deal Value");
2027 ankur.sing 608
        }
2126 ankur.sing 609
        if(newItem.getBestSellingRank() != item.getBestSellingRank()) {
2387 ankur.sing 610
            sb.append("\n-Best Selling Rank");
2066 ankur.sing 611
        }
612
        if(item.isDefaultForEntity() != defaultForEntity.getValue()) {
2387 ankur.sing 613
            sb.append("\n-Default For Entity Flag");
2066 ankur.sing 614
        }
2252 ankur.sing 615
        if(item.isRisky() != risky.getValue()) {
2387 ankur.sing 616
            sb.append("\n-Risky Flag");
2252 ankur.sing 617
        }
2068 ankur.sing 618
        if(newItem.getStartDate() != item.getStartDate()) {
2387 ankur.sing 619
            sb.append("\n-Start Date");
2068 ankur.sing 620
        }
2126 ankur.sing 621
        VendorPricings vendorPricings;
2066 ankur.sing 622
        long vendorId;
2105 ankur.sing 623
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
2359 ankur.sing 624
            vendorId = Long.parseLong(vendorTable.getText(row, TABLE_INDEX_PRICING_VENDORID));
2126 ankur.sing 625
            vendorPricings = item.getVendorPricesMap().get(vendorId);
2359 ankur.sing 626
            if(vendorPricings == null) {
2387 ankur.sing 627
                sb.append("\n-Vendor Prices (Vendor:" + vendorId + ")");
2359 ankur.sing 628
                continue;
629
            }
630
            if(vendorPricings.getMop() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_MOP))) {
2387 ankur.sing 631
                sb.append("\n-MOP (Vendor:" + vendorId + ")");
2066 ankur.sing 632
            }
2359 ankur.sing 633
            if(vendorPricings.getDealerPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_DP))) {
2387 ankur.sing 634
                sb.append("\n-Dealer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 635
            }
2359 ankur.sing 636
            if(vendorPricings.getTransferPrice() != Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_PRICING_TP))) {
2387 ankur.sing 637
                sb.append("\n-Transfer Price (Vendor:" + vendorId + ")");
2066 ankur.sing 638
            }
639
        }
2387 ankur.sing 640
 
641
        VendorItemMapping mapping;
642
        String old_key, new_key;
643
        for(int row = 0; row < vendorTableM.getRowCount(); row++) {
644
            vendorId = Long.parseLong(vendorTableM.getText(row, TABLE_INDEX_MAPPING_VENDORID));
645
            old_key = vendorTableM.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY_OLD);
646
            new_key = vendorTableM.getText(row, TABLE_INDEX_MAPPING_ITEM_KEY);
647
            mapping = item.getVendorKeysMap().get(vendorId + Item.KEY_SEPARATOR + old_key);
648
            if(mapping == null || !old_key.equals(new_key)) {
649
                sb.append("\n-Vendor Key (Vendor:" + vendorId + ",Key: = " + old_key + ")");
650
                continue;
651
            }
652
        }
2252 ankur.sing 653
        return sb.toString();
2066 ankur.sing 654
    }
655
 
656
    private boolean validatePrices() {
657
        if(newItem.getSellingPrice() > newItem.getMrp()) {
658
            Window.alert("Selling price cannot be more than MRP");
659
            return false;
660
        }
2119 ankur.sing 661
        for(VendorPricings v : newItem.getVendorPricesMap().values()) {
2105 ankur.sing 662
            if(newItem.getMrp() < v.getMop()) {
663
                Window.alert("MRP cannot be less than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 664
                return false;
665
            }
2105 ankur.sing 666
            if(v.getTransferPrice() > v.getMop()) {
667
                Window.alert("Transfer Price cannot be more than MOP. Vendor: " + v.getVendorId());
2066 ankur.sing 668
                return false;
669
            }
670
        }
671
        return true;
672
    }
2105 ankur.sing 673
 
2119 ankur.sing 674
    private boolean validateVendorPrices(double mop, double dp, double tp) {
2126 ankur.sing 675
        if(item.getMrp() != -1 && item.getMrp() < mop) {
2105 ankur.sing 676
            Window.alert("MOP cannot be more than MRP.");
677
            return false;
1992 ankur.sing 678
        }
2105 ankur.sing 679
        if(tp > mop) {
680
            Window.alert("Transfer Price cannot be more than MOP.");
681
            return false;
1992 ankur.sing 682
        }
2105 ankur.sing 683
        return true;
1992 ankur.sing 684
    }
2105 ankur.sing 685
 
686
    public long getItemId() {
687
        return item == null ? 0 : item.getId();
688
    }
689
 
690
    public Item getItem() {
691
        return item;
692
    }
693
 
694
    private boolean vendorExists(long vendorId) {
695
        long id;
696
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
2359 ankur.sing 697
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_PRICING_VENDORID));
2105 ankur.sing 698
            if(vendorId == id) {
699
                return false;
700
            }
1992 ankur.sing 701
        }
2105 ankur.sing 702
        return true;
1992 ankur.sing 703
    }
2387 ankur.sing 704
 
705
    private boolean checkStringsIfEqual(String s1, String s2) {
706
        if(s1 == s2) {
707
            return true;
708
        }
709
        if(s1 != null && s2 != null && s1.equals(s2)) {
710
            return true;
711
        }
712
        if((s1 == null && s2.equals("")) || (s2 == null && s1.equals(""))) {
713
            return true;
714
        }
715
        return false;
716
    }
1961 ankur.sing 717
}