Subversion Repositories SmartDukaan

Rev

Rev 32724 | Rev 33045 | 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
 
26066 amit.gupta 3
import com.google.gson.Gson;
22215 ashik.ali 4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26079 amit.gupta 5
import com.spice.profitmandi.common.web.client.RestClient;
21894 ashik.ali 6
import com.spice.profitmandi.thrift.clients.CatalogClient;
7
import com.spice.profitmandi.thrift.clients.InventoryClient;
8
import com.spice.profitmandi.thrift.clients.TransactionClient;
23509 amit.gupta 9
import com.spice.profitmandi.thrift.clients.WarehouseClient;
21894 ashik.ali 10
import in.shop2020.model.v1.catalog.CatalogService;
11
import in.shop2020.model.v1.inventory.InventoryService;
12
import in.shop2020.model.v1.inventory.StateInfo;
23509 amit.gupta 13
import in.shop2020.model.v1.inventory.Warehouse;
23884 amit.gupta 14
import in.shop2020.model.v1.order.OrderStatusGroups;
21894 ashik.ali 15
import in.shop2020.model.v1.order.RechargeOrderStatus;
16
import in.shop2020.model.v1.order.RechargePlan;
23509 amit.gupta 17
import in.shop2020.model.v1.order.WarehouseAddress;
18
import in.shop2020.warehouse.InventoryItem;
19
import in.shop2020.warehouse.WarehouseService;
30122 amit.gupta 20
import org.apache.commons.io.FileUtils;
21
import org.apache.commons.lang3.StringEscapeUtils;
22
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.Logger;
24
import org.springframework.core.io.InputStreamSource;
25
import org.springframework.mail.javamail.JavaMailSender;
26
import org.springframework.mail.javamail.MimeMessageHelper;
21543 ashik.ali 27
 
30122 amit.gupta 28
import javax.mail.Multipart;
29
import javax.mail.internet.InternetAddress;
30
import javax.mail.internet.MimeBodyPart;
31
import javax.mail.internet.MimeMessage;
32
import javax.mail.internet.MimeMultipart;
33
import java.io.File;
34
import java.io.IOException;
35
import java.io.Serializable;
32955 amit.gupta 36
import java.time.*;
30122 amit.gupta 37
import java.util.*;
38
import java.util.concurrent.ConcurrentHashMap;
39
import java.util.function.Function;
40
import java.util.function.Predicate;
41
import java.util.regex.Matcher;
42
import java.util.regex.Pattern;
43
 
