Subversion Repositories SmartDukaan

Rev

Rev 2066 | Rev 2119 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2066 Rev 2105
Line 1... Line 1...
1
package in.shop2020.catalog.dashboard.client;
1
package in.shop2020.catalog.dashboard.client;
2
 
2
 
-
 
3
import in.shop2020.catalog.dashboard.shared.Item;
3
import com.google.gwt.user.client.ui.ResizeComposite;
4
import in.shop2020.catalog.dashboard.shared.Utils;
-
 
5
import in.shop2020.catalog.dashboard.shared.VendorDetails;
4
 
6
 
5
public class ItemForm extends ResizeComposite {
7
import java.util.HashMap;
-
 
8
import java.util.Map;
6
 
9
 
-
 
10
import com.google.gwt.core.client.GWT;
-
 
11
import com.google.gwt.event.dom.client.ClickEvent;
-
 
12
import com.google.gwt.event.dom.client.ClickHandler;
-
 
13
import com.google.gwt.uibinder.client.UiBinder;
-
 
14
import com.google.gwt.uibinder.client.UiField;
-
 
15
import com.google.gwt.uibinder.client.UiHandler;
-
 
16
import com.google.gwt.user.client.Window;
-
 
17
import com.google.gwt.user.client.rpc.AsyncCallback;
-
 
18
import com.google.gwt.user.client.ui.Button;
-
 
19
import com.google.gwt.user.client.ui.CheckBox;
-
 
20
import com.google.gwt.user.client.ui.DialogBox;
-
 
21
import com.google.gwt.user.client.ui.FlexTable;
-
 
22
import com.google.gwt.user.client.ui.HTMLTable.Cell;
-
 
23
import com.google.gwt.user.client.ui.ListBox;
-
 
24
import com.google.gwt.user.client.ui.TextBox;
-
 
25
import com.google.gwt.user.client.ui.Widget;
-
 
26
import com.google.gwt.user.datepicker.client.DateBox;
-
 
27
 
-
 
28
public class ItemForm extends DialogBox {
-
 
29
 
-
 
30
    interface ItemFormUiBinder extends UiBinder<Widget, ItemForm> {}
-
 
31
    private static ItemFormUiBinder uiBinder = GWT.create(ItemFormUiBinder.class);
-
 
32
    
-
 
33
    private final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
-
 
34
    private final int TABLE_INDEX_VENDORID = 0, TABLE_INDEX_VENDOR_DESC = 1, 
-
 
35
    TABLE_INDEX_ITEM_KEY = 2, TABLE_INDEX_MOP = 3, TABLE_INDEX_DP = 4, TABLE_INDEX_TP = 5;
-
 
36
 
-
 
37
    @UiField ListBox vendorCategory;
-
 
38
    @UiField TextBox productGroup;
-
 
39
    @UiField TextBox brand, modelNumber, modelName, color, comments;
-
 
40
    @UiField TextBox mrp, sellingPrice, weight, bestDealText, bestDealValue, bestSellingRank;
-
 
41
    @UiField DateBox startDate, retireDate;
-
 
42
    @UiField Button addButton, cancelButton;
-
 
43
    @UiField CheckBox defaultForEntity;
-
 
44
    @UiField FlexTable headerVendor, vendorTable;
-
 
45
    @UiField Button addVendorPrices;
-
 
46
    
-
 
47
    public ItemForm(){
-
 
48
        setText("Add New Item");
-
 
49
        setWidget(uiBinder.createAndBindUi(this));
-
 
50
        initPricingHeader();
-
 
51
        vendorCategory.addItem("Handsets");
-
 
52
        vendorCategory.addItem("Accessories");
-
 
53
    }
-
 
54
    
-
 
55
    
-
 
56
    private void initPricingHeader(){
-
 
57
        // Initialize the header.
-
 
58
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
-
 
59
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
-
 
60
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
-
 
61
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
-
 
62
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
-
 
63
        headerVendor.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
-
 
64
 
-
 
65
        headerVendor.setText(0, TABLE_INDEX_VENDORID, "Vendor Id");
-
 
66
        headerVendor.setText(0, TABLE_INDEX_VENDOR_DESC, "Vendor Desc");
-
 
67
        headerVendor.setText(0, TABLE_INDEX_ITEM_KEY, "Item Key");
-
 
68
        headerVendor.setText(0, TABLE_INDEX_MOP, "MOP");
-
 
69
        headerVendor.setText(0, TABLE_INDEX_DP, "Dealer Price");
-
 
70
        headerVendor.setText(0, TABLE_INDEX_TP, "Transfer Price");
-
 
71
    }
-
 
72
 
-
 
73
    @UiHandler("addButton")
-
 
74
    /**
-
 
75
     * On add button click a new item instance is created and all the information from UI fields is dumped in this item instance.
-
 
76
     * This item instance is then passed to service implementation.
-
 
77
     */
-
 
78
    void addItem(ClickEvent event) {
-
 
79
        //TODO: validate UI fields
-
 
80
        
-
 
81
        Item item = new Item();
-
 
82
        
-
 
83
        item.setProductGroup(productGroup.getText().trim());
-
 
84
        item.setVendorCategory(vendorCategory.getItemText(vendorCategory.getSelectedIndex()));
-
 
85
        
-
 
86
        item.setBrand(brand.getText().trim());
-
 
87
        item.setModelNumber(modelNumber.getText().trim());
-
 
88
        item.setModelName(modelName.getText().trim());
-
 
89
        item.setColor(color.getText().trim());
-
 
90
        item.setComments(comments.getText().trim());
-
 
91
        try {
-
 
92
            double mrpValue = Double.parseDouble(mrp.getText().trim());
-
 
93
            if(mrpValue < 0) {
-
 
94
                throw new NumberFormatException("Negative value of MRP");
-
 
95
            }
-
 
96
            item.setMrp(mrpValue);
-
 
97
        } catch(NumberFormatException ex) {
-
 
98
            Window.alert("Invalid MRP format");
-
 
99
            return;
-
 
100
        }
-
 
101
        try {
-
 
102
            double spValue = Double.parseDouble(sellingPrice.getText().trim());
-
 
103
            if(spValue < 0) {
-
 
104
                throw new NumberFormatException("Negative value of Selling price");
-
 
105
            }
-
 
106
            item.setSellingPrice(spValue);
-
 
107
        } catch(NumberFormatException ex) {
-
 
108
            Window.alert("Invalid Selling Price format");
-
 
109
            return;
-
 
110
        }
-
 
111
        try {
-
 
112
            if(!weight.getText().trim().equals("")) {
-
 
113
                double wtValue = Double.parseDouble(weight.getText().trim());
-
 
114
                if(wtValue < 0) {
-
 
115
                    throw new NumberFormatException("Negative value of Weight");
-
 
116
                }
-
 
117
                item.setWeight(wtValue);
-
 
118
            }
-
 
119
        } catch(NumberFormatException ex) {
-
 
120
            Window.alert("Invalid weight format");
-
 
121
            return;
-
 
122
        }
-
 
123
        try {
-
 
124
            if(!startDate.getTextBox().getText().trim().equals("")) {
-
 
125
                item.setStartDate(startDate.getValue().getTime());
-
 
126
            }
-
 
127
        } catch(Exception ex) {
-
 
128
            Window.alert("Invalid start date format");
-
 
129
            return;
-
 
130
        }
-
 
131
        try {
-
 
132
            if(!retireDate.getTextBox().getText().trim().equals("")) {
-
 
133
                item.setRetireDate(retireDate.getValue().getTime());
-
 
134
            }
-
 
135
        } catch(Exception ex) {
-
 
136
            Window.alert("Invalid retire date format");
-
 
137
            return;
-
 
138
        }
-
 
139
        item.setBestDealsText(bestDealText.getText().trim());
-
 
140
        try {
-
 
141
            if(!bestDealValue.getText().trim().equals("")) {
-
 
142
                double bdValue = Double.parseDouble(bestDealValue.getText().trim());
-
 
143
                if(bdValue < 0) {
-
 
144
                    throw new NumberFormatException("Negative value of BestDealValue");
-
 
145
                }
-
 
146
                item.setBestDealsValue(bdValue);
-
 
147
            }
-
 
148
        } catch(NumberFormatException ex) {
-
 
149
            Window.alert("Invalid best deal value format");
-
 
150
            return;
-
 
151
        }
-
 
152
        try {
-
 
153
            if(!bestSellingRank.getText().trim().equals("")) {
-
 
154
                long bsrValue = Long.parseLong(bestSellingRank.getText().trim());
-
 
155
                if(bsrValue < 0) {
-
 
156
                    throw new NumberFormatException("Negative value of Best Selling Rank");
-
 
157
                }
-
 
158
                item.setBestSellingRank(bsrValue);
-
 
159
            }
-
 
160
            
-
 
161
        } catch(NumberFormatException ex) {
-
 
162
            Window.alert("Invalid best selling rank format");
-
 
163
            return;
-
 
164
        }
-
 
165
        item.setDefaultForEntity(defaultForEntity.getValue());
-
 
166
        
-
 
167
        /*Create an instance of VendorDetails for each row in vendor table. Set the vendor details to the instance.
-
 
168
          Add the instance to map and set the map to the item instance created above.*/
-
 
169
        Map<Long, VendorDetails> vendorPrices = new HashMap<Long, VendorDetails>();
-
 
170
        VendorDetails v;
-
 
171
        for(int row = 0; row < vendorTable.getRowCount(); row++) {
-
 
172
            v = new VendorDetails();
-
 
173
            v.setMop(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_MOP)));
-
 
174
            v.setDealerPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_DP)));
