Subversion Repositories SmartDukaan

Rev

Rev 3558 | Rev 4957 | 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;
2066 ankur.sing 7
import java.util.Map;
2105 ankur.sing 8
import java.util.Map.Entry;
2066 ankur.sing 9
 
10
import com.google.gwt.core.client.GWT;
2119 ankur.sing 11
import com.google.gwt.user.client.Window;
2066 ankur.sing 12
import com.google.gwt.user.client.rpc.AsyncCallback;
13
 
2427 ankur.sing 14
/**
15
 * Utility methods goes here
16
 *
17
 */
2066 ankur.sing 18
public class Utils {
2427 ankur.sing 19
    // item status values. These should be analogous to status enum in status.java
20
    // These values are used in the client side to check if transition from one status to another is 
21
    // valid. Also see validateStatusChange method
22
    // FIXME: These should be read from the ThriftConfig file and not hard-coded here.
2126 ankur.sing 23
    public static final int PHASED_OUT = 0,
24
                            DELETED = 1,
25
                            PAUSED = 2,
26
                            ACTIVE = 3,
27
                            IN_PROCESS = 4,
28
                            CONTENT_COMPLETE = 5;
2427 ankur.sing 29
 
30
    // role column in helper.catalogdashboarduser table. role specifies 
31
    // the username-pwd pair for staging or for production.
32
    // production password is needed while updating item on production server.
2359 ankur.sing 33
    public static final int ROLE_STAGING = 0, ROLE_PRODUCTION = 1;
2427 ankur.sing 34
 
3558 rajveer 35
    private static Map<Long, String> vendors, warehouses, sources;
2427 ankur.sing 36
 
2066 ankur.sing 37
    private static final CatalogServiceAsync catalogService = GWT.create(CatalogService.class);
2126 ankur.sing 38
 
2066 ankur.sing 39
    static {
40
        catalogService.getAllVendors(new AsyncCallback<Map<Long,String>>() {
41
            @Override
42
            public void onSuccess(Map<Long, String> result) {
43
                vendors = result;
44
            }
45
            @Override
46
            public void onFailure(Throwable caught) {
2359 ankur.sing 47
                caught.printStackTrace();
2066 ankur.sing 48
            }
49
        });
50
 
51
        catalogService.getAllWarehouses(new AsyncCallback<Map<Long,String>>() {
52
            @Override
53
            public void onSuccess(Map<Long, String> result) {
54
                warehouses = result;
55
            }
56
            @Override
57
            public void onFailure(Throwable caught) {
2359 ankur.sing 58
                caught.printStackTrace();
2066 ankur.sing 59
            }
60
        });
3558 rajveer 61
 
62
        catalogService.getAllSources(new AsyncCallback<Map<Long,String>>() {
63
            @Override
64
            public void onSuccess(Map<Long, String> result) {
65
                sources = result;
66
            }
67
            @Override
68
            public void onFailure(Throwable caught) {
69
                caught.printStackTrace();
70
            }
71
        });
72
 
2066 ankur.sing 73
    }
2119 ankur.sing 74
 
2105 ankur.sing 75
    public static Map<Long, String> getAllVendors() {
76
        return vendors;
77
    }
3558 rajveer 78
 
79
    public static Map<Long, String> getAllSources() {
80
        return sources;
81
    }
2066 ankur.sing 82
 
2105 ankur.sing 83
    public static Map<Long, String> getAllWarehouses() {
84
        return warehouses;
85
    }
86
 
2066 ankur.sing 87
    public static String getVendorDesc(long id) {
88
        if(vendors == null) {
89
            return null;
90
        }
91
        return vendors.get(id);
92
    }
93
 
3558 rajveer 94
    public static String getSourceDesc(long id) {
95
        if(sources == null) {
96
            return null;
97
        }
98
        return sources.get(id);
99
    }
100
 
2066 ankur.sing 101
    public static String getWarehouseDesc(long id) {
102
        if(warehouses == null) {
103
            return null;
104
        }
105
        return warehouses.get(id);
106
    }
2068 ankur.sing 107
 
108
    public static String getDisplayableDate(Long millis){
109
        Date date = new Date();
110
        date.setTime(millis);
111
        String dateString = date.toString();
112
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
113
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
114
        return dateString;      
115
    }
2105 ankur.sing 116
 
2427 ankur.sing 117
    /**
118
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
119
     * @param vendor name
120
     * @return vendor Id corresponding to the name
121
     */
2105 ankur.sing 122
    public static long getVendorId(String vendorDesc) {
123
        if(vendors == null) {
124
            return 0;
125
        }
126
        for(Entry<Long, String> v : vendors.entrySet()) {
127
            if(v.getValue().equals(vendorDesc)) {
128
                return v.getKey();
129
            }
130
        }
131
        return 0;
132
    }
2119 ankur.sing 133
 
2427 ankur.sing 134
    /**
4725 phani.kuma 135
     * Since in warehouse combo box, warehouse name is populated, this method is used to get Id from name
136
     * @param warehouse name
137
     * @return warehouse Id corresponding to the name
138
     */
139
    public static long getWarehouseId(String warehouseDesc) {
140
        if(warehouses == null) {
141
            return 0;
142
        }
143
        for(Entry<Long, String> w : warehouses.entrySet()) {
144
            if(w.getValue().equals(warehouseDesc)) {
145
                return w.getKey();
146
            }
147
        }
148
        return 0;
149
    }
150
 
151
    /**
3558 rajveer 152
     * Since in source combo box, source name is populated, this method is used to get Id from name
153
     * @param source name
154
     * @return source Id corresponding to the name
155
     */
156
    public static long getSourceId(String sourceDesc) {
157
        if(sources == null) {
158
            return 0;
159
        }
160
        for(Entry<Long, String> s : sources.entrySet()) {
161
            if(s.getValue().equals(sourceDesc)) {
162
                return s.getKey();
163
            }
164
        }
165
        return 0;
166
    }
167
 
168
    /**
2427 ankur.sing 169
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
170
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
171
     * Vendor Item Key should not be empty or null
172
     * @param item
2431 ankur.sing 173
     * @return true if validation is successful, false otherwise
2427 ankur.sing 174
     */
2119 ankur.sing 175
    public static boolean validateItem(Item item) {
176
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
177
            Window.alert("Product Group cannot be empty.");
178
            return false;
179
        }
180
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
181
            Window.alert("Brand cannot be empty.");
182
            return false;
183
        }