21543 ashik.ali 44
public class Utils {
31177 tejbeer 45
 
31903 amit.gupta 46
	public static final float FLOAT_EPSILON = 0.001f;
21986 kshitij.so 47
 
31903 amit.gupta 48
	public static final double DOUBLE_EPSILON = 0.001d;
31177 tejbeer 49
 
23568 govind 50
	private static final Logger logger = LogManager.getLogger(Utils.class);
21986 kshitij.so 51
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
52
	public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
53
	public static final String DOCUMENT_STORE = "/profitmandi/documents/";
26066 amit.gupta 54
	private Gson gson = new Gson();
23017 ashik.ali 55
	private static final Map<Integer, String> helpMap = new HashMap<>(6);
56
	private static final Map<Integer, String> dthIdAliasMap = new HashMap<>(7);
57
	private static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<>(20);
21986 kshitij.so 58
	private static Map<Long, String> mobileProvidersMap;
59
	private static Map<Long, String> dthProvidersMap;
60
	private static Map<Long, String> allProviders;
23017 ashik.ali 61
	public static Map<RechargeOrderStatus, String> rechargeStatusMap = new HashMap<>();
23884 amit.gupta 62
	public static OrderStatusGroups ORDER_STATUS_GROUPS = new OrderStatusGroups();
25764 amit.gupta 63
	public static final String SYSTEM_PARTNER = "testpxps@gmail.com";
27121 amit.gupta 64
	public static final int SYSTEM_PARTNER_ID = 175120474;
26079 amit.gupta 65
	private static final RestClient rc = new RestClient();
31177 tejbeer 66
	private static final String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
67
	public static final LocalTime MAX_TIME = LocalTime.of(23, 59, 59);
68
	// Compile regular expression to get the pattern
69
	private static final Pattern pattern = Pattern.compile(regex);
24440 amit.gupta 70
 
31177 tejbeer 71
	public static boolean validateEmail(String email) {
72
		if (email == null) {
73
			return false;
74
		}
75
		Matcher matcher = pattern.matcher(email);
76
		return matcher.matches();
77
	}
78
 
21543 ashik.ali 79
	@SuppressWarnings("serial")
24440 amit.gupta 80
	public static final Map<String, String> MIME_TYPE = Collections.unmodifiableMap(new HashMap<String, String>() {
81
		{
82
			put("image/png", "png");
83
			put("image/jpeg", "jpeg");
84
			put("image/pjpeg", "jpeg");
85
			put("application/pdf", "pdf");
27285 tejbeer 86
			put("application/msword", "doc");
87
			put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
88
			put("application/vnd.ms-excel", "xls");
89
			put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
24440 amit.gupta 90
		}
91
	});
21543 ashik.ali 92
 
24440 amit.gupta 93
	private static String getExportPath() {
26534 amit.gupta 94
		String exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
27285 tejbeer 95
		/*
96
		 * String exportPath = null;
97
		 * 
98
		 * try { ConfigClient client = ConfigClient.getClient(); exportPath =
99
		 * client.get("export_entities_path"); } catch (Exception ce) {
100
		 * logger.error("Unable to read export path from the config client: ", ce);
101
		 * logger.warn("Setting the default export path"); exportPath =
102
		 * "/var/lib/tomcat7/webapps/export/html/entities/"; }
103
		 */
21543 ashik.ali 104
		return exportPath;
105
	}
21986 kshitij.so 106
 
32724 amit.gupta 107
	public static LocalDateTime toLocalDateTime(long epochTimeInMillis) {
108
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochTimeInMillis), ZoneId.systemDefault());
109
	}
110
 
21646 kshitij.so 111
	public static String getRechargeDisplayStatus(RechargeOrderStatus status) {
21986 kshitij.so 112
		if (status == null) {
113
			status = RechargeOrderStatus.INIT;
114
		}
24440 amit.gupta 115
		String displayStatus = (String) rechargeStatusMap.get(status);
21986 kshitij.so 116
		if (displayStatus == null) {
117
			return "";
118
		}
119
		return displayStatus;
120
	}
21543 ashik.ali 121
 
24440 amit.gupta 122
	public static boolean copyDocument(String documentPath) {
21543 ashik.ali 123
		File source = new File(documentPath);
24440 amit.gupta 124
		File dest = new File(DOCUMENT_STORE + source.getName());
21543 ashik.ali 125
		try {
126
			FileUtils.copyFile(source, dest);
127
		} catch (IOException e) {
128
			e.printStackTrace();
129
			return false;
130
		}
131
		return true;
132
	}
133
 
134
	public static Map<Long, String> getMobileProvidersMap() {
135
		return mobileProvidersMap;
136
	}
137
 
138
	public static Map<Long, String> getDthProvidersMap() {
139
		return dthProvidersMap;
140
	}
21986 kshitij.so 141
 
21543 ashik.ali 142
	public static Map<Long, String> getAllProviders() {
143
		return allProviders;
144
	}
21986 kshitij.so 145
 
21543 ashik.ali 146
	public static String getProvider(long operatorId) {
21986 kshitij.so 147
		return allProviders.get(operatorId);
148
	}
21543 ashik.ali 149
 
26079 amit.gupta 150
	public static void sendSms(String text, String mobileNumber) throws Exception {
26089 amit.gupta 151
		Map<String, String> paramsMap = new HashMap<>();
26673 amit.gupta 152
		paramsMap.put("username", "SmartDukaanT");
153
		paramsMap.put("password", "Smart@91");
154
		paramsMap.put("senderID", "SMTDKN");
155
		paramsMap.put("message", text);
156
		paramsMap.put("mobile", mobileNumber);
157
		paramsMap.put("messageType", "Text");
27285 tejbeer 158
		// rc.getResponse(SMS_GATEWAY, paramsMap, null);
21543 ashik.ali 159
	}
24440 amit.gupta 160
 
161
	public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
