Subversion Repositories SmartDukaan

Rev

Rev 2489 | Rev 3558 | 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
 
2066 ankur.sing 35
    private static Map<Long, String> vendors, warehouses;
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
        });
61
    }
2119 ankur.sing 62
 
2105 ankur.sing 63
    public static Map<Long, String> getAllVendors() {
64
        return vendors;
65
    }
2066 ankur.sing 66
 
2105 ankur.sing 67
    public static Map<Long, String> getAllWarehouses() {
68
        return warehouses;
69
    }
70
 
2066 ankur.sing 71
    public static String getVendorDesc(long id) {
72
        if(vendors == null) {
73
            return null;
74
        }
75
        return vendors.get(id);
76
    }
77
 
78
    public static String getWarehouseDesc(long id) {
79
        if(warehouses == null) {
80
            return null;
81
        }
82
        return warehouses.get(id);
83
    }
2068 ankur.sing 84
 
85
    public static String getDisplayableDate(Long millis){
86
        Date date = new Date();
87
        date.setTime(millis);
88
        String dateString = date.toString();
89
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
90
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
91
        return dateString;      
92
    }
2105 ankur.sing 93
 
2427 ankur.sing 94
    /**
95
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
96
     * @param vendor name
97
     * @return vendor Id corresponding to the name
98
     */
2105 ankur.sing 99
    public static long getVendorId(String vendorDesc) {
100
        if(vendors == null) {
101
            return 0;
102
        }
103
        for(Entry<Long, String> v : vendors.entrySet()) {
104
            if(v.getValue().equals(vendorDesc)) {
105
                return v.getKey();
106
            }
107
        }
108
        return 0;
109
    }
2119 ankur.sing 110
 
2427 ankur.sing 111
    /**
112
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
113
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
114
     * Vendor Item Key should not be empty or null
115
     * @param item
2431 ankur.sing 116
     * @return true if validation is successful, false otherwise
2427 ankur.sing 117
     */
2119 ankur.sing 118
    public static boolean validateItem(Item item) {
119
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
120
            Window.alert("Product Group cannot be empty.");
121
            return false;
122
        }
123
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
124
            Window.alert("Brand cannot be empty.");
125
            return false;
126
        }
127
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
128
            Window.alert("Model Number cannot be empty.");
129
            return false;
130
        }
131
 
2489 ankur.sing 132
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 133
            Window.alert("Selling price cannot be more than MRP");
134
            return false;
135
        }
136
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
137
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 138
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 139
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
140
                    return false;
141
                }
142
                if(v.getTransferPrice() > v.getMop()) {
143
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
144
                    return false;
145
                }
146
            }
147
        }
2359 ankur.sing 148
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
149
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 150
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
151
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
152
                    return false;
153
                }
154
            }
155
        }
156
        return true;
157
    }
2126 ankur.sing 158
 
2427 ankur.sing 159
 
2126 ankur.sing 160
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
161
        switch(toStatus) {
162
        case PHASED_OUT: 
163
            switch(fromStatus) {
164
            case IN_PROCESS: return true;
165
            case CONTENT_COMPLETE: return true;
166
            case ACTIVE: return true;
167
            default: return false;
168
            }
169
        case IN_PROCESS:
170
            switch(fromStatus) {
171
            case PHASED_OUT: return true;
172
            default: return false;
173
            }
174
        case PAUSED:
175
            switch(fromStatus) {
176
            case ACTIVE: return true;
177
            default: return false;
178
            }
179
        case ACTIVE:
180
            switch(fromStatus) {
181
            case PAUSED: return true;
182
            default: return false;
183
            }
184
        }
185
        return true;
186
    }
2066 ankur.sing 187
}