184
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
185
            Window.alert("Model Number cannot be empty.");
186
            return false;
187
        }
188
 
2489 ankur.sing 189
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 190
            Window.alert("Selling price cannot be more than MRP");
191
            return false;
192
        }
193
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
194
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 195
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 196
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
197
                    return false;
198
                }
199
                if(v.getTransferPrice() > v.getMop()) {
200
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
201
                    return false;
202
                }
203
            }
204
        }
2359 ankur.sing 205
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
206
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 207
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
208
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
209
                    return false;
210
                }
211
            }
212
        }
213
        return true;
214
    }
2126 ankur.sing 215
 
2427 ankur.sing 216
 
2126 ankur.sing 217
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
218
        switch(toStatus) {
219
        case PHASED_OUT: 
220
            switch(fromStatus) {
221
            case IN_PROCESS: return true;
222
            case CONTENT_COMPLETE: return true;
223
            case ACTIVE: return true;
224
            default: return false;
225
            }
226
        case IN_PROCESS:
227
            switch(fromStatus) {
228
            case PHASED_OUT: return true;
229
            default: return false;
230
            }
231
        case PAUSED:
232
            switch(fromStatus) {
233
            case ACTIVE: return true;
234
            default: return false;
235
            }
236
        case ACTIVE:
237
            switch(fromStatus) {
238
            case PAUSED: return true;
239
            default: return false;
240
            }
241
        }
242
        return true;
243
    }
2066 ankur.sing 244
}