Subversion Repositories SmartDukaan

Rev

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