162
			String body, List<File> attachments) throws Exception {
23909 amit.gupta 163
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 164
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
165
		helper.setSubject(subject);
166
		helper.setText(body);
167
		if (cc != null) {
168
			helper.setCc(cc);
169
		}
27123 amit.gupta 170
		helper.setTo(emailTo);
27285 tejbeer 171
		// helper.setTo("amit.gupta@smartdukaan.com");
24440 amit.gupta 172
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
173
		helper.setFrom(senderAddress);
174
		if (attachments != null) {
175
			for (File file : attachments) {
176
				helper.addAttachment(file.getName(), file);
177
			}
178
		}
179
		mailSender.send(message);
22757 amit.gupta 180
	}
25764 amit.gupta 181
 
24593 amit.gupta 182
	public static class Attachment {
183
		public Attachment(String fileName, InputStreamSource inputStreamSource) {
184
			this.fileName = fileName;
185
			this.inputStreamSource = inputStreamSource;
186
		}
25764 amit.gupta 187
 
188
		private String fileName;
24593 amit.gupta 189
		private InputStreamSource inputStreamSource;
25764 amit.gupta 190
 
24593 amit.gupta 191
		public String getFileName() {
192
			return fileName;
193
		}
25764 amit.gupta 194
 
24593 amit.gupta 195
		public void setFileName(String fileName) {
196
			this.fileName = fileName;
197
		}
25764 amit.gupta 198
 
24593 amit.gupta 199
		public InputStreamSource getInputStreamSource() {
200
			return inputStreamSource;
201
		}
25764 amit.gupta 202
 
24593 amit.gupta 203
		public void setInputStreamSource(InputStreamSource inputStreamSource) {
204
			this.inputStreamSource = inputStreamSource;
205
		}
25764 amit.gupta 206
 
24593 amit.gupta 207
		@Override
208
		public String toString() {
209
			return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
210
		}
211
	}
25764 amit.gupta 212
 
24440 amit.gupta 213
	public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
214
			String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
23929 amit.gupta 215
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 216
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
23929 amit.gupta 217
		helper.setSubject(subject);
218
		helper.setText(body);
24440 amit.gupta 219
		if (cc != null) {
23929 amit.gupta 220
			helper.setCc(cc);
221
		}
222
		helper.setTo(emailTo);
223
		helper.addAttachment(fileName, inputStreamSource);
224
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
225
		helper.setFrom(senderAddress);
226
		mailSender.send(message);
227
	}
25764 amit.gupta 228
 
26978 tejbeer 229
	public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
230
			String subject, String body, Attachment... attachments) throws Exception {
24593 amit.gupta 231
		MimeMessage message = mailSender.createMimeMessage();
232
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
233
		helper.setSubject(subject);
234
		helper.setText(body);
235
		if (cc != null) {
236
			helper.setCc(cc);
237
		}
26978 tejbeer 238
		if (bcc != null) {
239
			helper.setBcc(bcc);
240
		}
24593 amit.gupta 241
		helper.setTo(emailTo);
25764 amit.gupta 242
		for (Attachment attachment : attachments) {
24593 amit.gupta 243
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
244
		}
245
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
246
		helper.setFrom(senderAddress);
247
		mailSender.send(message);
248
	}
25764 amit.gupta 249
 
250
	public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
251
			String subject, String body, Attachment... attachments) throws Exception {
25743 amit.gupta 252
		MimeMessage message = mailSender.createMimeMessage();
253
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
254
		helper.setSubject(subject);
31697 amit.gupta 255
		helper.setText(body, true);
25764 amit.gupta 256
		// helper.setText(body, true);
25743 amit.gupta 257
		if (cc != null) {
258
			helper.setCc(cc);
259
		}
260
		helper.setTo(emailTo);
25764 amit.gupta 261
		for (Attachment attachment : attachments) {
262
			if (attachment == null)
263
				break;
25743 amit.gupta 264
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
265
		}
266
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
267
		helper.setFrom(senderAddress);
268
		mailSender.send(message);
269
	}
24440 amit.gupta 270
 
