Subversion Repositories SmartDukaan

Rev

Rev 23912 | Rev 24256 | 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;
23509 amit.gupta 11
import java.util.HashSet;
21543 ashik.ali 12
import java.util.List;
13
import java.util.Map;
23296 ashik.ali 14
import java.util.Set;
15
import java.util.TreeSet;
21894 ashik.ali 16
 
23909 amit.gupta 17
import javax.mail.internet.InternetAddress;
18
import javax.mail.internet.MimeMessage;
19
 
21543 ashik.ali 20
import org.apache.commons.io.FileUtils;
21
import org.apache.commons.io.IOUtils;
22
import org.apache.http.client.utils.URIBuilder;
23929 amit.gupta 23
import org.apache.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
22351 ashik.ali 25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
21986 kshitij.so 27
import org.json.JSONObject;
23929 amit.gupta 28
import org.springframework.core.io.InputStreamSource;
22215 ashik.ali 29
import org.springframework.http.HttpHeaders;
30
import org.springframework.http.MediaType;
23909 amit.gupta 31
import org.springframework.mail.javamail.JavaMailSender;
32
import org.springframework.mail.javamail.MimeMessageHelper;
23074 ashik.ali 33
 
22215 ashik.ali 34
import com.spice.profitmandi.common.enumuration.SchemeType;
35
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21894 ashik.ali 36
import com.spice.profitmandi.common.model.GstRate;
22351 ashik.ali 37
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22215 ashik.ali 38
import com.spice.profitmandi.common.web.client.RestClient;
21894 ashik.ali 39
import com.spice.profitmandi.thrift.clients.CatalogClient;
40
import com.spice.profitmandi.thrift.clients.InventoryClient;
41
import com.spice.profitmandi.thrift.clients.TransactionClient;
22351 ashik.ali 42
import com.spice.profitmandi.thrift.clients.UserClient;
23509 amit.gupta 43
import com.spice.profitmandi.thrift.clients.WarehouseClient;
21894 ashik.ali 44
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
21543 ashik.ali 45
 
21894 ashik.ali 46
import in.shop2020.model.v1.catalog.CatalogService;
47
import in.shop2020.model.v1.inventory.InventoryService;
48
import in.shop2020.model.v1.inventory.StateInfo;
23509 amit.gupta 49
import in.shop2020.model.v1.inventory.Warehouse;
23884 amit.gupta 50
import in.shop2020.model.v1.order.OrderStatusGroups;
21894 ashik.ali 51
import in.shop2020.model.v1.order.RechargeOrderStatus;
52
import in.shop2020.model.v1.order.RechargePlan;
53
import in.shop2020.model.v1.order.RechargeType;
23509 amit.gupta 54
import in.shop2020.model.v1.order.SellerInfo;
55
import in.shop2020.model.v1.order.WarehouseAddress;
22351 ashik.ali 56
import in.shop2020.model.v1.user.Sex;
57
import in.shop2020.model.v1.user.User;
58
import in.shop2020.model.v1.user.UserContextException;
23509 amit.gupta 59
import in.shop2020.warehouse.InventoryItem;
60
import in.shop2020.warehouse.WarehouseService;
21543 ashik.ali 61
 
21894 ashik.ali 62
 
63
 