-
 
175
            v.setTransferPrice(Double.parseDouble(vendorTable.getText(row, TABLE_INDEX_TP)));
-
 
176
            v.setVendorId(Long.parseLong(vendorTable.getText(row, TABLE_INDEX_VENDORID)));
-
 
177
            vendorPrices.put(v.getVendorId(), v);
-
 
178
            item.setMop(v.getMop());
-
 
179
            item.setDealerPrice(v.getDealerPrice());
-
 
180
            item.setTransferPrice(v.getTransferPrice());
-
 
181
        }
-
 
182
        item.setVendorDetails(vendorPrices);
-
 
183
        
-
 
184
        /*Service method to add item to DB. */
-
 
185
        catalogService.addItem(item, new AsyncCallback<Long>() {
-
 
186
            @Override
-
 
187
            public void onSuccess(Long result) {
-
 
188
                if(result == null || result == 0) {
-
 
189
                    Window.alert("Error while adding item");
-
 
190
                    return;
-
 
191
                }
-
 
192
                Window.alert("Item added successfully. Id = " + result);
-
 
193
                hide();
-
 
194
            }
-
 
195
            @Override
-
 
196
            public void onFailure(Throwable caught) {
-
 
197
                Window.alert("Error while adding item");
-
 
198
            }
-
 
199
        });
