Subversion Repositories SmartDukaan

Rev

Rev 1992 | Rev 2066 | 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;
4
 
5
import java.util.Date;
1992 ankur.sing 6
import java.util.Map;
7
import java.util.Map.Entry;
1961 ankur.sing 8
 
9
import com.google.gwt.core.client.GWT;
10
import com.google.gwt.event.dom.client.ClickEvent;
1992 ankur.sing 11
import com.google.gwt.event.dom.client.ClickHandler;
12
import com.google.gwt.resources.client.CssResource;
1961 ankur.sing 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.ui.Button;
1992 ankur.sing 18
import com.google.gwt.user.client.ui.FlexTable;
19
import com.google.gwt.user.client.ui.HTMLTable.Cell;
1961 ankur.sing 20
import com.google.gwt.user.client.ui.Label;
21
import com.google.gwt.user.client.ui.ResizeComposite;
22
import com.google.gwt.user.client.ui.TextBox;
23
import com.google.gwt.user.client.ui.Widget;
24
 
25
public class ItemDetails extends ResizeComposite {
2027 ankur.sing 26
 
1961 ankur.sing 27
 
28
	interface ItemDetailsUiBinder extends UiBinder<Widget, ItemDetails> {}
29
	private static ItemDetailsUiBinder uiBinder = GWT.create(ItemDetailsUiBinder.class);
30
 
1992 ankur.sing 31
	public interface ItemUpdateListener {
32
        void onItemUpdate(Item item);
1961 ankur.sing 33
      }
34
 
1992 ankur.sing 35
	interface ItemDetailStyle extends CssResource{
36
	    String greenLabel();
37
	    String fieldChanged();
38
	}
39
 
40
	private ItemUpdateListener itemUpdateListener;
41
 
42
	private Item item;
1961 ankur.sing 43
 
1992 ankur.sing 44
	@UiField ItemDetailStyle style;
2027 ankur.sing 45
	@UiField Label itemId;
46
	@UiField TextBox productGroup, brand, modelNumber, modelName, color;
47
	@UiField Label category, catalogItemId;
48
	@UiField TextBox comments;
1992 ankur.sing 49
	@UiField TextBox sellingPrice, mrp, /*mop, dealerPrice, transferPrice,*/ weight;
1961 ankur.sing 50
	@UiField Label addedOn, startDate, retireDate, updatedOn;
2027 ankur.sing 51
	@UiField Label itemStatus;
52
	@UiField TextBox bestDealsText, bestDealsValue; 
1992 ankur.sing 53
	@UiField FlexTable headerAvailability, availabilityTable;
54
	@UiField FlexTable headerPricing, pricingTable;
1961 ankur.sing 55
	@UiField Button submit;
56
 
57
	public ItemDetails(Item item){
58
		initWidget(uiBinder.createAndBindUi(this));
2027 ankur.sing 59
		adjustUIFields();
1992 ankur.sing 60
		initAvailabilityHeader();
61
		initPricingHeader();
62
		setItemDetails(item);
1961 ankur.sing 63
	}
64
 
65
	public ItemDetails() {
66
	    this.item = null;
67
	    initWidget(uiBinder.createAndBindUi(this));
2027 ankur.sing 68
	    adjustUIFields();
1992 ankur.sing 69
	    initAvailabilityHeader();
70
	    initPricingHeader();
1961 ankur.sing 71
	}
72
 
2027 ankur.sing 73
	private void adjustUIFields() {
74
	    sellingPrice.setSize("8", "4");
75
	}
76
 
1961 ankur.sing 77
	public void setItemDetails(Item item){
1992 ankur.sing 78
	    this.item = item;
1961 ankur.sing 79
		itemId.setText(item.getId()+"");
80
		productGroup.setText(item.getProductGroup());
81
		brand.setText(item.getBrand());
82
		modelNumber.setText(item.getModelNumber());
83
		modelName.setText(item.getModelName());
84
		color.setText(item.getColor());
85
 
86
		category.setText(item.getCategory()+"");
87
		comments.setText(item.getComments());
88
		catalogItemId.setText(item.getCatalogItemId() + "");
89
 
90
		mrp.setText(item.getMrp()+"");
91
		sellingPrice.setText(item.getSellingPrice()+"");
1992 ankur.sing 92
		/*mop.setText(item.getMop()+"");
1961 ankur.sing 93
		dealerPrice.setText(item.getDealerPrice()+"");
1992 ankur.sing 94
		transferPrice.setText(item.getTransferPrice()+"");*/
1961 ankur.sing 95
		weight.setText(item.getWeight()+"");
96
 
97
		addedOn.setText(getDisplayableDate(item.getAddedOn()));
98
		startDate.setText(getDisplayableDate(item.getStartDate()));
99
		retireDate.setText(getDisplayableDate(item.getRetireDate()));
100
		updatedOn.setText(getDisplayableDate(item.getUpdatedOn()));
101
 
102
		bestDealsText.setText(item.getBestDealsText());
103
		bestDealsValue.setText(item.getBestDealsValue()+"");
1992 ankur.sing 104
 
105
		itemStatus.setText(item.getItemStatus());
106
 
107
		updateAvailabilityTable(item.getAvailability());
108
 
109
		updatePricingTable(item.getVendorPricings());
1961 ankur.sing 110
	}
111
 
1992 ankur.sing 112
	private void initAvailabilityHeader(){
113
        // Initialize the header.
114
        headerAvailability.getColumnFormatter().setWidth(0, "128px");
115
        headerAvailability.getColumnFormatter().setWidth(1, "128px");
116
 
117
        headerAvailability.setText(0, 0, "Warehouse Id");
118
        headerAvailability.setText(0, 1, "Availability");
119
 
120
    }
121
 
122
   private void initPricingHeader(){
123
        // Initialize the header.
124
        headerPricing.getColumnFormatter().setWidth(0, "128px");
125
        headerPricing.getColumnFormatter().setWidth(1, "128px");
126
        headerPricing.getColumnFormatter().setWidth(2, "128px");
127
        headerPricing.getColumnFormatter().setWidth(3, "128px");
128
 
129
        headerPricing.setText(0, 0, "Vendor Id");
130
        headerPricing.setText(0, 1, "MOP");
131
        headerPricing.setText(0, 2, "Dealer Price");
132
        headerPricing.setText(0, 3, "Transfer Price");
133
    }
134
 
135
    private void updateAvailabilityTable(Map<Long, Long> availabilityMap){
136
        availabilityTable.removeAllRows();
137
        if(availabilityMap == null) {
138
            return;
139
        }
140
        availabilityTable.getColumnFormatter().setWidth(0, "128px");
141
        availabilityTable.getColumnFormatter().setWidth(1, "128px");
142
 
143
 
144
        int i=0;
145
        for(Entry<Long, Long> availability : availabilityMap.entrySet()){
146
            int col = 0;
147
            availabilityTable.setText(i, col++, availability.getKey() + "");
148
            availabilityTable.setText(i, col++, availability.getValue() + "");
149
            i++;
150
        }
151
    }
152
 
153
    private void updatePricingTable(Map<Long, double[]> pricings){
154
        pricingTable.removeAllRows();
155
        if(pricings == null || pricings.isEmpty()) {
156
            return;
157
        }
158
        pricingTable.getColumnFormatter().setWidth(0, "128px");
159
        pricingTable.getColumnFormatter().setWidth(1, "128px");
160
        pricingTable.getColumnFormatter().setWidth(2, "128px");
161
        pricingTable.getColumnFormatter().setWidth(3, "128px");
162
 
163
 
164
        int i=0;
165
        for(Entry<Long, double[]> pricing : pricings.entrySet()){
166
            int col = 0;
167
            pricingTable.setText(i, col++, pricing.getKey() + "");
168
            pricingTable.setText(i, col++, pricing.getValue()[Item.INDEX_MOP] + "");
169
            pricingTable.setText(i, col++, pricing.getValue()[Item.INDEX_DP] + "");
170
            pricingTable.setText(i, col++, pricing.getValue()[Item.INDEX_TP] + "");
171
            Button editButton = new Button("Edit");
172
            pricingTable.setWidget(i, col++, editButton);
173
            i++;
174
            editButton.addClickHandler(new ClickHandler() {
175
                @Override
176
                public void onClick(ClickEvent event) {
177
                    Cell cell = pricingTable.getCellForEvent(event);
178
                    int row = cell.getRowIndex();
179
                    long vendorId = Long.parseLong(pricingTable.getText(row, 0));
180
                    updateVendorPrices(vendorId, row);
181
                }
182
            });
183
        }
184
    }
185
 
186
 
1961 ankur.sing 187
	//TODO: This code is duplicated between here and OrderList. Move it to a Utility class.
188
	private String getDisplayableDate(Long millis){
189
		Date date = new Date();
190
		date.setTime(millis);
191
		String dateString = date.toString();
192
		dateString = dateString.substring(0, dateString.lastIndexOf(" "));
193
		dateString = dateString.substring(0, dateString.lastIndexOf(" "));
194
		return dateString;		
195
	}
196
 
1992 ankur.sing 197
	public void setItemUpdateListener(ItemUpdateListener itemUpdatelistener) {
198
	    this.itemUpdateListener = itemUpdatelistener;
1961 ankur.sing 199
	}
200
 
201
	@UiHandler("submit")
202
	void onSubmitPressed(ClickEvent clickEvent) {
1992 ankur.sing 203
	    //long id = Long.parseLong(this.itemId.getText());
1961 ankur.sing 204
	    try {
1992 ankur.sing 205
	        updateItem();
1961 ankur.sing 206
	    } catch(NumberFormatException ex) {
1992 ankur.sing 207
	        return;
1961 ankur.sing 208
	    }
1992 ankur.sing 209
        itemUpdateListener.onItemUpdate(item);
2027 ankur.sing 210
        GWT.log("Item updated. Id = " + item.getId());
1961 ankur.sing 211
	}
212
 
1992 ankur.sing 213
	void updateItem() throws NumberFormatException {
2027 ankur.sing 214
	    item.setProductGroup(productGroup.getText().trim());
215
	    item.setBrand(brand.getText().trim());
216
	    item.setModelName(modelName.getText().trim());
217
	    item.setModelNumber(modelNumber.getText().trim());
218
	    item.setColor(color.getText().trim());
219
 
220
	    item.setComments(comments.getText().trim());
221
 
222
	    try {
223
	        item.setMrp(Double.parseDouble(this.mrp.getText()));
224
	    } catch(NumberFormatException ex) {
225
	        Window.alert("Invalid MRP format");
226
	        throw new NumberFormatException();
227
	    }
228
	    try { 
229
	        item.setSellingPrice(Double.parseDouble(this.sellingPrice.getText()));
230
	    } catch(NumberFormatException ex) {
231
            Window.alert("Invalid Selling Price format");
232
            throw new NumberFormatException();
233
        }
234
	    try {
235
	        item.setWeight(Double.parseDouble(this.weight.getText()));
236
	    } catch(NumberFormatException ex) {
237
            Window.alert("Invalid Weight format");
238
            throw new NumberFormatException();
239
        }
240
 
241
	    item.setBestDealsText(bestDealsText.getText().trim());
242
	    try {
243
	        item.setBestDealsValue(Double.parseDouble(bestDealsValue.getText().trim()));
244
	    } catch(NumberFormatException ex) {
245
            Window.alert("Invalid Best Deals Value format");
246
            throw new NumberFormatException();
247
        }
1992 ankur.sing 248
	}
249
 
250
	private void updateVendorPrices(final long vendorId, final int row) {
251
	    String mop = pricingTable.getText(row, 1);
252
	    String dp = pricingTable.getText(row, 2);
253
	    String tp = pricingTable.getText(row, 3);
254
	    VendorPricesDialog vpd = new VendorPricesDialog(mop, dp, tp);
255
	    vpd.setVendorPriceUpdateListener(new VendorPricesDialog.VendorPriceUpdateListener() {
256
            @Override
257
            public void onPriceUpdate(double mop, double dp, double tp) {
258
                pricingTable.setText(row, 1, mop + "");
259
                pricingTable.setText(row, 2, dp + "");
260
                pricingTable.setText(row, 3, tp + "");
261
                item.getVendorPricings().get(vendorId)[Item.INDEX_MOP] = mop;
262
                item.getVendorPricings().get(vendorId)[Item.INDEX_DP] = dp;
263
                item.getVendorPricings().get(vendorId)[Item.INDEX_TP] = tp;
264
                item.setMop(mop);
265
                item.setDealerPrice(dp);
266
                item.setTransferPrice(tp);
267
            }
268
        });
269
	    vpd.show();
270
	}
271
 
272
	/*
273
	@UiHandler("sellingPrice")
274
	void onSPUpdate(KeyPressEvent keyPressEvent) {
275
	    if(Double.parseDouble(sellingPrice.getText()) != item.getSellingPrice()) {
276
	        sellingPrice.getElement().addClassName(style.fieldChanged());
277
	    } else {
278
	        sellingPrice.getElement().addClassName(style.greenLabel());
279
	    }
280
	}
281
 
282
	@UiHandler("mop")
283
    void onMOPUpdate(KeyPressEvent keyPressEvent) {
284
        if(Double.parseDouble(mop.getText()) != item.getMop()) {
285
            mop.getElement().addClassName(style.fieldChanged());
286
        } else {
287
            mop.getElement().addClassName(style.greenLabel());
288
        }
289
    }
290
 
291
	@UiHandler("dealerPrice")
292
    void onDPUpdate(KeyPressEvent keyPressEvent) {
293
        if(Double.parseDouble(dealerPrice.getText()) != item.getDealerPrice()) {
294
            dealerPrice.getElement().addClassName(style.fieldChanged());
295
        } else {
296
            dealerPrice.getElement().addClassName(style.greenLabel());
297
        }
298
    }
299
 
300
	@UiHandler("transferPrice")
301
    void onTPUpdate(KeyPressEvent keyPressEvent) {
302
        if(Double.parseDouble(transferPrice.getText()) != item.getTransferPrice()) {
303
            transferPrice.getElement().addClassName(style.fieldChanged());
304
        } else {
305
            transferPrice.getElement().addClassName(style.greenLabel());
306
        }
307
    }
308
 
309
	@UiHandler("weight")
310
    void onWeightUpdate(KeyPressEvent keyPressEvent) {
311
        if(Double.parseDouble(weight.getText()) != item.getWeight()) {
312
            weight.getElement().addClassName(style.fieldChanged());
313
        } else {
314
            weight.getElement().addClassName(style.greenLabel());
315
        }
316
    }
317
 
318
	void applyDefaultStyles() {
319
	    sellingPrice.getElement().addClassName(style.greenLabel());
320
	    mop.getElement().addClassName(style.greenLabel());
321
	    dealerPrice.getElement().addClassName(style.greenLabel());
322
	    transferPrice.getElement().addClassName(style.greenLabel());
323
	    weight.getElement().addClassName(style.greenLabel());
324
	}*/
325
 
1961 ankur.sing 326
}