Subversion Repositories SmartDukaan

Rev

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