-
 
200
        
-
 
201
    }
-
 
202
    
-
 
203
    @UiHandler("cancelButton")
-
 
204
    void closeForm(ClickEvent event) {
-
 
205
        this.hide();
-
 
206
    }
-
 
207
    
-
 
208
    /**
-
 
209
     * This will pop up a dialog box for adding vendor details. 
-
 
210
     * @param event
-
 
211
     */
-
 
212
    @UiHandler("addVendorPrices")
-
 
213
    void addVendorPrices(ClickEvent event) {
-
 
214
        VendorPricesDialog pricesDialog = new VendorPricesDialog();
-
 
215
        pricesDialog.updateButton.setText("Add");
-
 
216
        pricesDialog.setVendorDetailsUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
-
 
217
            @Override
-
 
218
            public boolean onUpdate(String itemKey, double mop, double dp, double tp, long vendorId) {
-
 
219
                if(!vendorExists(vendorId)) {
-
 
220
                    Window.alert("Vendor already exists");
-
 
221
                    return false;
-
 
222
                }   
-
 
223
                if(!validateVendorDetails(itemKey, mop, dp, tp)) {
-
 
224
                    return false;
-
 
225
                }
-
 
226
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDORID, "128px");
-
 
227
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_VENDOR_DESC, "128px");
-
 
