Subversion Repositories SmartDukaan

Rev

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