21543 ashik.ali 64
public class Utils {
21986 kshitij.so 65
 
23568 govind 66
	private static final Logger logger = LogManager.getLogger(Utils.class);
21986 kshitij.so 67
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
68
	public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
69
	public static final String DOCUMENT_STORE = "/profitmandi/documents/";
23017 ashik.ali 70
	private static final Map<Integer, String> helpMap = new HashMap<>(6);
71
	private static final Map<Integer, String> dthIdAliasMap = new HashMap<>(7);
72
	private static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<>(20);
21986 kshitij.so 73
	private static Map<Long, String> mobileProvidersMap;
74
	private static Map<Long, String> dthProvidersMap;
22757 amit.gupta 75
	//private static final String SMS_GATEWAY = "http://103.15.179.45:8085/SMSGateway/sendingSMS";
76
	private static final String SMS_GATEWAY = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS";
21986 kshitij.so 77
	private static Map<Long, String> allProviders;
23017 ashik.ali 78
	public static Map<RechargeOrderStatus, String> rechargeStatusMap = new HashMap<>();
23884 amit.gupta 79
	public static OrderStatusGroups ORDER_STATUS_GROUPS = new OrderStatusGroups();
21646 kshitij.so 80
 
21986 kshitij.so 81
	static {
82
		helpMap.put(Integer.valueOf(1), "Your VC number starts with 0 and is 11 digits long.");
83
		helpMap.put(Integer.valueOf(2), "Smart card number starts with 2 and is 12 digits long.");
84
		helpMap.put(Integer.valueOf(3), "Smart card number starts with 4 and is 11 digits long.");
85
		helpMap.put(Integer.valueOf(4), "Subscriber ID starts with 1 and is 10 digits long.");
86
		helpMap.put(Integer.valueOf(5), "For customer ID, SMS ID to 9212012299 from your registered mobile no.");
87
		helpMap.put(Integer.valueOf(26), "Customer ID starts with 3 and is 10 digits long.");
21543 ashik.ali 88
 
21986 kshitij.so 89
		dthIdAliasMap.put(Integer.valueOf(1), "VC Number :");
90
		dthIdAliasMap.put(Integer.valueOf(2), "Smart Card Number :");
91
		dthIdAliasMap.put(Integer.valueOf(3), "Smart Card Number :");
92
		dthIdAliasMap.put(Integer.valueOf(4), "Subscriber Id :");
93
		dthIdAliasMap.put(Integer.valueOf(5), "Customer Id :");
94
		dthIdAliasMap.put(Integer.valueOf(0), "Account Number :");
95
		dthIdAliasMap.put(Integer.valueOf(26), "Customer Id :");
21543 ashik.ali 96
 
21986 kshitij.so 97
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
98
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
99
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
100
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed Refunded");
101
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
102
		rechargeStatusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
103
		rechargeStatusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
104
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_PENDING, "Payment Failed");
105
		rechargeStatusMap.put(RechargeOrderStatus.INIT, "Payment Initiated");
106
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_UNKNOWN, "Recharge In Process");
107
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_IN_PROCESS, "Recharge In Process");
108
 
109
		TransactionClient tcl;
110
		try
111
		{
112
			tcl = new TransactionClient();
113
			mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
114
			dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
115
			logger.info("mobileProvidersMap" + mobileProvidersMap);
116
			logger.info("dthProvidersMap" + dthProvidersMap);
22390 amit.gupta 117
			allProviders = new HashMap<>(mobileProvidersMap);
21986 kshitij.so 118
			logger.info("allProviders" + allProviders);
119
			allProviders.putAll(dthProvidersMap);
120
			logger.info("allProviders" + allProviders);
121
 
122
			for (Long operatorId : mobileProvidersMap.keySet()) {
123
				List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId.longValue());
124
				if (!plans.isEmpty())
125
					operatorPlanMap.put(operatorId, plans);
126
			}
127
		} catch (Exception e) {
128
			logger.error("Could not get providers", e);
129
		}
130
	}
131
 
132
 
23509 amit.gupta 133
	public static SellerInfo getSellerInfoBySellerId(int sellerId) throws Exception {
134
		TransactionClient tcl = new TransactionClient();
135
		return tcl.getClient().getSellerInfo((long)sellerId);
136
	}
137
 
21543 ashik.ali 138
	@SuppressWarnings("serial")
139
	public static final Map<String,String> MIME_TYPE = Collections.unmodifiableMap(
140
			new HashMap<String, String>(){
141
				{
142
					put("image/png", "png");
143
					put("image/jpeg", "jpeg");
144
					put("image/pjpeg", "jpeg");
145
					put("application/pdf","pdf");
146
				}
147
			});
148
 
149
	private static String getExportPath(){
150
		String exportPath=null;
151
 
152
		ConfigClient client = ConfigClient.getClient();
153
		try{
154
			exportPath = client.get("export_entities_path");
155
		}catch(Exception ce){
156
			logger.error("Unable to read export path from the config client: ", ce);
157
			logger.warn("Setting the default export path");
158
			exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
159
		}
160
		return exportPath;
161
	}
21986 kshitij.so 162
 
