Subversion Repositories SmartDukaan

Rev

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