Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21543 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
3
import java.io.File;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.net.URISyntaxException;
7
import java.net.URL;
21902 ashik.ali 8
import java.util.ArrayList;
21543 ashik.ali 9
import java.util.Collections;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
23296 ashik.ali 13
import java.util.Set;
14
import java.util.TreeSet;
21894 ashik.ali 15
 
21543 ashik.ali 16
import org.apache.commons.io.FileUtils;
17
import org.apache.commons.io.IOUtils;
18
import org.apache.http.client.utils.URIBuilder;
22351 ashik.ali 19
import org.apache.thrift.TException;
20
import org.apache.thrift.transport.TTransportException;
21986 kshitij.so 21
import org.json.JSONObject;
21543 ashik.ali 22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
22215 ashik.ali 24
import org.springframework.http.HttpHeaders;
25
import org.springframework.http.MediaType;
23074 ashik.ali 26
 
22215 ashik.ali 27
import com.spice.profitmandi.common.enumuration.SchemeType;
28
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21894 ashik.ali 29
import com.spice.profitmandi.common.model.GstRate;
22351 ashik.ali 30
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22215 ashik.ali 31
import com.spice.profitmandi.common.web.client.RestClient;
21894 ashik.ali 32
import com.spice.profitmandi.thrift.clients.CatalogClient;
33
import com.spice.profitmandi.thrift.clients.InventoryClient;
34
import com.spice.profitmandi.thrift.clients.TransactionClient;
22351 ashik.ali 35
import com.spice.profitmandi.thrift.clients.UserClient;
21894 ashik.ali 36
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
21543 ashik.ali 37
 
21894 ashik.ali 38
import in.shop2020.model.v1.catalog.CatalogService;
39
import in.shop2020.model.v1.catalog.StateGstRates;
40
import in.shop2020.model.v1.inventory.InventoryService;
41
import in.shop2020.model.v1.inventory.StateInfo;
42
import in.shop2020.model.v1.order.RechargeOrderStatus;
43
import in.shop2020.model.v1.order.RechargePlan;
44
import in.shop2020.model.v1.order.RechargeType;
22351 ashik.ali 45
import in.shop2020.model.v1.user.Sex;
46
import in.shop2020.model.v1.user.User;
47
import in.shop2020.model.v1.user.UserContextException;
21543 ashik.ali 48
 
21894 ashik.ali 49
 
50
 
21543 ashik.ali 51
public class Utils {
21986 kshitij.so 52
 
53
	private static final Logger logger = LoggerFactory.getLogger(Utils.class);
54
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
55
	public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
56
	public static final String DOCUMENT_STORE = "/profitmandi/documents/";
23017 ashik.ali 57
	private static final Map<Integer, String> helpMap = new HashMap<>(6);
58
	private static final Map<Integer, String> dthIdAliasMap = new HashMap<>(7);
59
	private static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<>(20);
21986 kshitij.so 60
	private static Map<Long, String> mobileProvidersMap;
61
	private static Map<Long, String> dthProvidersMap;
22757 amit.gupta 62
	//private static final String SMS_GATEWAY = "http://103.15.179.45:8085/SMSGateway/sendingSMS";
63
	private static final String SMS_GATEWAY = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS";
21986 kshitij.so 64
	private static Map<Long, String> allProviders;
23017 ashik.ali 65
	public static Map<RechargeOrderStatus, String> rechargeStatusMap = new HashMap<>();
21646 kshitij.so 66
 
21986 kshitij.so 67
	static {
68
		helpMap.put(Integer.valueOf(1), "Your VC number starts with 0 and is 11 digits long.");
69
		helpMap.put(Integer.valueOf(2), "Smart card number starts with 2 and is 12 digits long.");
70
		helpMap.put(Integer.valueOf(3), "Smart card number starts with 4 and is 11 digits long.");
71
		helpMap.put(Integer.valueOf(4), "Subscriber ID starts with 1 and is 10 digits long.");
72
		helpMap.put(Integer.valueOf(5), "For customer ID, SMS ID to 9212012299 from your registered mobile no.");
73
		helpMap.put(Integer.valueOf(26), "Customer ID starts with 3 and is 10 digits long.");
21543 ashik.ali 74
 
21986 kshitij.so 75
		dthIdAliasMap.put(Integer.valueOf(1), "VC Number :");
76
		dthIdAliasMap.put(Integer.valueOf(2), "Smart Card Number :");
77
		dthIdAliasMap.put(Integer.valueOf(3), "Smart Card Number :");
78
		dthIdAliasMap.put(Integer.valueOf(4), "Subscriber Id :");
79
		dthIdAliasMap.put(Integer.valueOf(5), "Customer Id :");
80
		dthIdAliasMap.put(Integer.valueOf(0), "Account Number :");
81
		dthIdAliasMap.put(Integer.valueOf(26), "Customer Id :");
21543 ashik.ali 82
 
21986 kshitij.so 83
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
84
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
85
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
86
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed Refunded");
87
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
88
		rechargeStatusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
89
		rechargeStatusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
90
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_PENDING, "Payment Failed");
91
		rechargeStatusMap.put(RechargeOrderStatus.INIT, "Payment Initiated");
92
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_UNKNOWN, "Recharge In Process");
93
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_IN_PROCESS, "Recharge In Process");
94
 
