Subversion Repositories SmartDukaan

Rev

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