Subversion Repositories SmartDukaan

Rev

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