95
		TransactionClient tcl;
96
		try
97
		{
98
			tcl = new TransactionClient();
99
			mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
100
			dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
101
			logger.info("mobileProvidersMap" + mobileProvidersMap);
102
			logger.info("dthProvidersMap" + dthProvidersMap);
22390 amit.gupta 103
			allProviders = new HashMap<>(mobileProvidersMap);
21986 kshitij.so 104
			logger.info("allProviders" + allProviders);
105
			allProviders.putAll(dthProvidersMap);
106
			logger.info("allProviders" + allProviders);
107
 
108
			for (Long operatorId : mobileProvidersMap.keySet()) {
109
				List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId.longValue());
110
				if (!plans.isEmpty())
111
					operatorPlanMap.put(operatorId, plans);
112
			}
113
		} catch (Exception e) {
114
			logger.error("Could not get providers", e);
115
		}
116
	}
117
 
118
 
119
 
21543 ashik.ali 120
	@SuppressWarnings("serial")
121
	public static final Map<String,String> MIME_TYPE = Collections.unmodifiableMap(
122
			new HashMap<String, String>(){
123
				{
124
					put("image/png", "png");
125
					put("image/jpeg", "jpeg");
126
					put("image/pjpeg", "jpeg");
127
					put("application/pdf","pdf");
128
				}
129
			});
130
 
131
	private static String getExportPath(){
132
		String exportPath=null;
133
 
134
		ConfigClient client = ConfigClient.getClient();
135
		try{
136
			exportPath = client.get("export_entities_path");
137
		}catch(Exception ce){
138
			logger.error("Unable to read export path from the config client: ", ce);
139
			logger.warn("Setting the default export path");
140
			exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
141
		}
142
		return exportPath;
143
	}
21986 kshitij.so 144
 
21646 kshitij.so 145
	public static String getRechargeDisplayStatus(RechargeOrderStatus status) {
21986 kshitij.so 146
		if (status == null) {
147
			status = RechargeOrderStatus.INIT;
148
		}
149
		String displayStatus = (String)rechargeStatusMap.get(status);
150
		if (displayStatus == null) {
151
			return "";
152
		}
153
		return displayStatus;
154
	}
21543 ashik.ali 155
 
156
	public static boolean copyDocument(String documentPath){
157
		File source = new File(documentPath);
158
		File dest = new File(DOCUMENT_STORE+source.getName());
159
		try {
160
			FileUtils.copyFile(source, dest);
161
		} catch (IOException e) {
162
			e.printStackTrace();
163
			return false;
164
		}
165
		return true;
166
	}
167
 
168
	public static Map<Long, String> getMobileProvidersMap() {
169
		return mobileProvidersMap;
170
	}
171
 
172
	public static Map<Long, String> getDthProvidersMap() {
173
		return dthProvidersMap;
174
	}
21986 kshitij.so 175
 
21543 ashik.ali 176
	public static Map<Long, String> getAllProviders() {
177
		return allProviders;
178
	}
21986 kshitij.so 179
 
