Subversion Repositories SmartDukaan

Rev

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