Subversion Repositories SmartDukaan

Rev

Rev 2431 | Rev 2566 | 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;
2126 ankur.sing 11
import com.google.gwt.user.client.DOM;
2119 ankur.sing 12
import com.google.gwt.user.client.Window;
2066 ankur.sing 13
import com.google.gwt.user.client.rpc.AsyncCallback;
2126 ankur.sing 14
import com.google.gwt.user.client.ui.RootPanel;
2066 ankur.sing 15
 
2427 ankur.sing 16
/**
17
 * Utility methods goes here
18
 *
19
 */
2066 ankur.sing 20
public class Utils {
2427 ankur.sing 21
    // item status values. These should be analogous to status enum in status.java
22
    // These values are used in the client side to check if transition from one status to another is 
23
    // valid. Also see validateStatusChange method
24
    // FIXME: These should be read from the ThriftConfig file and not hard-coded here.
2126 ankur.sing 25
    public static final int PHASED_OUT = 0,
26
                            DELETED = 1,
27
                            PAUSED = 2,
28
                            ACTIVE = 3,
29
                            IN_PROCESS = 4,
30
                            CONTENT_COMPLETE = 5;
2427 ankur.sing 31
 
32
    // role column in helper.catalogdashboarduser table. role specifies 
33
    // the username-pwd pair for staging or for production.
34
    // production password is needed while updating item on production server.
2359 ankur.sing 35
    public static final int ROLE_STAGING = 0, ROLE_PRODUCTION = 1;
2427 ankur.sing 36
 
2066 ankur.sing 37
    private static Map<Long, String> vendors, warehouses;
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
        });
52
 
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
        });
63
    }
2119 ankur.sing 64
 
2105 ankur.sing 65
    public static Map<Long, String> getAllVendors() {
66
        return vendors;
67
    }
2066 ankur.sing 68
 
2105 ankur.sing 69
    public static Map<Long, String> getAllWarehouses() {
70
        return warehouses;
71
    }
72
 
2066 ankur.sing 73
    public static String getVendorDesc(long id) {
74
        if(vendors == null) {
75
            return null;
76
        }
77
        return vendors.get(id);
78
    }
79
 
80
    public static String getWarehouseDesc(long id) {
81
        if(warehouses == null) {
82
            return null;
83
        }
84
        return warehouses.get(id);
85
    }
2068 ankur.sing 86
 
87
    public static String getDisplayableDate(Long millis){
88
        Date date = new Date();
89
        date.setTime(millis);
90
        String dateString = date.toString();
91
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
92
        dateString = dateString.substring(0, dateString.lastIndexOf(" "));
93
        return dateString;      
94
    }
2105 ankur.sing 95
 
2427 ankur.sing 96
    /**
97
     * Since in vendor combo box, vendor name is populated, this method is used to get Id from name
98
     * @param vendor name
99
     * @return vendor Id corresponding to the name
100
     */
2105 ankur.sing 101
    public static long getVendorId(String vendorDesc) {
102
        if(vendors == null) {
103
            return 0;
104
        }
105
        for(Entry<Long, String> v : vendors.entrySet()) {
106
            if(v.getValue().equals(vendorDesc)) {
107
                return v.getKey();
108
            }
109
        }
110
        return 0;
111
    }
2119 ankur.sing 112
 
2427 ankur.sing 113
    /**
114
     * validates item object. ProductGroup, brand, modelNumber, modelName should not be empty or null.
115
     * Also does item and vendor prices validations (MRP >= SP, MRP >= MOP, TP <= MOP)
116
     * Vendor Item Key should not be empty or null
117
     * @param item
2431 ankur.sing 118
     * @return true if validation is successful, false otherwise
2427 ankur.sing 119
     */
2119 ankur.sing 120
    public static boolean validateItem(Item item) {
121
        if(item.getProductGroup() == null || item.getProductGroup().isEmpty()) {
122
            Window.alert("Product Group cannot be empty.");
123
            return false;
124
        }
125
        if(item.getBrand() == null || item.getBrand().isEmpty()) {
126
            Window.alert("Brand cannot be empty.");
127
            return false;
128
        }
129
        if(item.getModelNumber() == null || item.getModelNumber().isEmpty()) {
130
            Window.alert("Model Number cannot be empty.");
131
            return false;
132
        }
133
 
2489 ankur.sing 134
        if(item.getSellingPrice() != null && item.getMrp() != null && item.getSellingPrice() > item.getMrp()) {
2119 ankur.sing 135
            Window.alert("Selling price cannot be more than MRP");
136
            return false;
137
        }
138
        if(item.getVendorPricesMap() != null && !item.getVendorPricesMap().isEmpty()) {
139
            for(VendorPricings v : item.getVendorPricesMap().values()) {
2489 ankur.sing 140
                if(item.getMrp() != null && item.getMrp() < v.getMop()) {
2119 ankur.sing 141
                    Window.alert("MRP cannot be less than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
142
                    return false;
143
                }
144
                if(v.getTransferPrice() > v.getMop()) {
145
                    Window.alert("Transfer Price cannot be more than MOP. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
146
                    return false;
147
                }
148
            }
149
        }
2359 ankur.sing 150
        if(item.getVendorKeysMap() != null && !item.getVendorKeysMap().isEmpty()) {
151
            for(VendorItemMapping v : item.getVendorKeysMap().values()) {
2119 ankur.sing 152
                if(v.getItemKey() == null || v.getItemKey().isEmpty()) {
153
                    Window.alert("Item Key cannot be empty. Vendor: " + Utils.getVendorDesc(v.getVendorId()));
154
                    return false;
155
                }
156
            }
157
        }
158
        return true;
159
    }
2126 ankur.sing 160
 
2427 ankur.sing 161
 
2126 ankur.sing 162
    public static boolean validateStatusChange(int fromStatus, int toStatus) {
163
        switch(toStatus) {
164
        case PHASED_OUT: 
165
            switch(fromStatus) {
166
            case IN_PROCESS: return true;
167
            case CONTENT_COMPLETE: return true;
168
            case ACTIVE: return true;
169
            default: return false;
170
            }
171
        case IN_PROCESS:
172
            switch(fromStatus) {
173
            case PHASED_OUT: return true;
174
            default: return false;
175
            }
176
        case PAUSED:
177
            switch(fromStatus) {
178
            case ACTIVE: return true;
179
            default: return false;
180
            }
181
        case ACTIVE:
182
            switch(fromStatus) {
183
            case PAUSED: return true;
184
            default: return false;
185
            }
186
        }
187
        return true;
188
    }
2066 ankur.sing 189
}