21543 ashik.ali 180
	public static String getProvider(long operatorId) {
21986 kshitij.so 181
		return allProviders.get(operatorId);
182
	}
21543 ashik.ali 183
 
184
	public static void sendSms(String text, String mobileNumber) throws URISyntaxException, IOException{
185
		URIBuilder generalSearchUrl = new URIBuilder(SMS_GATEWAY);
22757 amit.gupta 186
		generalSearchUrl.addParameter("Mobile", "91"+mobileNumber);
187
		generalSearchUrl.addParameter("Username", "srlsaholic");                  
188
		generalSearchUrl.addParameter("Password", "sr18mar"); 					  
189
		generalSearchUrl.addParameter("SenderID", "PROFTM");             			  
190
		generalSearchUrl.setParameter("Message", text);
191
		generalSearchUrl.setParameter("MessageType", "txt");
192
/*		generalSearchUrl.addParameter("ani", "91"+mobileNumber);
21543 ashik.ali 193
		generalSearchUrl.addParameter("uname", "srlsaholic");                  
194
		generalSearchUrl.addParameter("passwd", "sr18mar"); 					  
195
		generalSearchUrl.addParameter("cli", "PROFTM");             			  
196
		generalSearchUrl.setParameter("message", text);
22757 amit.gupta 197
*/		URL url = generalSearchUrl.build().toURL();
22889 amit.gupta 198
		System.out.println(url);
21543 ashik.ali 199
		InputStream is = url.openStream();
200
		String response;
201
		try{
202
			response = IOUtils.toString(is, "UTF-8");
203
			System.out.println("response sms gateway "+response);
204
		}
205
		finally{
206
			is.close();
207
		}
208
	}
22757 amit.gupta 209
 
210
	public static void sendMailWithAttachments(String email, String message, List<File> attachments){
211
 
212
	}
213
 
21543 ashik.ali 214
	public static String[] getOrderStatus(RechargeOrderStatus status){
215
		if (status == null){
216
			status = RechargeOrderStatus.INIT;
217
		}
218
		if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
219
			return new String[]{"false", "PAYMENT FAILED", "Payment failed at the payment gateway."};
220
		}
221
		else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
222
			if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)){
223
				return new String[]{"false", "PAYMENT SUCCESSFUL", "Your Payment was successful but due to some internal error with the operator's system we are not sure if the recharge was successful."
224
						+ " We have put your recharge under process."
225
						+ " As soon as we get a confirmation on this transaction, we will notify you."};
226
			}
227
			else{
228
				return new String[]{"false", "RECHARGE IN PROCESS", "Your Payment is successful.We have put your recharge under process." +
229
				" Please wait while we check with the operator."};
230
			}
231
		}
232
		else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
22391 amit.gupta 233
			return new String[]{"false", "RECHARGE FAILED", "Your Payment was successful but unfortunately the recharge failed.Don't worry your payment is safe with us."
21543 ashik.ali 234
					+ " The entire Amount has been refunded to your wallet."};
235
		} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
236
			return new String[] {"false", "SUCCESS", "Congratulations! Your device is successfully recharged."};
237
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
238
			return new String[]{"false", "PAYMENT REFUNDED","The payment associated with this recharge order has been refunded."};
239
		} else {
240
			return new String[]{"true", "ERROR", "INVALID INPUT"};
241
		}
242
	}
21986 kshitij.so 243
 
244
 
245
	public static String getIconUrl(int entityId,String host, int port, String webapp){
246
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
247
		Map<String, String> params = new HashMap<String, String>();
248
		String response = null;
249
		String uri = webapp+"/entity/"+entityId;
22215 ashik.ali 250
		Map<String, String> headers = new HashMap<>(1);
251
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
21986 kshitij.so 252
		try {
22215 ashik.ali 253
			response = rc.get(uri, params, headers);
21986 kshitij.so 254
		} catch (ProfitMandiBusinessException e) {
255
			// TODO Auto-generated catch block
256
			e.printStackTrace();
257
			return "";
258
		}
259
		JSONObject obj = new JSONObject(response);
260
		String iconUrl = null;
261
		try{
262
		iconUrl = obj.getJSONObject("entity").getString("iconImageUrl");
263
		}
264
		catch(Exception e){
265
			return "";
266
		}
267
		return iconUrl==null?"":iconUrl;
268
	}