21646 kshitij.so 163
	public static String getRechargeDisplayStatus(RechargeOrderStatus status) {
21986 kshitij.so 164
		if (status == null) {
165
			status = RechargeOrderStatus.INIT;
166
		}
167
		String displayStatus = (String)rechargeStatusMap.get(status);
168
		if (displayStatus == null) {
169
			return "";
170
		}
171
		return displayStatus;
172
	}
21543 ashik.ali 173
 
174
	public static boolean copyDocument(String documentPath){
175
		File source = new File(documentPath);
176
		File dest = new File(DOCUMENT_STORE+source.getName());
177
		try {
178
			FileUtils.copyFile(source, dest);
179
		} catch (IOException e) {
180
			e.printStackTrace();
181
			return false;
182
		}
183
		return true;
184
	}
185
 
186
	public static Map<Long, String> getMobileProvidersMap() {
187
		return mobileProvidersMap;
188
	}
189
 
190
	public static Map<Long, String> getDthProvidersMap() {
191
		return dthProvidersMap;
192
	}
21986 kshitij.so 193
 
21543 ashik.ali 194
	public static Map<Long, String> getAllProviders() {
195
		return allProviders;
196
	}
21986 kshitij.so 197
 
21543 ashik.ali 198
	public static String getProvider(long operatorId) {
21986 kshitij.so 199
		return allProviders.get(operatorId);
200
	}
21543 ashik.ali 201
 
202
	public static void sendSms(String text, String mobileNumber) throws URISyntaxException, IOException{
203
		URIBuilder generalSearchUrl = new URIBuilder(SMS_GATEWAY);
22757 amit.gupta 204
		generalSearchUrl.addParameter("Mobile", "91"+mobileNumber);
205
		generalSearchUrl.addParameter("Username", "srlsaholic");                  
206
		generalSearchUrl.addParameter("Password", "sr18mar"); 					  
23376 amit.gupta 207
		generalSearchUrl.addParameter("SenderID", "SMTDKN");             			  
22757 amit.gupta 208
		generalSearchUrl.setParameter("Message", text);
209
		generalSearchUrl.setParameter("MessageType", "txt");
210
/*		generalSearchUrl.addParameter("ani", "91"+mobileNumber);
21543 ashik.ali 211
		generalSearchUrl.addParameter("uname", "srlsaholic");                  
212
		generalSearchUrl.addParameter("passwd", "sr18mar"); 					  
213
		generalSearchUrl.addParameter("cli", "PROFTM");             			  
214
		generalSearchUrl.setParameter("message", text);
22757 amit.gupta 215
*/		URL url = generalSearchUrl.build().toURL();
22889 amit.gupta 216
		System.out.println(url);
21543 ashik.ali 217
		InputStream is = url.openStream();
218
		String response;
219
		try{
220
			response = IOUtils.toString(is, "UTF-8");
221
			System.out.println("response sms gateway "+response);
222
		}
223
		finally{
224
			is.close();
225
		}
226
	}
22757 amit.gupta 227
 
23909 amit.gupta 228
	public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject, String body, List<File> attachments) throws Exception {
229
		MimeMessage message = mailSender.createMimeMessage();
230
    	MimeMessageHelper helper = new MimeMessageHelper(message,true);
231
    	helper.setSubject(subject);
232
    	helper.setText(body);
23910 amit.gupta 233
    	if(cc != null) {
234
    		helper.setCc(cc);
235
    	}
23909 amit.gupta 236
    	helper.setTo(emailTo);
23912 amit.gupta 237
    	InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
23909 amit.gupta 238
    	helper.setFrom(senderAddress);
23910 amit.gupta 239
    	if(attachments != null) {
240
	    	for (File file : attachments) {
241
	    		helper.addAttachment(file.getName(), file);
242
	    	}
23909 amit.gupta 243
    	}
244
    	mailSender.send(message);
22757 amit.gupta 245
	}
23929 amit.gupta 246
 
247
	public static void sendMailWithAttachment(JavaMailSender mailSender, String emailTo, String[] cc, String subject, String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
248
		MimeMessage message = mailSender.createMimeMessage();
249
		MimeMessageHelper helper = new MimeMessageHelper(message,true);
250
		helper.setSubject(subject);
251
		helper.setText(body);
252
		if(cc != null) {
253
			helper.setCc(cc);
254
		}
255
		helper.setTo(emailTo);
256
		helper.addAttachment(fileName, inputStreamSource);
257
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
258
		helper.setFrom(senderAddress);
259
		mailSender.send(message);
260
	}
