Subversion Repositories SmartDukaan

Rev

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