Subversion Repositories SmartDukaan

Rev

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