22757 amit.gupta 261
 
21543 ashik.ali 262
	public static String[] getOrderStatus(RechargeOrderStatus status){
263
		if (status == null){
264
			status = RechargeOrderStatus.INIT;
265
		}
266
		if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
267
			return new String[]{"false", "PAYMENT FAILED", "Payment failed at the payment gateway."};
268
		}
269
		else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
270
			if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)){
271
				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."
272
						+ " We have put your recharge under process."
273
						+ " As soon as we get a confirmation on this transaction, we will notify you."};
274
			}
275
			else{
276
				return new String[]{"false", "RECHARGE IN PROCESS", "Your Payment is successful.We have put your recharge under process." +
277
				" Please wait while we check with the operator."};
278
			}
279
		}
280
		else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
22391 amit.gupta 281
			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 282
					+ " The entire Amount has been refunded to your wallet."};
283
		} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
284
			return new String[] {"false", "SUCCESS", "Congratulations! Your device is successfully recharged."};
285
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
286
			return new String[]{"false", "PAYMENT REFUNDED","The payment associated with this recharge order has been refunded."};
287
		} else {
288
			return new String[]{"true", "ERROR", "INVALID INPUT"};
289
		}
290
	}
21986 kshitij.so 291
 
292
 
293
	public static String getIconUrl(int entityId,String host, int port, String webapp){
23526 ashik.ali 294
		RestClient rc = new RestClient();
21986 kshitij.so 295
		Map<String, String> params = new HashMap<String, String>();
296
		String response = null;
297
		String uri = webapp+"/entity/"+entityId;
22215 ashik.ali 298
		Map<String, String> headers = new HashMap<>(1);
299
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
21986 kshitij.so 300
		try {
23526 ashik.ali 301
			response = rc.get(SchemeType.HTTP, host , port, uri, params, headers);
23376 amit.gupta 302
		} catch (Throwable e) {
21986 kshitij.so 303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
305
			return "";
306
		}
307
		JSONObject obj = new JSONObject(response);
308
		String iconUrl = null;
309
		try{
310
		iconUrl = obj.getJSONObject("entity").getString("iconImageUrl");
311
		}
312
		catch(Exception e){
313
			return "";
314
		}
315
		return iconUrl==null?"":iconUrl;
316
	}
21894 ashik.ali 317
 
23147 ashik.ali 318
	public static Map<Integer, GstRate> getStateTaxRate(List<Integer> itemIds, int stateId) throws ProfitMandiBusinessException{
23152 ashik.ali 319
		List<Long> longItemIds = new ArrayList<>();
320
		for(int itemId : itemIds){
321
			longItemIds.add(Long.valueOf(itemId));
322
		}
23147 ashik.ali 323
		try{
324
			CatalogService.Client catalogClient = getCatalogClient();
23152 ashik.ali 325
			Map<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateMap = catalogClient.getStateTaxRate(longItemIds, stateId);
23147 ashik.ali 326
			Map<Integer, GstRate> itemIdGstRateMap = new HashMap<>();
23152 ashik.ali 327
			for(Map.Entry<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateEntry : totalTaxRateMap.entrySet()){
23147 ashik.ali 328
				GstRate gstRate = new GstRate();
329
				gstRate.setIgstRate(Double.valueOf(totalTaxRateEntry.getValue().getIgstRate()).floatValue());
330
				gstRate.setCgstRate(Double.valueOf(totalTaxRateEntry.getValue().getCgstRate()).floatValue());
331
				gstRate.setSgstRate(Double.valueOf(totalTaxRateEntry.getValue().getSgstRate()).floatValue());
23152 ashik.ali 332
				itemIdGstRateMap.put(totalTaxRateEntry.getKey().intValue(), gstRate);
23147 ashik.ali 333
			}
334
			return itemIdGstRateMap;
335
		}catch(Exception e){
336
			e.printStackTrace();
23780 ashik.ali 337
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID + ", " + ProfitMandiConstants.STATE_ID, itemIds +", " + stateId, "THRFT_1000");
23147 ashik.ali 338
		}
339
	}
