Subversion Repositories SmartDukaan

Rev

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