21894 ashik.ali 269
 
21915 ashik.ali 270
	public static Map<String, GstRate> getInterGstRates(){
271
		try{
272
			CatalogService.Client catalogClient = getCatalogClient();
273
			return toGstRateMap(catalogClient.getInterStateGstRates());
274
		}catch (Exception e) {
275
			e.printStackTrace();
276
			return new HashMap<>();
277
		}
278
	}
279
 
23147 ashik.ali 280
	public static Map<Integer, GstRate> getStateTaxRate(List<Integer> itemIds, int stateId) throws ProfitMandiBusinessException{
23152 ashik.ali 281
		List<Long> longItemIds = new ArrayList<>();
282
		for(int itemId : itemIds){
283
			longItemIds.add(Long.valueOf(itemId));
284
		}
23147 ashik.ali 285
		try{
286
			CatalogService.Client catalogClient = getCatalogClient();
23152 ashik.ali 287
			Map<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateMap = catalogClient.getStateTaxRate(longItemIds, stateId);
23147 ashik.ali 288
			Map<Integer, GstRate> itemIdGstRateMap = new HashMap<>();
23152 ashik.ali 289
			for(Map.Entry<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateEntry : totalTaxRateMap.entrySet()){
23147 ashik.ali 290
				GstRate gstRate = new GstRate();
291
				gstRate.setIgstRate(Double.valueOf(totalTaxRateEntry.getValue().getIgstRate()).floatValue());
292
				gstRate.setCgstRate(Double.valueOf(totalTaxRateEntry.getValue().getCgstRate()).floatValue());
293
				gstRate.setSgstRate(Double.valueOf(totalTaxRateEntry.getValue().getSgstRate()).floatValue());
23152 ashik.ali 294
				itemIdGstRateMap.put(totalTaxRateEntry.getKey().intValue(), gstRate);
23147 ashik.ali 295
			}
296
			return itemIdGstRateMap;
297
		}catch(Exception e){
298
			e.printStackTrace();
299
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID + ", " + ProfitMandiConstants.STATE_ID, itemIds +", " + stateId, "");
300
		}
301
	}
302
 
303
	public static Map<Integer, Float> getIgstTaxRate(List<Integer> itemIds) throws ProfitMandiBusinessException{
23152 ashik.ali 304
		List<Long> longItemIds = new ArrayList<>();
305
		for(int itemId : itemIds){
306
			longItemIds.add(Long.valueOf(itemId));
307
		}
23147 ashik.ali 308
		try{
309
			CatalogService.Client catalogClient = getCatalogClient();
23152 ashik.ali 310
			Map<Long, Double> totalTaxRateMap = catalogClient.getIgstTaxRate(longItemIds);
23147 ashik.ali 311
			Map<Integer, Float> itemIdIgstRateMap = new HashMap<>();
23152 ashik.ali 312
			for(Map.Entry<Long, Double> totalTaxRateEntry : totalTaxRateMap.entrySet()){
313
				itemIdIgstRateMap.put(totalTaxRateEntry.getKey().intValue(), Double.valueOf(totalTaxRateEntry.getValue()).floatValue());
23147 ashik.ali 314
			}
315
			return itemIdIgstRateMap;
316
		}catch(Exception e){
317
			e.printStackTrace();
318
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, itemIds, "");
319
		}
320
	}
321
 
21894 ashik.ali 322
	public static Map<String, GstRate> getGstRates(String stateName){
323
		try{
21915 ashik.ali 324
			CatalogService.Client catalogClient = getCatalogClient();
325
			long stateId = getStateId(stateName);
326
			StateGstRates stateGstRates = catalogClient.getGstRatesByState(stateId);
327
			return toGstRateMap(stateGstRates.getGstRatesMap());
21894 ashik.ali 328
		}catch(Exception e){
21915 ashik.ali 329
			e.printStackTrace();
22139 amit.gupta 330
			return new HashMap<>();	
21894 ashik.ali 331
		}
332
	}
333
 