271
	public static String[] getOrderStatus(RechargeOrderStatus status) {
272
		if (status == null) {
21543 ashik.ali 273
			status = RechargeOrderStatus.INIT;
274
		}
24440 amit.gupta 275
		if (status.equals(RechargeOrderStatus.PAYMENT_FAILED) || status.equals(RechargeOrderStatus.PAYMENT_PENDING)) {
276
			return new String[] { "false", "PAYMENT FAILED", "Payment failed at the payment gateway." };
277
		} else if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
278
				|| status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
279
			if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
280
				return new String[] { "false", "PAYMENT SUCCESSFUL",
281
						"Your Payment was successful but due to some internal error with the operator's system we are not sure if the recharge was successful."
282
								+ " We have put your recharge under process."
283
								+ " As soon as we get a confirmation on this transaction, we will notify you." };
284
			} else {
285
				return new String[] { "false", "RECHARGE IN PROCESS",
286
						"Your Payment is successful.We have put your recharge under process."
287
								+ " Please wait while we check with the operator." };
21543 ashik.ali 288
			}
24440 amit.gupta 289
		} else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)
290
				|| status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)) {
291
			return new String[] { "false", "RECHARGE FAILED",
292
					"Your Payment was successful but unfortunately the recharge failed.Don't worry your payment is safe with us."
293
							+ " The entire Amount has been refunded to your wallet." };
294
		} else if (status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)) {
295
			return new String[] { "false", "SUCCESS", "Congratulations! Your device is successfully recharged." };
296
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED)
297
				|| status.equals(RechargeOrderStatus.REFUNDED)) {
298
			return new String[] { "false", "PAYMENT REFUNDED",
299
					"The payment associated with this recharge order has been refunded." };
21543 ashik.ali 300
		} else {
24440 amit.gupta 301
			return new String[] { "true", "ERROR", "INVALID INPUT" };
21543 ashik.ali 302
		}
303
	}
21986 kshitij.so 304
 
24440 amit.gupta 305
	public static String getIconUrl(int entityId, String host, int port, String webapp) {
25743 amit.gupta 306
		return "";
21986 kshitij.so 307
	}
25764 amit.gupta 308
 
309
	private static CatalogService.Client getCatalogClient() throws Exception {
310
		try {
21915 ashik.ali 311
			CatalogClient client = new CatalogClient();
312
			CatalogService.Client catalogClient = client.getClient();
313
			return catalogClient;
25764 amit.gupta 314
		} catch (Exception e) {
21915 ashik.ali 315
			throw e;
316
		}
317
	}
23509 amit.gupta 318
 
24440 amit.gupta 319
	private static WarehouseService.Client getWarehouseClient() throws Exception {
320
		try {
23509 amit.gupta 321
			WarehouseClient client = new WarehouseClient();
322
			WarehouseService.Client warehouseClient = client.getClient();
323
			return warehouseClient;
24440 amit.gupta 324
		} catch (Exception e) {
23509 amit.gupta 325
			throw e;
326
		}
327
	}
24440 amit.gupta 328
 
329
	public static Map<String, Warehouse> getWarehouseByImeis(List<String> imeis) throws Exception {
23509 amit.gupta 330
		List<InventoryItem> inventoryItems = getWarehouseClient().getInventoryItemsBySerailNumbers(imeis);
331
		Map<String, InventoryItem> imeiInventoryItemMap = new HashMap<>();
24440 amit.gupta 332
		for (InventoryItem inventoryItem : inventoryItems) {
23509 amit.gupta 333
			imeiInventoryItemMap.put(inventoryItem.getSerialNumber(), inventoryItem);
334
		}
24440 amit.gupta 335
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
23509 amit.gupta 336
		Map<String, Warehouse> serialNumberWarehouseMap = new HashMap<>();
337
		InventoryClient client = new InventoryClient();
338
		InventoryService.Client inventoryClient = client.getClient();
339
		logger.info("[imeiInventoryItemMap] {}", imeiInventoryItemMap);
24440 amit.gupta 340
		for (InventoryItem inventory : new HashSet<>(imeiInventoryItemMap.values())) {
23509 amit.gupta 341
			Warehouse phWarehouse = null;
24440 amit.gupta 342
			if (warehouseMap.containsKey(inventory.getPhysicalWarehouseId())) {
343
				phWarehouse = warehouseMap.get((int) inventory.getPhysicalWarehouseId());
23509 amit.gupta 344
			} else {
345
				phWarehouse = inventoryClient.getWarehouse(inventory.getPhysicalWarehouseId());
24440 amit.gupta 346
				warehouseMap.put((int) inventory.getPhysicalWarehouseId(), phWarehouse);
23509 amit.gupta 347
			}
348
			serialNumberWarehouseMap.put(inventory.getSerialNumber(), phWarehouse);
24440 amit.gupta 349
 
23509 amit.gupta 350
		}
351
		return serialNumberWarehouseMap;
352
	}
