Subversion Repositories SmartDukaan

Rev

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