Subversion Repositories SmartDukaan

Rev

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