Subversion Repositories SmartDukaan

Rev

Rev 6788 | Rev 12523 | 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.shared;
2
 
3
import in.shop2020.catalog.dashboard.client.CatalogService;
4
import in.shop2020.catalog.dashboard.client.CatalogServiceAsync;
5
 
2068 ankur.sing 6
import java.util.Date;
4957 phani.kuma 7
import java.util.List;
2066 ankur.sing 8
import java.util.Map;
2105 ankur.sing 9
import java.util.Map.Entry;
2066 ankur.sing 10
 
11
import com.google.gwt.core.client.GWT;
2119 ankur.sing 12
import com.google.gwt.user.client.Window;
2066 ankur.sing 13
import com.google.gwt.user.client.rpc.AsyncCallback;
14
 
2427 ankur.sing 15
/**
16
 * Utility methods goes here
17
 *
18
 */
2066 ankur.sing 19
public class Utils {
2427 ankur.sing 20
    // item status values. These should be analogous to status enum in status.java
21
    // These values are used in the client side to check if transition from one status to another is 
22
    // valid. Also see validateStatusChange method
23
    // FIXME: These should be read from the ThriftConfig file and not hard-coded here.
2126 ankur.sing 24
    public static final int PHASED_OUT = 0,
25
                            DELETED = 1,
26
                            PAUSED = 2,
27
                            ACTIVE = 3,
28
                            IN_PROCESS = 4,
29
                            CONTENT_COMPLETE = 5;
2427 ankur.sing 30
 
31
    // role column in helper.catalogdashboarduser table. role specifies 
32
    // the username-pwd pair for staging or for production.
33
    // production password is needed while updating item on production server.
6788 rajveer 34
    public static final int ROLE_STAGING = 0, ROLE_READONLY = 1, ROLE_PRODUCTION = 2;
5586 phani.kuma 35
    private static boolean entityIdMandatory = false;
2427 ankur.sing 36
 
5516 phani.kuma 37
    private static Map<Long, String> vendors, warehouses, sources, voucherType;
4957 phani.kuma 38
    private static List<String> categories, brands;
2427 ankur.sing 39
 
2066 ankur.sing 40
    private static final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2126 ankur.sing 41
 
6530 vikram.rag 42
    private static List<ItemWarehouse> ignoredInventoryUpdateItemsIdsWarehouseIds;
6838 vikram.rag 43
    private static Map<Long,String> allInsurers;
6530 vikram.rag 44
 
2066 ankur.sing 45
    static {
6838 vikram.rag 46
    	catalogService.getAllInsurers(new AsyncCallback<Map<Long,String>>() {
47
            @Override
48
            public void onSuccess(Map<Long, String> result) {
49
            	allInsurers = result;
50
            }
51
            @Override
52
            public void onFailure(Throwable caught) {
53
                caught.printStackTrace();
54
            }
55
        });
5586 phani.kuma 56
    	catalogService.getConfigforentityIdMandatory(new AsyncCallback<Boolean>() {
57
	        @Override
58
	        public void onSuccess(Boolean result) {
59
	        	setEntityIdMandatory(result);
60
	        }
61
	        @Override
62
	        public void onFailure(Throwable caught) {
63
	            caught.printStackTrace();
64
	        }
65
	    });
6530 vikram.rag 66
    	catalogService.getignoredInventoryUpdateItemsIdsWarehouseIds(new AsyncCallback<List<ItemWarehouse>>() {
67
	        @Override
68
	        public void onSuccess(List<ItemWarehouse> result) {
69
	        	ignoredInventoryUpdateItemsIdsWarehouseIds=result;
70
	        }
71
	        @Override
72
	        public void onFailure(Throwable caught) {
73
	            caught.printStackTrace();
74
	        }
75
	    });
76
 
5586 phani.kuma 77
 
2066 ankur.sing 78
        catalogService.getAllVendors(new AsyncCallback<Map<Long,String>>() {
79
            @Override
80
            public void onSuccess(Map<Long, String> result) {
81
                vendors = result;
82
            }
83
            @Override
84
            public void onFailure(Throwable caught) {
2359 ankur.sing 85
                caught.printStackTrace();
2066 ankur.sing 86
            }
87
        });
5118 mandeep.dh 88
 
2066 ankur.sing 89
        catalogService.getAllWarehouses(new AsyncCallback<Map<Long,String>>() {
90
            @Override
91
            public void onSuccess(Map<Long, String> result) {
92
                warehouses = result;
93
            }
94
            @Override
95
            public void onFailure(Throwable caught) {
2359 ankur.sing 96
                caught.printStackTrace();
2066 ankur.sing 97
            }
98
        });
3558 rajveer 99
 
100
        catalogService.getAllSources(new AsyncCallback<Map<Long,String>>() {
101
            @Override
102
            public void onSuccess(Map<Long, String> result) {
103
                sources = result;
104
            }
105
            @Override
106
            public void onFailure(Throwable caught) {
107
                caught.printStackTrace();
108
            }
109
        });
4957 phani.kuma 110
 
111
        catalogService.getAllCategories(new AsyncCallback<List<String>>() {
112
            @Override
113
            public void onSuccess(List<String> result) {
114
            	categories = result;
115
            }
116
            @Override
117
            public void onFailure(Throwable caught) {
118
                caught.printStackTrace();
119
            }
120
        });
121
 
122
        catalogService.getAllBrands(new AsyncCallback<List<String>>() {
123
            @Override
124
            public void onSuccess(List<String> result) {
125
            	brands = result;
126
            }
127
            @Override
128
            public void onFailure(Throwable caught) {
129
                caught.printStackTrace();
130
            }
131
        });
5504 phani.kuma 132
 
5516 phani.kuma 133
        catalogService.getvoucherTypes(new AsyncCallback<Map<Long,String>>() {
5504 phani.kuma 134
            @Override
5516 phani.kuma 135
            public void onSuccess(Map<Long,String> result) {
136
            	setVoucherType(result);
5504 phani.kuma 137
            }
138
            @Override
139
            public void onFailure(Throwable caught) {
140
                caught.printStackTrace();
141
            }
142
        });
3558 rajveer 143
 
2066 ankur.sing 144
    }
2119 ankur.sing 145
 
5586 phani.kuma 146
    public static void setEntityIdMandatory(boolean entityIdMandatory) {
147
		Utils.entityIdMandatory = entityIdMandatory;
148
	}
149
 
150
	public static boolean isEntityIdMandatory() {
151
		return entityIdMandatory;
152
	}
153
 
154
	public static Map<Long, String> getAllVendors() {
2105 ankur.sing 155
        return vendors;
156
    }
6838 vikram.rag 157
	public static Map<Long, String> getAllInsurers() {
158
        return allInsurers;
159
    }
3558 rajveer 160
 
161
    public static Map<Long, String> getAllSources() {
162
        return sources;
163
    }
2066 ankur.sing 164
 
2105 ankur.sing 165
    public static Map<Long, String> getAllWarehouses() {
166
        return warehouses;
167
    }
5118 mandeep.dh 168
 
4957 phani.kuma 169
    public static List<String> getAllCategories() {
170
        return categories;
171
    }
172
 
173
    public static List<String> getAllBrands() {
174
        return brands;
175
    }
176
 
2066 ankur.sing 177
    public static String getVendorDesc(long id) {
178
        if(vendors == null) {
179
            return null;
180
        }
181
        return vendors.get(id);
182
    }
183
 
6530 vikram.rag 184
    public static List<ItemWarehouse> getignoredInventoryUpdateItemsIdsWarehouseIds(){
185
    	return ignoredInventoryUpdateItemsIdsWarehouseIds;
186
    }
187
 
3558 rajveer 188
    public static String getSourceDesc(long id) {
189
        if(sources == null) {
190
            return null;
191
        }
192
        return sources.get(id);
193
    }
194
 
2066 ankur.sing 195
    public static String getWarehouseDesc(long id) {
196
        if(warehouses == null) {
197
            return null;
198
        }
199
        return warehouses.get(id);
200
    }
5516 phani.kuma 201
 
202
    public static long getVoucherTypeId(String voucherDesc) {
203
        if(voucherType == null) {
204
            return 0;
205
        }
206
        for(Entry<Long, String> v : voucherType.entrySet()) {
207
            if(v.getValue().equals(voucherDesc)) {
208
                return v.getKey();
209
            }
210
        }
211
        return 0;
212
    }
213
 
2068 ankur.sing 214
    public static String getDisplayableDate(Long millis){
215
        Date date = new Date();
216
        date.setTime(millis);
217
        String dateString = date.toString();
218
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
219
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
220
        return dateString;      
221
    }
2105 ankur.sing 222
 
2427 ankur.sing 223
    /**
224
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
225
     * @param vendor name
226
     * @return vendor Id corresponding to the name
227
     */
2105 ankur.sing 228
    public static long getVendorId(String vendorDesc) {
229
        if(vendors == null) {
230
            return 0;
231
        }
232
        for(Entry<Long, String> v : vendors.entrySet()) {
233
            if(v.getValue().equals(vendorDesc)) {
234
                return v.getKey();
235
            }
236
        }
237
        return 0;
238
    }
2119 ankur.sing 239
 
2427 ankur.sing 240
    /**
4725 phani.kuma 241
     * Since in warehouse combo box, warehouse name is populated, this method is used to get Id from name
242
     * @param warehouse name
243
     * @return warehouse Id corresponding to the name
244
     */
245
    public static long getWarehouseId(String warehouseDesc) {
246
        if(warehouses == null) {
247
            return 0;
248
        }
249
        for(Entry<Long, String> w : warehouses.entrySet()) {
250
            if(w.getValue().equals(warehouseDesc)) {
251
                return w.getKey();
252
            }
253
        }
254
        return 0;
255
    }
256
 
257
    /**
3558 rajveer 258
     * Since in source combo box, source name is populated, this method is used to get Id from name
259
     * @param source name
260
     * @return source Id corresponding to the name
261
     */
262
    public static long getSourceId(String sourceDesc) {
263
        if(sources == null) {
264
            return 0;
265
        }
266
        for(Entry<Long, String> s : sources.entrySet()) {
267
            if(s.getValue().equals(sourceDesc)) {
268
                return s.getKey();
269
            }
270
        }
271
        return 0;
272
    }
273
 
274
    /**
2427 ankur.sing 275
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
276
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
277
     * Vendor Item Key should not be empty or null
278
     * @param item
2431 ankur.sing 279
     * @return true if validation is successful, false otherwise
2427 ankur.sing 280
     */
2119 ankur.sing 281
    public static boolean validateItem(Item item) {
282
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
283
            Window.alert("Product Group cannot be empty.");
284
            return false;
285
        }
286
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
287
            Window.alert("Brand cannot be empty.");
288
            return false;
289
        }
