Subversion Repositories SmartDukaan

Rev

Rev 5118 | Rev 5504 | 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
 
5384 phani.kuma 36
    private static Map<Long, String> vendors, warehouses, sources;
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
        });
3558 rajveer 96
 
2066 ankur.sing 97
    }
2119 ankur.sing 98
 
2105 ankur.sing 99
    public static Map<Long, String> getAllVendors() {
100
        return vendors;
101
    }
3558 rajveer 102
 
103
    public static Map<Long, String> getAllSources() {
104
        return sources;
105
    }
2066 ankur.sing 106
 
2105 ankur.sing 107
    public static Map<Long, String> getAllWarehouses() {
108
        return warehouses;
109
    }
5118 mandeep.dh 110
 
4957 phani.kuma 111
    public static List<String> getAllCategories() {
112
        return categories;
113
    }
114
 
115
    public static List<String> getAllBrands() {
116
        return brands;
117
    }
118
 
2066 ankur.sing 119
    public static String getVendorDesc(long id) {
120
        if(vendors == null) {
121
            return null;
122
        }
123
        return vendors.get(id);
124
    }
125
 
3558 rajveer 126
    public static String getSourceDesc(long id) {
127
        if(sources == null) {
128
            return null;
129
        }
130
        return sources.get(id);
131
    }
132
 
2066 ankur.sing 133
    public static String getWarehouseDesc(long id) {
134
        if(warehouses == null) {
135
            return null;
136
        }
137
        return warehouses.get(id);
138
    }
5118 mandeep.dh 139
 
2068 ankur.sing 140
    public static String getDisplayableDate(Long millis){
141
        Date date = new Date();
142
        date.setTime(millis);
143
        String dateString = date.toString();
144
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
145
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
146
        return dateString;      
147
    }
2105 ankur.sing 148
 
2427 ankur.sing 149
    /**
150
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
151
     * @param vendor name
152
     * @return vendor Id corresponding to the name
153
     */
2105 ankur.sing 154
    public static long getVendorId(String vendorDesc) {
155
        if(vendors == null) {
156
            return 0;
157
        }
158
        for(Entry<Long, String> v : vendors.entrySet()) {
159
            if(v.getValue().equals(vendorDesc)) {
160
                return v.getKey();
161
            }
162
        }
163
        return 0;
164
    }
2119 ankur.sing 165
 
2427 ankur.sing 166
    /**
4725 phani.kuma 167
     * Since in warehouse combo box, warehouse name is populated, this method is used to get Id from name
168
     * @param warehouse name
169
     * @return warehouse Id corresponding to the name
170
     */
171
    public static long getWarehouseId(String warehouseDesc) {
172
        if(warehouses == null) {
173
            return 0;
174
        }
175
        for(Entry<Long, String> w : warehouses.entrySet()) {
176
            if(w.getValue().equals(warehouseDesc)) {
177
                return w.getKey();
178
            }
179
        }
180
        return 0;
181
    }
182
 
183
    /**
3558 rajveer 184
     * Since in source combo box, source name is populated, this method is used to get Id from name
185
     * @param source name
186
     * @return source Id corresponding to the name
187
     */
188
    public static long getSourceId(String sourceDesc) {
189
        if(sources == null) {
190
            return 0;
191
        }
192
        for(Entry<Long, String> s : sources.entrySet()) {
193
            if(s.getValue().equals(sourceDesc)) {
194
                return s.getKey();
195
            }
196
        }
197
        return 0;
198
    }
199
 
200
    /**
2427 ankur.sing 201
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
202
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
203
     * Vendor Item Key should not be empty or null
204
     * @param item
2431 ankur.sing 205
     * @return true if validation is successful, false otherwise
2427 ankur.sing 206
     */
2119 ankur.sing 207
    public static boolean validateItem(Item item) {
208
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
209
            Window.alert("Product Group cannot be empty.");
210
            return false;
211
        }
212
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
213
            Window.alert("Brand cannot be empty.");
214
            return false;
215
        }
216
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
217
            Window.alert("Model Number cannot be empty.");
218
            return false;
219
        }
220
 
2489 ankur.sing 221
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 222
            Window.alert("Selling price cannot be more than MRP");
223
            return false;
224
        }
225
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
226
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 227
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 228
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
229
                    return false;
230
                }
231
                if(v.getTransferPrice() > v.getMop()) {
232
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
233
                    return false;
234
                }
235
            }
236
        }
2359 ankur.sing 237
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
238
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 239
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
240
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
241
                    return false;
242
                }
243
            }
244
        }
245
        return true;
246
    }
2126 ankur.sing 247
 
2427 ankur.sing 248
 
2126 ankur.sing 249
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
250
        switch(toStatus) {
251
        case PHASED_OUT: 
252
            switch(fromStatus) {
253
            case IN_PROCESS: return true;
254
            case CONTENT_COMPLETE: return true;
255
            case ACTIVE: return true;
256
            default: return false;
257
            }
258
        case IN_PROCESS:
259
            switch(fromStatus) {
260
            case PHASED_OUT: return true;
261
            default: return false;
262
            }
263
        case PAUSED:
264
            switch(fromStatus) {
265
            case ACTIVE: return true;
266
            default: return false;
267
            }
268
        case ACTIVE:
269
            switch(fromStatus) {
270
            case PAUSED: return true;
271
            default: return false;
272
            }
273
        }
274
        return true;
275
    }
2066 ankur.sing 276
}