Subversion Repositories SmartDukaan

Rev

Rev 16569 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9269 amit.gupta 1
package in.shop2020.mobileapi.serving.utils;
9103 anupam.sin 2
 
3
import in.shop2020.config.ConfigException;
12620 amit.gupta 4
import in.shop2020.model.v1.catalog.ExclusiveAffiliateItemInfo;
9103 anupam.sin 5
import in.shop2020.model.v1.order.LineItem;
6
import in.shop2020.model.v1.order.Order;
7
import in.shop2020.model.v1.order.RechargeOrderStatus;
8
import in.shop2020.model.v1.order.RechargePlan;
9
import in.shop2020.model.v1.order.RechargeType;
10
import in.shop2020.model.v1.user.Cart;
11
import in.shop2020.model.v1.user.Line;
12620 amit.gupta 12
import in.shop2020.thrift.clients.CatalogClient;
9103 anupam.sin 13
import in.shop2020.thrift.clients.TransactionClient;
14
import in.shop2020.thrift.clients.config.ConfigClient;
15
 
16541 amit.gupta 16
import java.io.BufferedReader;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.io.InputStreamReader;
9103 anupam.sin 20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collections;
12620 amit.gupta 23
import java.util.Date;
9103 anupam.sin 24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27
 
28
import javax.mail.internet.AddressException;
29
import javax.mail.internet.InternetAddress;
30
 
31
import org.apache.commons.lang.StringUtils;
16541 amit.gupta 32
import org.apache.http.HttpEntity;
33
import org.apache.http.HttpResponse;
34
import org.apache.http.client.HttpClient;
35
import org.apache.http.client.methods.HttpGet;
36
import org.apache.http.impl.client.DefaultHttpClient;
9103 anupam.sin 37
import org.apache.log4j.Logger;
38
 