340
 
341
	public static Map<Integer, Float> getIgstTaxRate(List<Integer> itemIds) throws ProfitMandiBusinessException{
23152 ashik.ali 342
		List<Long> longItemIds = new ArrayList<>();
343
		for(int itemId : itemIds){
344
			longItemIds.add(Long.valueOf(itemId));
345
		}
23147 ashik.ali 346
		try{
347
			CatalogService.Client catalogClient = getCatalogClient();
23152 ashik.ali 348
			Map<Long, Double> totalTaxRateMap = catalogClient.getIgstTaxRate(longItemIds);
23147 ashik.ali 349
			Map<Integer, Float> itemIdIgstRateMap = new HashMap<>();
23152 ashik.ali 350
			for(Map.Entry<Long, Double> totalTaxRateEntry : totalTaxRateMap.entrySet()){
351
				itemIdIgstRateMap.put(totalTaxRateEntry.getKey().intValue(), Double.valueOf(totalTaxRateEntry.getValue()).floatValue());
23147 ashik.ali 352
			}
353
			return itemIdIgstRateMap;
354
		}catch(Exception e){
355
			e.printStackTrace();
23780 ashik.ali 356
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, itemIds, "THRFT_1001");
23147 ashik.ali 357
		}
358
	}
359
 
21915 ashik.ali 360
	private static CatalogService.Client getCatalogClient() throws Exception{
361
		try{
362
			CatalogClient client = new CatalogClient();
363
			CatalogService.Client catalogClient = client.getClient();
364
			return catalogClient;
365
		}catch (Exception e) {
366
			throw e;
367
		}
368
	}
23509 amit.gupta 369
 
370
	private static WarehouseService.Client getWarehouseClient() throws Exception{
371
		try{
372
			WarehouseClient client = new WarehouseClient();
373
			WarehouseService.Client warehouseClient = client.getClient();
374
			return warehouseClient;
375
		}catch (Exception e) {
376
			throw e;
377
		}
378
	}
21915 ashik.ali 379
 
380
 
23509 amit.gupta 381
	public static Map<String, Warehouse> getWarehouseByImeis(List<String> imeis) throws Exception{
382
		List<InventoryItem> inventoryItems = getWarehouseClient().getInventoryItemsBySerailNumbers(imeis);
383
		Map<String, InventoryItem> imeiInventoryItemMap = new HashMap<>();
384
		for(InventoryItem inventoryItem : inventoryItems) {
385
			imeiInventoryItemMap.put(inventoryItem.getSerialNumber(), inventoryItem);
386
		}
387
		Map<Integer, Warehouse> warehouseMap = new HashMap<>(); 
388
		Map<String, Warehouse> serialNumberWarehouseMap = new HashMap<>();
389
		InventoryClient client = new InventoryClient();
390
		InventoryService.Client inventoryClient = client.getClient();
391
		logger.info("[imeiInventoryItemMap] {}", imeiInventoryItemMap);
392
		for(InventoryItem inventory : new HashSet<>(imeiInventoryItemMap.values())) {
393
			Warehouse phWarehouse = null;
394
			if(warehouseMap.containsKey(inventory.getPhysicalWarehouseId())) {
395
				phWarehouse = warehouseMap.get((int)inventory.getPhysicalWarehouseId());
396
			} else {
397
				phWarehouse = inventoryClient.getWarehouse(inventory.getPhysicalWarehouseId());
398
				warehouseMap.put((int)inventory.getPhysicalWarehouseId(), phWarehouse);
399
			}
400
			serialNumberWarehouseMap.put(inventory.getSerialNumber(), phWarehouse);
401
 
402
		}
403
		return serialNumberWarehouseMap;
404
	}
405
 
23615 amit.gupta 406
	public static Map<Integer, Warehouse> getWarehousesByIds(Set<Integer> warehouseIds) throws Exception {
407
		InventoryClient client = new InventoryClient();
408
		InventoryService.Client inventoryClient = client.getClient();
409
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
410
		for (Integer warehouseId : warehouseIds) {
411
			warehouseMap.put(warehouseId, inventoryClient.getWarehouse(warehouseId));
412
		}
413
		return warehouseMap;
414
	}
23509 amit.gupta 415
 
23615 amit.gupta 416
 
