Subversion Repositories SmartDukaan

Rev

Rev 2566 | Rev 4725 | 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
    }
3558 rajveer 133
 
2119 ankur.sing 134
 
2427 ankur.sing 135
    /**
3558 rajveer 136
     * Since in source combo box, source name is populated, this method is used to get Id from name
137
     * @param source name
138
     * @return source Id corresponding to the name
139
     */
140
    public static long getSourceId(String sourceDesc) {
141
        if(sources == null) {
142
            return 0;
143
        }
144
        for(Entry<Long, String> s : sources.entrySet()) {
145
            if(s.getValue().equals(sourceDesc)) {
146
                return s.getKey();
147
            }
148
        }
149
        return 0;
150
    }
151
 
152
    /**
2427 ankur.sing 153
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
154
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
155
     * Vendor Item Key should not be empty or null
156
     * @param item
2431 ankur.sing 157
     * @return true if validation is successful, false otherwise
2427 ankur.sing 158
     */
2119 ankur.sing 159
    public static boolean validateItem(Item item) {
160
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
161
            Window.alert("Product Group cannot be empty.");
162
            return false;
163
        }
164
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
165
            Window.alert("Brand cannot be empty.");
166
            return false;
167
        }
168
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
169
            Window.alert("Model Number cannot be empty.");
170
            return false;
171
        }
172
 
2489 ankur.sing 173
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 174
            Window.alert("Selling price cannot be more than MRP");
175
            return false;
176
        }
177
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
178
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 179
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 180
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
181
                    return false;
182
                }
183
                if(v.getTransferPrice() > v.getMop()) {
184
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
185
                    return false;
186
                }
187
            }
188
        }
2359 ankur.sing 189
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
190
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 191
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
192
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
193
                    return false;
194
                }
195
            }
196
        }
197
        return true;
198
    }
2126 ankur.sing 199
 
2427 ankur.sing 200
 
2126 ankur.sing 201
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
202
        switch(toStatus) {
203
        case PHASED_OUT: 
204
            switch(fromStatus) {
205
            case IN_PROCESS: return true;
206
            case CONTENT_COMPLETE: return true;
207
            case ACTIVE: return true;
208
            default: return false;
209
            }
210
        case IN_PROCESS:
211
            switch(fromStatus) {
212
            case PHASED_OUT: return true;
213
            default: return false;
214
            }
215
        case PAUSED:
216
            switch(fromStatus) {
217
            case ACTIVE: return true;
218
            default: return false;
219
            }
220
        case ACTIVE:
221
            switch(fromStatus) {
222
            case PAUSED: return true;
223
            default: return false;
224
            }
225
        }
226
        return true;
227
    }
2066 ankur.sing 228
}