Subversion Repositories SmartDukaan

Rev

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