21902 ashik.ali 417
	public static String getStateCode(String stateName) throws Exception{
418
		return getStateInfo(stateName).getStateCode();
419
	}
420
 
23539 amit.gupta 421
	public static long getStateId(String stateName) throws ProfitMandiBusinessException {
422
		try {
423
			return getStateInfo(stateName).getId();
424
		} catch(Exception e) {
425
			e.printStackTrace();
426
			throw new ProfitMandiBusinessException("State Name", stateName, "Could not fetch state");
427
		}
21902 ashik.ali 428
	}
429
 
23509 amit.gupta 430
	public static StateInfo getStateByStateId(long stateId) throws Exception{
431
		InventoryClient client = new InventoryClient();
432
		InventoryService.Client inventoryClient = client.getClient();
433
		Map<Long, StateInfo> map = inventoryClient.getStateMaster();
434
		return map.get(stateId);
435
	}
436
 
21902 ashik.ali 437
	public static StateInfo getStateInfo(String stateName) throws Exception{
21894 ashik.ali 438
		try{
439
			InventoryClient client = new InventoryClient();
440
			InventoryService.Client inventoryClient = client.getClient();
441
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 442
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 443
			logger.info("State Name: {}", stateName);
21894 ashik.ali 444
			for(StateInfo stateInfo : stateInfos){
22664 amit.gupta 445
				logger.info("State Name from service: {}", stateInfo.getStateName());
22938 amit.gupta 446
				if(stateName.toUpperCase().equals(stateInfo.getStateName().toUpperCase())){
21902 ashik.ali 447
					return stateInfo;
21894 ashik.ali 448
				}
449
			}
21902 ashik.ali 450
			throw new Exception("Not found");
21894 ashik.ali 451
		}catch (Exception e) {
452
			e.printStackTrace();
453
			throw e;
454
		}
455
	}
22351 ashik.ali 456
 
23296 ashik.ali 457
	public static List<String> getAllStateNames(){
458
		List<String> sortedStateNames = new ArrayList<>();
459
		try{
460
			InventoryClient client = new InventoryClient();
461
			InventoryService.Client inventoryClient = client.getClient();
462
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
463
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
464
			Set<String> stateNames = new TreeSet<>();
465
 
466
			for(StateInfo stateInfo : stateInfos){
467
				stateNames.add(stateInfo.getStateName());
468
			}
469
			sortedStateNames = new ArrayList<>(stateNames);
470
			return sortedStateNames;
471
		}catch (Exception e) {
472
			e.printStackTrace();
473
			return sortedStateNames;
474
		}
475
	}
23509 amit.gupta 476
 
477
	public static Map<Long, StateInfo> getAllStatesMap() throws Exception{
478
		InventoryClient client = new InventoryClient();
479
		InventoryService.Client inventoryClient = client.getClient();
480
		return  inventoryClient.getStateMaster();
481
 
482
	}
23296 ashik.ali 483
 
22351 ashik.ali 484
	public static User createSaholicUser(String emailId) throws ProfitMandiBusinessException{
485
		in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();
486
		user.setEmail(emailId);
487
		user.setPassword("");
488
		user.setCommunicationEmail(emailId);
489
		user.setSex(Sex.WONT_SAY);
490
 
491
		try {
492
			UserClient userContextServiceClient = new UserClient();
493
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
494
			user = userClient.createUser(user);
495
			return user;
496
		}catch (UserContextException ux){
497
			logger.error("Unable to register user: " + ux.getMessage());
498
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
499
		} catch (TTransportException e) {
500
			logger.error("Unable to register user." + e);
501
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
502
		}catch (TException e) {
503
			logger.error("Unable to register user." + e);
504
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
505
		} 
506
	}
22757 amit.gupta 507
 
508
	public static void main (String[] args) throws Exception{
509
		Utils.sendSms("Hello", "9990381569");
510
	}
23509 amit.gupta 511
 
512
	public static WarehouseAddress getWarehouseByWarehouseId(int warehouseAddressId) throws Exception{
513
		TransactionClient tcl = new TransactionClient();
514
		return tcl.getClient().getWarehouseAddress((long)warehouseAddressId);
515
	}
23074 ashik.ali 516
 
23884 amit.gupta 517
 
21543 ashik.ali 518
}