Subversion Repositories SmartDukaan

Rev

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