228
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_ITEM_KEY, "250px");
-
 
229
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_MOP, "128px");
-
 
230
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_DP, "128px");
-
 
231
                vendorTable.getColumnFormatter().setWidth(TABLE_INDEX_TP, "128px");
-
 
232
                
-
 
233
                int row = vendorTable.getRowCount();
-
 
234
                vendorTable.setText(row, TABLE_INDEX_VENDORID, vendorId+"");
-
 
235
                vendorTable.setText(row, TABLE_INDEX_VENDOR_DESC, Utils.getVendorDesc(vendorId));
-
 
236
                vendorTable.setText(row, TABLE_INDEX_ITEM_KEY, itemKey);
-
 
237
                vendorTable.setText(row, TABLE_INDEX_MOP, mop + "");
-
 
238
                vendorTable.setText(row, TABLE_INDEX_DP, dp + "");
-
 
239
                vendorTable.setText(row, TABLE_INDEX_TP, tp + "");
-
 
240
                Button removeButton = new Button("Remove");
-
 
241
                vendorTable.setWidget(row, TABLE_INDEX_TP + 1, removeButton);
-
 
242
                removeButton.addClickHandler(new ClickHandler() {
-
 
243
                    @Override
-
 
244
                    public void onClick(ClickEvent event) {
-
 
245
                        Cell cell = vendorTable.getCellForEvent(event);
-
 
246
                        int row = cell.getRowIndex();
-
 
247
                        vendorTable.removeRow(row);
-
 
248
                    }
-
 
249
                });
-
 
250
                return true;
-
 
251
            }
-
 
252
        });
-
 
253
        pricesDialog.center();
-
 
254
        pricesDialog.show();
-
 
255
    }
-
 
256
    
-
 
257
    /**
-
 
258
     * Checks if vendor details already exists corresponding to the vendor Id parameter. 
-
 
259
     */
-
 
260
    private boolean vendorExists(long vendorId) {
-
 
261
        long id;
-
 
262
        for(int i = 0; i < vendorTable.getRowCount(); i++) {
-
 
263
            id = Long.parseLong(vendorTable.getText(i, TABLE_INDEX_VENDORID));
-
 
264
            if(vendorId == id) {
-
 
265
                return false;
-
 
266
            }
-
 
267
        }
-
 
268
        return true;
-
 
269
    }
-
 
270
    
-
 
271
    /**
-
 
272
     * validate vendor details (Item key, MOP, DealerPrice, TransferPrice)
-
 
273
     */
-
 
274
    private boolean validateVendorDetails(String key, double mop, double dp, double tp) {
-
 
275
        double mrpValue;
-
 
276
        if(key == null || key.isEmpty()) {
-
 
277
            Window.alert("Item key cannot be empty");
-
 
278
            return false;
-
 
279
        }
-
 
280
        try {
-
 
281
            mrpValue = Double.parseDouble(mrp.getText().trim());
-
 
282
        } catch (NumberFormatException ex) {
-
 
283
            Window.alert("Invalid MRP value.");
-
 
284
            return false;
-
 
285
        }
-
 
286
        if(mrpValue < mop) {
-
 
287
            Window.alert("MOP cannot be more than MRP.");
-
 
288
            return false;
-
 
289
        }
-
 
290
        if(tp > mop) {
-
 
291
            Window.alert("Transfer Price cannot be more than MOP.");
-
 
292
            return false;
-
 
293
        }
-
 
294
        return true;
-
 
295
    }
7
}
296
}