Subversion Repositories SmartDukaan

Rev

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