24440 amit.gupta 353
 
23615 amit.gupta 354
	public static Map<Integer, Warehouse> getWarehousesByIds(Set<Integer> warehouseIds) throws Exception {
355
		InventoryClient client = new InventoryClient();
356
		InventoryService.Client inventoryClient = client.getClient();
357
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
358
		for (Integer warehouseId : warehouseIds) {
359
			warehouseMap.put(warehouseId, inventoryClient.getWarehouse(warehouseId));
360
		}
361
		return warehouseMap;
362
	}
24440 amit.gupta 363
 
364
	public static String getStateCode(String stateName) throws Exception {
21902 ashik.ali 365
		return getStateInfo(stateName).getStateCode();
366
	}
24440 amit.gupta 367
 
23539 amit.gupta 368
	public static long getStateId(String stateName) throws ProfitMandiBusinessException {
369
		try {
370
			return getStateInfo(stateName).getId();
24440 amit.gupta 371
		} catch (Exception e) {
23539 amit.gupta 372
			e.printStackTrace();
373
			throw new ProfitMandiBusinessException("State Name", stateName, "Could not fetch state");
374
		}
21902 ashik.ali 375
	}
24440 amit.gupta 376
 
377
	public static StateInfo getStateByStateId(long stateId) throws Exception {
23509 amit.gupta 378
		InventoryClient client = new InventoryClient();
379
		InventoryService.Client inventoryClient = client.getClient();
380
		Map<Long, StateInfo> map = inventoryClient.getStateMaster();
381
		return map.get(stateId);
382
	}
24440 amit.gupta 383
 
384
	public static StateInfo getStateInfo(String stateName) throws Exception {
385
		try {
21894 ashik.ali 386
			InventoryClient client = new InventoryClient();
387
			InventoryService.Client inventoryClient = client.getClient();
388
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 389
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 390
			logger.info("State Name: {}", stateName);
24440 amit.gupta 391
			for (StateInfo stateInfo : stateInfos) {
22664 amit.gupta 392
				logger.info("State Name from service: {}", stateInfo.getStateName());
24440 amit.gupta 393
				if (stateName.toUpperCase().equals(stateInfo.getStateName().toUpperCase())) {
21902 ashik.ali 394
					return stateInfo;
21894 ashik.ali 395
				}
396
			}
21902 ashik.ali 397
			throw new Exception("Not found");
24440 amit.gupta 398
		} catch (Exception e) {
21894 ashik.ali 399
			e.printStackTrace();
400
			throw e;
401
		}
402
	}
24440 amit.gupta 403
 
404
	public static void main(String[] args) throws Exception {
22757 amit.gupta 405
		Utils.sendSms("Hello", "9990381569");
406
	}
23509 amit.gupta 407
 
24440 amit.gupta 408
	public static WarehouseAddress getWarehouseByWarehouseId(int warehouseAddressId) throws Exception {
23509 amit.gupta 409
		TransactionClient tcl = new TransactionClient();
24440 amit.gupta 410
		return tcl.getClient().getWarehouseAddress((long) warehouseAddressId);
23509 amit.gupta 411
	}
24440 amit.gupta 412
 
25766 amit.gupta 413
	public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
