Subversion Repositories SmartDukaan

Rev

Rev 21986 | Rev 22215 | 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
 
21986 kshitij.so 3
import com.spice.profitmandi.common.enumuration.SchemeType;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.web.client.RestClient;
6
import com.spice.profitmandi.thrift.clients.TransactionClient;
7
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
8
 
9
import in.shop2020.model.v1.order.RechargeOrderStatus;
10
import in.shop2020.model.v1.order.RechargePlan;
11
import in.shop2020.model.v1.order.RechargeType;
12
import in.shop2020.model.v1.order.TransactionService.Client;
21543 ashik.ali 13
import java.io.File;
14
import java.io.IOException;
15
import java.io.InputStream;
21986 kshitij.so 16
import java.io.PrintStream;
17
import java.net.URI;
21543 ashik.ali 18
import java.net.URISyntaxException;
19
import java.net.URL;
21902 ashik.ali 20
import java.util.ArrayList;
21543 ashik.ali 21
import java.util.Collections;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
21986 kshitij.so 25
import java.util.Set;
21894 ashik.ali 26
 
21543 ashik.ali 27
import org.apache.commons.io.FileUtils;
28
import org.apache.commons.io.IOUtils;
29
import org.apache.http.client.utils.URIBuilder;
21986 kshitij.so 30
import org.json.JSONObject;
21543 ashik.ali 31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
21986 kshitij.so 33
import org.springframework.beans.factory.annotation.Value;
21543 ashik.ali 34
 
21894 ashik.ali 35
import com.spice.profitmandi.common.model.GstRate;
36
import com.spice.profitmandi.thrift.clients.CatalogClient;
37
import com.spice.profitmandi.thrift.clients.InventoryClient;
38
import com.spice.profitmandi.thrift.clients.TransactionClient;
39
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
21543 ashik.ali 40
 
21894 ashik.ali 41
import in.shop2020.model.v1.catalog.CatalogService;
42
import in.shop2020.model.v1.catalog.StateGstRates;
43
import in.shop2020.model.v1.inventory.InventoryService;
44
import in.shop2020.model.v1.inventory.StateInfo;
45
import in.shop2020.model.v1.order.RechargeOrderStatus;
46
import in.shop2020.model.v1.order.RechargePlan;
47
import in.shop2020.model.v1.order.RechargeType;
21543 ashik.ali 48
 
21894 ashik.ali 49
 
50
 
21543 ashik.ali 51
public class Utils {
21986 kshitij.so 52
 
53
	private static final Logger logger = LoggerFactory.getLogger(Utils.class);
54
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
55
	public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
56
	public static final String DOCUMENT_STORE = "/profitmandi/documents/";
57
	private static final Map<Integer, String> helpMap = new HashMap(6);
58
	private static final Map<Integer, String> dthIdAliasMap = new HashMap(7);
59
	private static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap(20);
60
	private static Map<Long, String> mobileProvidersMap;
61
	private static Map<Long, String> dthProvidersMap;
62
	private static final String SMS_GATEWAY = "http://103.15.179.45:8085/SMSGateway/sendingSMS";
63
	private static Map<Long, String> allProviders;
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);
102
			allProviders = mobileProvidersMap;
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);
185
		generalSearchUrl.addParameter("ani", "91"+mobileNumber);
186
		generalSearchUrl.addParameter("uname", "srlsaholic");                  
187
		generalSearchUrl.addParameter("passwd", "sr18mar"); 					  
188
		generalSearchUrl.addParameter("cli", "PROFTM");             			  
189
		generalSearchUrl.setParameter("message", text);
190
		URL url = generalSearchUrl.build().toURL();
191
		InputStream is = url.openStream();
192
		String response;
193
		try{
194
			response = IOUtils.toString(is, "UTF-8");
195
			System.out.println("response sms gateway "+response);
196
		}
197
		finally{
198
			is.close();
199
		}
200
	}
201
 
202
	public static String[] getOrderStatus(RechargeOrderStatus status){
203
		if (status == null){
204
			status = RechargeOrderStatus.INIT;
205
		}
206
		if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
207
			return new String[]{"false", "PAYMENT FAILED", "Payment failed at the payment gateway."};
208
		}
209
		else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
210
			if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)){
211
				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."
212
						+ " We have put your recharge under process."
213
						+ " As soon as we get a confirmation on this transaction, we will notify you."};
214
			}
215
			else{
216
				return new String[]{"false", "RECHARGE IN PROCESS", "Your Payment is successful.We have put your recharge under process." +
217
				" Please wait while we check with the operator."};
218
			}
219
		}
220
		else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
221
			return new String[]{"false", "RECHARGE IN PROCESS", "Your Payment was successful but unfortunately the recharge failed.Don't worry your payment is safe with us."
222
					+ " The entire Amount has been refunded to your wallet."};
223
		} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
224
			return new String[] {"false", "SUCCESS", "Congratulations! Your device is successfully recharged."};
225
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
226
			return new String[]{"false", "PAYMENT REFUNDED","The payment associated with this recharge order has been refunded."};
227
		} else {
228
			return new String[]{"true", "ERROR", "INVALID INPUT"};
229
		}
230
	}
21986 kshitij.so 231
 
232
 
233
	public static String getIconUrl(int entityId,String host, int port, String webapp){
234
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
235
		Map<String, String> params = new HashMap<String, String>();
236
		String response = null;
237
		String uri = webapp+"/entity/"+entityId;
238
		try {
239
			response = rc.get(uri, params);
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());
21894 ashik.ali 317
			for(StateInfo stateInfo : stateInfos){
318
				if(stateName.equals(stateInfo.getStateName())){
21902 ashik.ali 319
					return stateInfo;
21894 ashik.ali 320
				}
321
			}
21902 ashik.ali 322
			throw new Exception("Not found");
21894 ashik.ali 323
		}catch (Exception e) {
324
			e.printStackTrace();
325
			throw e;
326
		}
327
	}
21543 ashik.ali 328
}