Subversion Repositories SmartDukaan

Rev

Rev 22391 | Rev 22757 | 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
 
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/";
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);
58
	private static Map<Long, String> mobileProvidersMap;
59
	private static Map<Long, String> dthProvidersMap;
60
	private static final String SMS_GATEWAY = "http://103.15.179.45:8085/SMSGateway/sendingSMS";
61
	private static Map<Long, String> allProviders;
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);
183
		generalSearchUrl.addParameter("ani", "91"+mobileNumber);
184
		generalSearchUrl.addParameter("uname", "srlsaholic");                  
185
		generalSearchUrl.addParameter("passwd", "sr18mar"); 					  
186
		generalSearchUrl.addParameter("cli", "PROFTM");             			  
187
		generalSearchUrl.setParameter("message", text);
188
		URL url = generalSearchUrl.build().toURL();
189
		InputStream is = url.openStream();
190
		String response;
191
		try{
192
			response = IOUtils.toString(is, "UTF-8");
193
			System.out.println("response sms gateway "+response);
194
		}
195
		finally{
196
			is.close();
197
		}
198
	}
199
 
200
	public static String[] getOrderStatus(RechargeOrderStatus status){
201
		if (status == null){
202
			status = RechargeOrderStatus.INIT;
203
		}
204
		if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
205
			return new String[]{"false", "PAYMENT FAILED", "Payment failed at the payment gateway."};
206
		}
207
		else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
208
			if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)){
209
				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."
210
						+ " We have put your recharge under process."
211
						+ " As soon as we get a confirmation on this transaction, we will notify you."};
212
			}
213
			else{
214
				return new String[]{"false", "RECHARGE IN PROCESS", "Your Payment is successful.We have put your recharge under process." +
215
				" Please wait while we check with the operator."};
216
			}
217
		}
218
		else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
22391 amit.gupta 219
			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 220
					+ " The entire Amount has been refunded to your wallet."};
221
		} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
222
			return new String[] {"false", "SUCCESS", "Congratulations! Your device is successfully recharged."};
223
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
224
			return new String[]{"false", "PAYMENT REFUNDED","The payment associated with this recharge order has been refunded."};
225
		} else {
226
			return new String[]{"true", "ERROR", "INVALID INPUT"};
227
		}
228
	}
21986 kshitij.so 229
 
230
 
231
	public static String getIconUrl(int entityId,String host, int port, String webapp){
232
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
233
		Map<String, String> params = new HashMap<String, String>();
234
		String response = null;
235
		String uri = webapp+"/entity/"+entityId;
22215 ashik.ali 236
		Map<String, String> headers = new HashMap<>(1);
237
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
21986 kshitij.so 238
		try {
22215 ashik.ali 239
			response = rc.get(uri, params, headers);
21986 kshitij.so 240
		} catch (ProfitMandiBusinessException e) {
241
			// TODO Auto-generated catch block
242
			e.printStackTrace();
243
			return "";
244
		}
245
		JSONObject obj = new JSONObject(response);
246
		String iconUrl = null;
247
		try{
248
		iconUrl = obj.getJSONObject("entity").getString("iconImageUrl");
249
		}
250
		catch(Exception e){
251
			return "";
252
		}
253
		return iconUrl==null?"":iconUrl;
254
	}
21894 ashik.ali 255
 
21915 ashik.ali 256
	public static Map<String, GstRate> getInterGstRates(){
257
		try{
258
			CatalogService.Client catalogClient = getCatalogClient();
259
			return toGstRateMap(catalogClient.getInterStateGstRates());
260
		}catch (Exception e) {
261
			e.printStackTrace();
262
			return new HashMap<>();
263
		}
264
	}
265
 
21894 ashik.ali 266
	public static Map<String, GstRate> getGstRates(String stateName){
267
		try{
21915 ashik.ali 268
			CatalogService.Client catalogClient = getCatalogClient();
269
			long stateId = getStateId(stateName);
270
			StateGstRates stateGstRates = catalogClient.getGstRatesByState(stateId);
271
			return toGstRateMap(stateGstRates.getGstRatesMap());
21894 ashik.ali 272
		}catch(Exception e){
21915 ashik.ali 273
			e.printStackTrace();
22139 amit.gupta 274
			return new HashMap<>();	
21894 ashik.ali 275
		}
276
	}
277
 
21915 ashik.ali 278
	private static CatalogService.Client getCatalogClient() throws Exception{
279
		try{
280
			CatalogClient client = new CatalogClient();
281
			CatalogService.Client catalogClient = client.getClient();
282
			return catalogClient;
283
		}catch (Exception e) {
284
			throw e;
285
		}
286
	}
287
 
288
	private static Map<String, GstRate> toGstRateMap(Map<String, in.shop2020.model.v1.catalog.GstRate> hsnCodeGstRateMap){
289
		Map<String, GstRate> gstRateMap = new HashMap<>();
290
		for(Map.Entry<String, in.shop2020.model.v1.catalog.GstRate> entry : hsnCodeGstRateMap.entrySet()){
291
			in.shop2020.model.v1.catalog.GstRate tGstRate = entry.getValue();
292
			GstRate gstRate = new GstRate();
293
			gstRate.setHsnCode(entry.getKey());
294
			gstRate.setIgstRate(Double.valueOf(tGstRate.getIgstRate()).floatValue());
295
			gstRate.setCgstRate(Double.valueOf(tGstRate.getCgstRate()).floatValue());
296
			gstRate.setSgstRate(Double.valueOf(tGstRate.getSgstRate()).floatValue());
297
			gstRateMap.put(entry.getKey(), gstRate);
298
		}
299
		return gstRateMap;
300
	}
301
 
302
 
21902 ashik.ali 303
	public static String getStateCode(String stateName) throws Exception{
304
		return getStateInfo(stateName).getStateCode();
305
	}
306
 
307
	public static long getStateId(String stateName) throws Exception{
308
		return getStateInfo(stateName).getId();
309
	}
310
 
311
	public static StateInfo getStateInfo(String stateName) throws Exception{
21894 ashik.ali 312
		try{
313
			InventoryClient client = new InventoryClient();
314
			InventoryService.Client inventoryClient = client.getClient();
315
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 316
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 317
			logger.info("State Name: {}", stateName);
21894 ashik.ali 318
			for(StateInfo stateInfo : stateInfos){
22664 amit.gupta 319
				logger.info("State Name from service: {}", stateInfo.getStateName());
21894 ashik.ali 320
				if(stateName.equals(stateInfo.getStateName())){
21902 ashik.ali 321
					return stateInfo;
21894 ashik.ali 322
				}
323
			}
21902 ashik.ali 324
			throw new Exception("Not found");
21894 ashik.ali 325
		}catch (Exception e) {
326
			e.printStackTrace();
327
			throw e;
328
		}
329
	}
22351 ashik.ali 330
 
331
	public static User createSaholicUser(String emailId) throws ProfitMandiBusinessException{
332
		in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();
333
		user.setEmail(emailId);
334
		user.setPassword("");
335
		user.setCommunicationEmail(emailId);
336
		user.setSex(Sex.WONT_SAY);
337
 
338
		try {
339
			UserClient userContextServiceClient = new UserClient();
340
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
341
			user = userClient.createUser(user);
342
			return user;
343
		}catch (UserContextException ux){
344
			logger.error("Unable to register user: " + ux.getMessage());
345
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
346
		} catch (TTransportException e) {
347
			logger.error("Unable to register user." + e);
348
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
349
		}catch (TException e) {
350
			logger.error("Unable to register user." + e);
351
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
352
		} 
353
	}
21543 ashik.ali 354
}