39
public class Utils {
40
    private static Logger logger = Logger.getLogger(Utils.class);
41
 
42
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
43
	public static final String EXPORT_JAVASCRIPT_CONTENT_PATH = "/var/lib/tomcat6/webapps/export/javascripts/";
11038 amit.gupta 44
	public static final String STATIC_SERVER_PATH = "http://static1.saholic.com/images/mobileapi/";
9103 anupam.sin 45
	public static final long ROOT_CATEGORY = 10000;
46
	public static final long MOBILE_PHONES_CATEGORY = 10001;
47
	public static final long MOBILE_ACCESSORIES_CATEGORY = 10011;
48
	public static final long TABLETS_CATEGORY = 10010;
49
    public static final long LAPTOPS_CATEGORY = 10050;
50
    public static final long CAMERAS_CATEGORY = 11001;
16569 amit.gupta 51
    private static final String DTR_ENTITY_URL = "http://dtr:8057/ordered-offers/4/%d";
9103 anupam.sin 52
 
13425 amit.gupta 53
    @SuppressWarnings("serial")
54
	public static final Map<String, String> NET_BANKING_CODES_MAP = Collections.unmodifiableMap(
55
    		new HashMap<String, String>(){
56
	    		{
57
	    			put("1004","AXIB");
58
	    			put("1135","CRPB");
59
	    			put("1029","FEDB");
60
	    			put("1007","HDFB");
13433 amit.gupta 61
	    			put("1016","ICIB");
62
	    			put("1032","SBIB");
63
	    			put("1034","SBHB");
64
	    			put("1038","SBMB");
65
	    			put("1039","SBTB");
13469 amit.gupta 66
	    			//put("1127","CITNB");
13442 amit.gupta 67
	    			put("1033","SBBJB");
13429 amit.gupta 68
    				//Oriental Bank of Commerce
69
	    			//put("1154","");
13425 amit.gupta 70
	    			put("1147","CBIB");
71
	    			put("1146","YESB");
72
	    			put("1214","BOIB");
73
	    			put("1215","CUBB");
13436 amit.gupta 74
	    			put("1212","UNIB");
13425 amit.gupta 75
	    			put("1224","CABB");
13429 amit.gupta 76
	    			put("1210","INGB");
77
	    			put("1148","162B");
78
	    			put("1273","DLSB");
13425 amit.gupta 79
	    			put("1015","JAKB");
80
	    			put("1133","KRKB");
81
	    			put("1143","INDB");
13436 amit.gupta 82
	    			put("1216","UBIB");
13425 amit.gupta 83
	    			put("1213","INOB");
84
	    			put("1229","BOMB");
13429 amit.gupta 85
	    			put("1601","CSBN");
86
	    			put("1602","DCBB");
87
	    			put("1603","DSHB");
88
	    			put("1604","IDBB");
89
	    			put("1605","INIB");
90
	    			put("1606","KRVB");
91
	    			put("1607","PNBB");
92
	    			put("1608","SOIB");
93
	    			put("1609","SRSWT");
94
	    			put("1610","VJYB");	    			
13425 amit.gupta 95
	    		}
96
    		});
97
 
9103 anupam.sin 98
    static Map<Long, String> providersMap;
99
    static Map<Long, String> mobileProvidersMap;
100
    static Map<Long, String> dthProvidersMap;
101
    static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<Long, List<RechargePlan>>(); 
102
    private static Map<String, Map<String, String>> uriOperatorMap = new HashMap<String, Map<String,String>>();
12620 amit.gupta 103
    static Map<Long, ExclusiveAffiliateItemInfo> exclusiveAffiliateItems = null;
104
    static long fifteenMinsCounter = 0l;
16541 amit.gupta 105
    static long thirtyMinsCounter = 0l;
9103 anupam.sin 106
 
107
    public static final List<RechargeOrderStatus> refundableRechargeStatusList = new ArrayList<RechargeOrderStatus>(Arrays.asList(new RechargeOrderStatus[] 
108
                                                                                                                                                     {RechargeOrderStatus.RECHARGE_FAILED, 
109
                                                                                                                                                      RechargeOrderStatus.RECHARGE_FAILED_REFUNDED}));
110
 
111
    public static Map<RechargeOrderStatus, String> statusMap = new HashMap<RechargeOrderStatus, String>();
112
 
113
    //public static final List<String> multiValuedFacet = Arrays.asList("F_50006");
114
 
9269 amit.gupta 115
    public static final  List<String>rootfacetDefIDs = Arrays.asList("F_50010","F_50011", "F_50001", "F_50028");
9103 anupam.sin 116
    public static final  String availabilityFacet = "F_50028";
117
	public static final Map<String, String> FACET_LABEL_MAP = Collections.unmodifiableMap(
118
			new HashMap<String, String>(){
119
				/**
120
				 * 
121
				 */
122
				private static final long serialVersionUID = 1L;
123
 
124
				{
125
					put("F_50010","Category");
126
					put("F_50011","Sub Category");
127
					put("F_50002","Price");
128
					put("F_50001","Brand");
129
					put("F_50006","Data Connectivity");
130
					put("F_50007","Camera Resolution");
131
					put("F_50012","Display");
132
					put("F_50013","Operating System");
133
					put("F_50014","RAM");
134
					put("F_50015","Storage Capacity");
135
					put("F_50017","Processor");
136
					put("F_50018","Capacity");
137
					put("F_50019","Class");
138
					put("F_50020","Capacity");
139
					put("F_50021","Capacity");
140
					put("F_50022","Type");
141
					put("F_50023","Interface");
142
					//Resolution for Composite
143
					put("F_50024","Resolution");
144
					put("F_50025","Optical Zoom");
145
					put("F_50026","Display Size");
146
					//Resolution for DSLR
147
					put("F_50027","Resolution");
148
					put("F_50028","Availability");
149
					put("F_50031","Operating System");
150
					put("F_50032","Screen Size");	//Screen size for mobiles
151
					put("F_50033","Screen Size");	//Screen size for laptop
152
					put("F_50034","Screen Size"); 	//Screen size for tablets
153
					put("F_50035","Operating System"); 	//Screen size for tablets
154
					put("F_50036","Camera Resolution"); 	//Screen size for tablets
155
					put("F_50037","Voice Call Facility"); 	//Screen size for tablets
9475 amit.gupta 156
					put("F_50038","Mobile Type");
12151 amit.gupta 157
					put("F_50039", "Your Deals");
9103 anupam.sin 158
				}
159
		});
160
 
161
    static {
162
        statusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
163
        statusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
164
        statusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
16134 manish.sha 165
        statusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed Refunded");
9103 anupam.sin 166
        statusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
167
        statusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
168
        statusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
16135 manish.sha 169
        statusMap.put(RechargeOrderStatus.PAYMENT_PENDING, "Payment Failed");
16134 manish.sha 170
        statusMap.put(RechargeOrderStatus.INIT, "Payment Initiated");
171
        statusMap.put(RechargeOrderStatus.RECHARGE_UNKNOWN, "Recharge In Process");
172
        statusMap.put(RechargeOrderStatus.RECHARGE_IN_PROCESS, "Recharge In Process");
9103 anupam.sin 173
 
174
        TransactionClient tcl;
175
        try {
176
            tcl = new TransactionClient();
177
            mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
178
            dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
179
//            providersMap.putAll(mobileProvidersMap);
180
//            providersMap.putAll(dthProvidersMap);
181
            for (Long operatorId : mobileProvidersMap.keySet()) {
182
                List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId);
183
                if (!plans.isEmpty()) {
184
                    operatorPlanMap.put(operatorId, plans);
185
                }
186
            }
187
            for (Long operatorId : mobileProvidersMap.keySet()) {
188
            	Map<String,String> valueMap = new HashMap<String,String>();
189
            	valueMap.put("Id", operatorId+"");
190
                valueMap.put("Name", mobileProvidersMap.get(operatorId)+"");
191
                valueMap.put("ServiceType", "1");
192
                valueMap.put("ServiceName", "Prepaid Mobile");
193
                uriOperatorMap.put(mobileProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
194
            }
195
            for (Long operatorId : dthProvidersMap.keySet()) {
196
            	Map<String,String> valueMap = new HashMap<String,String>();
197
            	valueMap.put("Id", operatorId+"");
198
            	valueMap.put("Name", dthProvidersMap.get(operatorId)+"");
199
                valueMap.put("ServiceType", "2");
200
                valueMap.put("ServiceName", "DTH TV");
201
                uriOperatorMap.put(dthProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
202
            }
203
 
204
            Map<String,String> valueMap = new HashMap<String,String>();
205
            valueMap.put("Id", "0");
206
	    	valueMap.put("Name", "Mobile and DTH");
207
	        valueMap.put("ServiceType", "1");
208
	        valueMap.put("ServiceName", "");
209
	        uriOperatorMap.put("recharge", valueMap);
210
//            providersMap.putAll(tcl.getClient().getServiceProviders(RechargeType.DTH));
211
 
212
 
213
        } catch (Exception e) {
214
            logger.error("Could not get providers", e);
215
        }
216
    }
217
 
218
 
219
 
220
	private static String getExportPath(){
221
		String exportPath=null;
222
		ConfigClient client = ConfigClient.getClient();
223
		try{
224
			exportPath = client.get("export_entities_path");
225
		}catch(ConfigException ce){
226
			logger.error("Unable to read export path from the config client: ", ce);
227
			logger.warn("Setting the default export path");
228
			exportPath = "/var/lib/tomcat6/webapps/export/html/entities/";
229
		}
230
		return exportPath;
231
	}
232
 
233
	public static boolean validatePin(String pincode) {
234
		int pin;
235
		try {
236
			pin = Integer.parseInt(pincode);
237
		}
238
		catch (NumberFormatException e) {
239
			return false;
240
		}
241
 
242
		if (pin < 100000 || pin > 999999) {
243
			return false;
244
		}
245
		return true;
246
	}
247
 
248
	public static boolean isValidEmail(String emailAddress)	{
249
 
250
		if(emailAddress == null || emailAddress.isEmpty())	{
251
			return false;
252
		}
253
		try {
254
			InternetAddress address = new InternetAddress(emailAddress, true);
255
			address.validate();
256
			return true;
257
 
258
		} catch (AddressException e) {
259
			logger.error(emailAddress + " is not valid email address ", e);
260
			return false;
261
		}
262
	}
263
 
264
	public static boolean validatePhone(String phone) {
265
		long iPhone;
266
		try {
267
    		iPhone = Long.parseLong(phone);
268
		}
269
		catch (NumberFormatException e) {
270
			return false;
271
		}
272
 
273
		if (iPhone < 1000000000l || iPhone > 9999999999l) {
274
			return false;
275
		}
276
		return true;
277
	}
278
 
279
	public static String getItemIdStringInCart(Cart cart) {
280
	    List<String> itemIds = new ArrayList<String>();
281
        for (Line line : cart.getLines()) {
282
            itemIds.add(Long.toString(line.getItemId()));
283
        }
284
        return StringUtils.join(itemIds, '_');
285
    }
286
 
287
	public static String getItemIdStringFromOrders(List<Order> orders) {
288
        List<String> itemIds = new ArrayList<String>();
289
        if (orders != null) {
290
            for (Order order : orders) {
291
                for (LineItem lItem : order.getLineitems()) {
292
                    itemIds.add(Long.toString(lItem.getItem_id()));
293
                }
294
            }
295
        }
296
        return StringUtils.join(itemIds, '_');
297
    }
298
 
299
 
300
 
301
	public static String getRechargeProviderName(long providerId) {
302
            return providersMap.get(providerId);
303
	}
304
 
305
    public static Map<Long, String> getMobileProvidersMap() {
306
        return mobileProvidersMap;
307
    }
308
 
309
    public static Map<Long, String> getDthProvidersMap() {
310
        return dthProvidersMap;
311
    }
312
 
313
    public static Map<Long, List<RechargePlan>> getOperatorPlanMap() {
314
        return operatorPlanMap;
315
    }
316
 
317
    public static Map<String, Map<String, String>> getUriOperatorMap(){
318
    	return uriOperatorMap;
319
	}
320
 
321
    public static Map<String, String> getOperatorByUri(String uri){
322
    	return uriOperatorMap.get(uri);
323
	}
12620 amit.gupta 324
 
325
 
326
 
327
	public static Map<Long, ExclusiveAffiliateItemInfo> getExclusiveAffiliateItems(){
328
		try{
12626 amit.gupta 329
			Long currentTime = new Date().getTime();
12620 amit.gupta 330
			CatalogClient client = new CatalogClient();
16541 amit.gupta 331
			if(exclusiveAffiliateItems == null || thirtyMinsCounter < currentTime) {
12620 amit.gupta 332
				exclusiveAffiliateItems = client.getClient().getExAffiliateItemInfo();
16541 amit.gupta 333
				thirtyMinsCounter = new Date().getTime() + 15*60000;
12620 amit.gupta 334
			}
335
		}catch(Exception ce){
336
			logger.error("Unable to fetch exclusive items from catalog", ce);
337
		}
338
		return exclusiveAffiliateItems;
339
	}
16541 amit.gupta 340
 
341
	public static String getDtrAffiliateString(Long entityId){
342
		HttpClient httpclient = new DefaultHttpClient();
343
 
344
		String url = String.format(DTR_ENTITY_URL, entityId);
345
		HttpGet httpget = new HttpGet(url);
16568 amit.gupta 346
		String str = "{}";
16541 amit.gupta 347
		// If the response does not enclose an entity, there is no need
348
	  // to worry about connection release
349
		try{
350
			HttpResponse response = httpclient.execute(httpget);
351
			// Get hold of the response entity
352
			HttpEntity entity = response.getEntity();
353
			if (entity != null) {
354
				InputStream instream = entity.getContent();
355
				try {   
356
					BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
357
					str = reader.readLine();
358
				} catch (IOException ex) {
359
					throw ex;
360
				} catch (RuntimeException ex) {	  
361
		          httpget.abort();
362
		          throw ex;
363
 
364
				} finally {
365
		          // Closing the input stream will trigger connection release
366
		          instream.close();
367
				}
368
			}
369
		} catch (Exception e){
370
			e.printStackTrace();
371
		}
372
		httpclient.getConnectionManager().shutdown();
16570 amit.gupta 373
		if (str== null){
374
			str = "{}";
375
		}
16541 amit.gupta 376
		return str;
377
	}
9103 anupam.sin 378
}