Subversion Repositories SmartDukaan

Rev

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