290
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
291
            Window.alert("Model Number cannot be empty.");
292
            return false;
293
        }
294
 
2489 ankur.sing 295
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 296
            Window.alert("Selling price cannot be more than MRP");
297
            return false;
298
        }
299
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
300
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 301
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 302
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
303
                    return false;
304
                }
305
                if(v.getTransferPrice() > v.getMop()) {
306
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
307
                    return false;
308
                }
309
            }
310
        }
2359 ankur.sing 311
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
312
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 313
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
314
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
315
                    return false;
316
                }
317
            }
318
        }
319
        return true;
320
    }
2126 ankur.sing 321
 
2427 ankur.sing 322
 
2126 ankur.sing 323
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
324
        switch(toStatus) {
325
        case PHASED_OUT: 
326
            switch(fromStatus) {
327
            case IN_PROCESS: return true;
328
            case CONTENT_COMPLETE: return true;
329
            case ACTIVE: return true;
330
            default: return false;
331
            }
332
        case IN_PROCESS:
333
            switch(fromStatus) {
334
            case PHASED_OUT: return true;
335
            default: return false;
336
            }
337
        case PAUSED:
338
            switch(fromStatus) {
339
            case ACTIVE: return true;
340
            default: return false;
341
            }
342
        case ACTIVE:
343
            switch(fromStatus) {
344
            case PAUSED: return true;
345
            default: return false;
346
            }
347
        }
348
        return true;
349
    }
5516 phani.kuma 350
 
351
	public static void setVoucherType(Map<Long, String> voucherType) {
352
		Utils.voucherType = voucherType;
353
	}
354
 
355
	public static Map<Long, String> getVoucherType() {
356
		return voucherType;
357
	}
2066 ankur.sing 358
}