414
			String body, Map<? extends Serializable, File> map) throws Exception {
415
		MimeMessage message = mailSender.createMimeMessage();
416
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
417
		helper.setSubject(subject);
25998 amit.gupta 418
		Multipart mp = new MimeMultipart();
27285 tejbeer 419
 
25766 amit.gupta 420
		MimeBodyPart messageBodyPart = new MimeBodyPart();
27285 tejbeer 421
		messageBodyPart.setContent(body, "text/html");
25998 amit.gupta 422
		mp.addBodyPart(messageBodyPart);
27285 tejbeer 423
 
25766 amit.gupta 424
		// helper.setText(body, true);
425
		if (cc != null) {
426
			helper.setCc(cc);
427
		}
428
		helper.setTo(emailTo);
429
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
430
		helper.setFrom(senderAddress);
431
		for (Map.Entry<? extends Serializable, File> entry : map.entrySet()) {
25764 amit.gupta 432
			entry.getKey();
433
			entry.getValue();
434
			MimeBodyPart imagePart = new MimeBodyPart();
435
			imagePart.setHeader("Content-ID", entry.getKey() + "");
436
			imagePart.setDisposition(MimeBodyPart.INLINE);
437
			imagePart.attachFile(entry.getValue());
25766 amit.gupta 438
			mp.addBodyPart(imagePart);
25998 amit.gupta 439
 
25764 amit.gupta 440
		}
25768 amit.gupta 441
		message.setContent(mp);
25767 amit.gupta 442
		mailSender.send(message);
25764 amit.gupta 443
 
444
	}
27285 tejbeer 445
 
32199 amit.gupta 446
	public String html(String string) {
447
		return org.apache.commons.lang.StringEscapeUtils.escapeHtml(String.valueOf(string));
448
	}
449
 
26066 amit.gupta 450
	public String htmlJson(Object object) {
451
		return StringEscapeUtils.escapeHtml4(gson.toJson(object));
452
	}
25764 amit.gupta 453
 
26534 amit.gupta 454
	public static String getHyphenatedString(String s) {
455
		s = s.trim().replaceAll("\\s+", " ");
456
		s = s.replaceAll("\\s", "-");
457
		return s.toLowerCase();
458
	}
27285 tejbeer 459
 
26989 amit.gupta 460
	public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
461
			String body, Attachment... attachments) throws Exception {
462
		MimeMessage message = mailSender.createMimeMessage();
463
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
464
		helper.setSubject(subject);
465
		helper.setText(body);
466
		if (cc != null) {
467
			helper.setCc(cc);
468
		}
469
		helper.setTo(emailTo);
470
		for (Attachment attachment : attachments) {
471
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
472
		}
473
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
474
		helper.setFrom(senderAddress);
475
		mailSender.send(message);
476
	}
30122 amit.gupta 477
 
29552 amit.gupta 478
	public static int compareFloat(float f1, float f2) {
30122 amit.gupta 479
		if ((f1 - f2 < FLOAT_EPSILON) || (f1 - f2 < -FLOAT_EPSILON)) {
29552 amit.gupta 480
			return 0;
481
		}
482
		return Float.compare(f1, f2);
483
	}
26534 amit.gupta 484
 
31177 tejbeer 485
	public static int compareDouble(double d1, double d2) {
486
		if ((d1 - d2 < DOUBLE_EPSILON) || (d1 - d2 < -DOUBLE_EPSILON)) {
487
			return 0;
488
		}
489
		return Double.compare(d1, d2);
490
	}
491
 
30122 amit.gupta 492
	public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
493
		Set<Object> seen = ConcurrentHashMap.newKeySet();
494
		return t -> seen.add(keyExtractor.apply(t));
495
	}
496
 
31903 amit.gupta 497
	public static <T> Predicate<T> smallestByKey(Function<? super T, ?> keyExtractor) {
498
		Set<Object> seen = ConcurrentHashMap.newKeySet();
499
		return t -> seen.add(keyExtractor.apply(t));
500
	}
501
 
32955 amit.gupta 502
	public static LocalDate convertToLocalDate(Date dateToConvert) {
503
		return Instant.ofEpochMilli(dateToConvert.getTime())
504
				.atZone(ZoneId.systemDefault())
505
				.toLocalDate();
506
	}
507
	public static LocalDateTime convertToLocalDateTime(Date dateToConvert) {
508
		return Instant.ofEpochMilli(dateToConvert.getTime())
509
				.atZone(ZoneId.systemDefault())
510
				.toLocalDateTime();
511
	}
512
 
26084 amit.gupta 513
}