Subversion Repositories SmartDukaan

Rev

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