21915 ashik.ali 334
	private static CatalogService.Client getCatalogClient() throws Exception{
335
		try{
336
			CatalogClient client = new CatalogClient();
337
			CatalogService.Client catalogClient = client.getClient();
338
			return catalogClient;
339
		}catch (Exception e) {
340
			throw e;
341
		}
342
	}
343
 
344
	private static Map<String, GstRate> toGstRateMap(Map<String, in.shop2020.model.v1.catalog.GstRate> hsnCodeGstRateMap){
345
		Map<String, GstRate> gstRateMap = new HashMap<>();
346
		for(Map.Entry<String, in.shop2020.model.v1.catalog.GstRate> entry : hsnCodeGstRateMap.entrySet()){
347
			in.shop2020.model.v1.catalog.GstRate tGstRate = entry.getValue();
348
			GstRate gstRate = new GstRate();
349
			gstRate.setHsnCode(entry.getKey());
350
			gstRate.setIgstRate(Double.valueOf(tGstRate.getIgstRate()).floatValue());
351
			gstRate.setCgstRate(Double.valueOf(tGstRate.getCgstRate()).floatValue());
352
			gstRate.setSgstRate(Double.valueOf(tGstRate.getSgstRate()).floatValue());
353
			gstRateMap.put(entry.getKey(), gstRate);
354
		}
355
		return gstRateMap;
356
	}
357
 
358
 
21902 ashik.ali 359
	public static String getStateCode(String stateName) throws Exception{
360
		return getStateInfo(stateName).getStateCode();
361
	}
362
 
363
	public static long getStateId(String stateName) throws Exception{
364
		return getStateInfo(stateName).getId();
365
	}
366
 
367
	public static StateInfo getStateInfo(String stateName) throws Exception{
21894 ashik.ali 368
		try{
369
			InventoryClient client = new InventoryClient();
370
			InventoryService.Client inventoryClient = client.getClient();
371
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 372
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 373
			logger.info("State Name: {}", stateName);
21894 ashik.ali 374
			for(StateInfo stateInfo : stateInfos){
22664 amit.gupta 375
				logger.info("State Name from service: {}", stateInfo.getStateName());
22938 amit.gupta 376
				if(stateName.toUpperCase().equals(stateInfo.getStateName().toUpperCase())){
21902 ashik.ali 377
					return stateInfo;
21894 ashik.ali 378
				}
379
			}
21902 ashik.ali 380
			throw new Exception("Not found");
21894 ashik.ali 381
		}catch (Exception e) {
382
			e.printStackTrace();
383
			throw e;
384
		}
385
	}
22351 ashik.ali 386
 
23296 ashik.ali 387
	public static List<String> getAllStateNames(){
388
		List<String> sortedStateNames = new ArrayList<>();
389
		try{
390
			InventoryClient client = new InventoryClient();
391
			InventoryService.Client inventoryClient = client.getClient();
392
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
393
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
394
			Set<String> stateNames = new TreeSet<>();
395
 
396
			for(StateInfo stateInfo : stateInfos){
397
				stateNames.add(stateInfo.getStateName());
398
			}
399
			sortedStateNames = new ArrayList<>(stateNames);
400
			return sortedStateNames;
401
		}catch (Exception e) {
402
			e.printStackTrace();
403
			return sortedStateNames;
404
		}
405
	}
406
 
22351 ashik.ali 407
	public static User createSaholicUser(String emailId) throws ProfitMandiBusinessException{
408
		in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();
409
		user.setEmail(emailId);
410
		user.setPassword("");
411
		user.setCommunicationEmail(emailId);
412
		user.setSex(Sex.WONT_SAY);
413
 
414
		try {
415
			UserClient userContextServiceClient = new UserClient();
416
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
417
			user = userClient.createUser(user);
418
			return user;
419
		}catch (UserContextException ux){
420
			logger.error("Unable to register user: " + ux.getMessage());
421
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
422
		} catch (TTransportException e) {
423
			logger.error("Unable to register user." + e);
424
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
425
		}catch (TException e) {
426
			logger.error("Unable to register user." + e);
427
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
428
		} 
429
	}
22757 amit.gupta 430
 
431
	public static void main (String[] args) throws Exception{
432
		Utils.sendSms("Hello", "9990381569");
433
	}
23074 ashik.ali 434
 
435
 
21543 ashik.ali 436
}