Subversion Repositories SmartDukaan

Rev

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