Subversion Repositories SmartDukaan

Rev

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