Subversion Repositories SmartDukaan

Rev

Rev 31903 | Rev 32724 | 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;
36
import java.time.LocalTime;
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
 
21646 kshitij.so 107
	public static String getRechargeDisplayStatus(RechargeOrderStatus status) {
21986 kshitij.so 108
		if (status == null) {
109
			status = RechargeOrderStatus.INIT;
110
		}
24440 amit.gupta 111
		String displayStatus = (String) rechargeStatusMap.get(status);
21986 kshitij.so 112
		if (displayStatus == null) {
113
			return "";
114
		}
115
		return displayStatus;
116
	}
21543 ashik.ali 117
 
24440 amit.gupta 118
	public static boolean copyDocument(String documentPath) {
21543 ashik.ali 119
		File source = new File(documentPath);
24440 amit.gupta 120
		File dest = new File(DOCUMENT_STORE + source.getName());
21543 ashik.ali 121
		try {
122
			FileUtils.copyFile(source, dest);
123
		} catch (IOException e) {
124
			e.printStackTrace();
125
			return false;
126
		}
127
		return true;
128
	}
129
 
130
	public static Map<Long, String> getMobileProvidersMap() {
131
		return mobileProvidersMap;
132
	}
133
 
134
	public static Map<Long, String> getDthProvidersMap() {
135
		return dthProvidersMap;
136
	}
21986 kshitij.so 137
 
21543 ashik.ali 138
	public static Map<Long, String> getAllProviders() {
139
		return allProviders;
140
	}
21986 kshitij.so 141
 
21543 ashik.ali 142
	public static String getProvider(long operatorId) {
21986 kshitij.so 143
		return allProviders.get(operatorId);
144
	}
21543 ashik.ali 145
 
26079 amit.gupta 146
	public static void sendSms(String text, String mobileNumber) throws Exception {
26089 amit.gupta 147
		Map<String, String> paramsMap = new HashMap<>();
26673 amit.gupta 148
		paramsMap.put("username", "SmartDukaanT");
149
		paramsMap.put("password", "Smart@91");
150
		paramsMap.put("senderID", "SMTDKN");
151
		paramsMap.put("message", text);
152
		paramsMap.put("mobile", mobileNumber);
153
		paramsMap.put("messageType", "Text");
27285 tejbeer 154
		// rc.getResponse(SMS_GATEWAY, paramsMap, null);
21543 ashik.ali 155
	}
24440 amit.gupta 156
 
157
	public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
158
			String body, List<File> attachments) throws Exception {
23909 amit.gupta 159
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 160
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
161
		helper.setSubject(subject);
162
		helper.setText(body);
163
		if (cc != null) {
164
			helper.setCc(cc);
165
		}
27123 amit.gupta 166
		helper.setTo(emailTo);
27285 tejbeer 167
		// helper.setTo("amit.gupta@smartdukaan.com");
24440 amit.gupta 168
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
169
		helper.setFrom(senderAddress);
170
		if (attachments != null) {
171
			for (File file : attachments) {
172
				helper.addAttachment(file.getName(), file);
173
			}
174
		}
175
		mailSender.send(message);
22757 amit.gupta 176
	}
25764 amit.gupta 177
 
24593 amit.gupta 178
	public static class Attachment {
179
		public Attachment(String fileName, InputStreamSource inputStreamSource) {
180
			this.fileName = fileName;
181
			this.inputStreamSource = inputStreamSource;
182
		}
25764 amit.gupta 183
 
184
		private String fileName;
24593 amit.gupta 185
		private InputStreamSource inputStreamSource;
25764 amit.gupta 186
 
24593 amit.gupta 187
		public String getFileName() {
188
			return fileName;
189
		}
25764 amit.gupta 190
 
24593 amit.gupta 191
		public void setFileName(String fileName) {
192
			this.fileName = fileName;
193
		}
25764 amit.gupta 194
 
24593 amit.gupta 195
		public InputStreamSource getInputStreamSource() {
196
			return inputStreamSource;
197
		}
25764 amit.gupta 198
 
24593 amit.gupta 199
		public void setInputStreamSource(InputStreamSource inputStreamSource) {
200
			this.inputStreamSource = inputStreamSource;
201
		}
25764 amit.gupta 202
 
24593 amit.gupta 203
		@Override
204
		public String toString() {
205
			return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
206
		}
207
	}
25764 amit.gupta 208
 
24440 amit.gupta 209
	public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
210
			String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
23929 amit.gupta 211
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 212
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
23929 amit.gupta 213
		helper.setSubject(subject);
214
		helper.setText(body);
24440 amit.gupta 215
		if (cc != null) {
23929 amit.gupta 216
			helper.setCc(cc);
217
		}
218
		helper.setTo(emailTo);
219
		helper.addAttachment(fileName, inputStreamSource);
220
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
221
		helper.setFrom(senderAddress);
222
		mailSender.send(message);
223
	}
25764 amit.gupta 224
 
26978 tejbeer 225
	public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String[] bcc,
226
			String subject, String body, Attachment... attachments) throws Exception {
24593 amit.gupta 227
		MimeMessage message = mailSender.createMimeMessage();
228
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
229
		helper.setSubject(subject);
230
		helper.setText(body);
231
		if (cc != null) {
232
			helper.setCc(cc);
233
		}
26978 tejbeer 234
		if (bcc != null) {
235
			helper.setBcc(bcc);
236
		}
24593 amit.gupta 237
		helper.setTo(emailTo);
25764 amit.gupta 238
		for (Attachment attachment : attachments) {
24593 amit.gupta 239
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
240
		}
241
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
242
		helper.setFrom(senderAddress);
243
		mailSender.send(message);
244
	}
25764 amit.gupta 245
 
246
	public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
247
			String subject, String body, Attachment... attachments) throws Exception {
25743 amit.gupta 248
		MimeMessage message = mailSender.createMimeMessage();
249
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
250
		helper.setSubject(subject);
31697 amit.gupta 251
		helper.setText(body, true);
25764 amit.gupta 252
		// helper.setText(body, true);
25743 amit.gupta 253
		if (cc != null) {
254
			helper.setCc(cc);
255
		}
256
		helper.setTo(emailTo);
25764 amit.gupta 257
		for (Attachment attachment : attachments) {
258
			if (attachment == null)
259
				break;
25743 amit.gupta 260
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
261
		}
262
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
263
		helper.setFrom(senderAddress);
264
		mailSender.send(message);
265
	}
24440 amit.gupta 266
 
267
	public static String[] getOrderStatus(RechargeOrderStatus status) {
268
		if (status == null) {
21543 ashik.ali 269
			status = RechargeOrderStatus.INIT;
270
		}
24440 amit.gupta 271
		if (status.equals(RechargeOrderStatus.PAYMENT_FAILED) || status.equals(RechargeOrderStatus.PAYMENT_PENDING)) {
272
			return new String[] { "false", "PAYMENT FAILED", "Payment failed at the payment gateway." };
273
		} else if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
274
				|| status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
275
			if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
276
				return new String[] { "false", "PAYMENT SUCCESSFUL",
277
						"Your Payment was successful but due to some internal error with the operator's system we are not sure if the recharge was successful."
278
								+ " We have put your recharge under process."
279
								+ " As soon as we get a confirmation on this transaction, we will notify you." };
280
			} else {
281
				return new String[] { "false", "RECHARGE IN PROCESS",
282
						"Your Payment is successful.We have put your recharge under process."
283
								+ " Please wait while we check with the operator." };
21543 ashik.ali 284
			}
24440 amit.gupta 285
		} else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)
286
				|| status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)) {
287
			return new String[] { "false", "RECHARGE FAILED",
288
					"Your Payment was successful but unfortunately the recharge failed.Don't worry your payment is safe with us."
289
							+ " The entire Amount has been refunded to your wallet." };
290
		} else if (status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)) {
291
			return new String[] { "false", "SUCCESS", "Congratulations! Your device is successfully recharged." };
292
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED)
293
				|| status.equals(RechargeOrderStatus.REFUNDED)) {
294
			return new String[] { "false", "PAYMENT REFUNDED",
295
					"The payment associated with this recharge order has been refunded." };
21543 ashik.ali 296
		} else {
24440 amit.gupta 297
			return new String[] { "true", "ERROR", "INVALID INPUT" };
21543 ashik.ali 298
		}
299
	}
21986 kshitij.so 300
 
24440 amit.gupta 301
	public static String getIconUrl(int entityId, String host, int port, String webapp) {
25743 amit.gupta 302
		return "";
21986 kshitij.so 303
	}
25764 amit.gupta 304
 
305
	private static CatalogService.Client getCatalogClient() throws Exception {
306
		try {
21915 ashik.ali 307
			CatalogClient client = new CatalogClient();
308
			CatalogService.Client catalogClient = client.getClient();
309
			return catalogClient;
25764 amit.gupta 310
		} catch (Exception e) {
21915 ashik.ali 311
			throw e;
312
		}
313
	}
23509 amit.gupta 314
 
24440 amit.gupta 315
	private static WarehouseService.Client getWarehouseClient() throws Exception {
316
		try {
23509 amit.gupta 317
			WarehouseClient client = new WarehouseClient();
318
			WarehouseService.Client warehouseClient = client.getClient();
319
			return warehouseClient;
24440 amit.gupta 320
		} catch (Exception e) {
23509 amit.gupta 321
			throw e;
322
		}
323
	}
24440 amit.gupta 324
 
325
	public static Map<String, Warehouse> getWarehouseByImeis(List<String> imeis) throws Exception {
23509 amit.gupta 326
		List<InventoryItem> inventoryItems = getWarehouseClient().getInventoryItemsBySerailNumbers(imeis);
327
		Map<String, InventoryItem> imeiInventoryItemMap = new HashMap<>();
24440 amit.gupta 328
		for (InventoryItem inventoryItem : inventoryItems) {
23509 amit.gupta 329
			imeiInventoryItemMap.put(inventoryItem.getSerialNumber(), inventoryItem);
330
		}
24440 amit.gupta 331
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
23509 amit.gupta 332
		Map<String, Warehouse> serialNumberWarehouseMap = new HashMap<>();
333
		InventoryClient client = new InventoryClient();
334
		InventoryService.Client inventoryClient = client.getClient();
335
		logger.info("[imeiInventoryItemMap] {}", imeiInventoryItemMap);
24440 amit.gupta 336
		for (InventoryItem inventory : new HashSet<>(imeiInventoryItemMap.values())) {
23509 amit.gupta 337
			Warehouse phWarehouse = null;
24440 amit.gupta 338
			if (warehouseMap.containsKey(inventory.getPhysicalWarehouseId())) {
339
				phWarehouse = warehouseMap.get((int) inventory.getPhysicalWarehouseId());
23509 amit.gupta 340
			} else {
341
				phWarehouse = inventoryClient.getWarehouse(inventory.getPhysicalWarehouseId());
24440 amit.gupta 342
				warehouseMap.put((int) inventory.getPhysicalWarehouseId(), phWarehouse);
23509 amit.gupta 343
			}
344
			serialNumberWarehouseMap.put(inventory.getSerialNumber(), phWarehouse);
24440 amit.gupta 345
 
23509 amit.gupta 346
		}
347
		return serialNumberWarehouseMap;
348
	}
24440 amit.gupta 349
 
23615 amit.gupta 350
	public static Map<Integer, Warehouse> getWarehousesByIds(Set<Integer> warehouseIds) throws Exception {
351
		InventoryClient client = new InventoryClient();
352
		InventoryService.Client inventoryClient = client.getClient();
353
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
354
		for (Integer warehouseId : warehouseIds) {
355
			warehouseMap.put(warehouseId, inventoryClient.getWarehouse(warehouseId));
356
		}
357
		return warehouseMap;
358
	}
24440 amit.gupta 359
 
360
	public static String getStateCode(String stateName) throws Exception {
21902 ashik.ali 361
		return getStateInfo(stateName).getStateCode();
362
	}
24440 amit.gupta 363
 
23539 amit.gupta 364
	public static long getStateId(String stateName) throws ProfitMandiBusinessException {
365
		try {
366
			return getStateInfo(stateName).getId();
24440 amit.gupta 367
		} catch (Exception e) {
23539 amit.gupta 368
			e.printStackTrace();
369
			throw new ProfitMandiBusinessException("State Name", stateName, "Could not fetch state");
370
		}
21902 ashik.ali 371
	}
24440 amit.gupta 372
 
373
	public static StateInfo getStateByStateId(long stateId) throws Exception {
23509 amit.gupta 374
		InventoryClient client = new InventoryClient();
375
		InventoryService.Client inventoryClient = client.getClient();
376
		Map<Long, StateInfo> map = inventoryClient.getStateMaster();
377
		return map.get(stateId);
378
	}
24440 amit.gupta 379
 
380
	public static StateInfo getStateInfo(String stateName) throws Exception {
381
		try {
21894 ashik.ali 382
			InventoryClient client = new InventoryClient();
383
			InventoryService.Client inventoryClient = client.getClient();
384
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 385
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 386
			logger.info("State Name: {}", stateName);
24440 amit.gupta 387
			for (StateInfo stateInfo : stateInfos) {
22664 amit.gupta 388
				logger.info("State Name from service: {}", stateInfo.getStateName());
24440 amit.gupta 389
				if (stateName.toUpperCase().equals(stateInfo.getStateName().toUpperCase())) {
21902 ashik.ali 390
					return stateInfo;
21894 ashik.ali 391
				}
392
			}
21902 ashik.ali 393
			throw new Exception("Not found");
24440 amit.gupta 394
		} catch (Exception e) {
21894 ashik.ali 395
			e.printStackTrace();
396
			throw e;
397
		}
398
	}
24440 amit.gupta 399
 
400
	public static void main(String[] args) throws Exception {
22757 amit.gupta 401
		Utils.sendSms("Hello", "9990381569");
402
	}
23509 amit.gupta 403
 
24440 amit.gupta 404
	public static WarehouseAddress getWarehouseByWarehouseId(int warehouseAddressId) throws Exception {
23509 amit.gupta 405
		TransactionClient tcl = new TransactionClient();
24440 amit.gupta 406
		return tcl.getClient().getWarehouseAddress((long) warehouseAddressId);
23509 amit.gupta 407
	}
24440 amit.gupta 408
 
25766 amit.gupta 409
	public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
410
			String body, Map<? extends Serializable, File> map) throws Exception {
411
		MimeMessage message = mailSender.createMimeMessage();
412
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
413
		helper.setSubject(subject);
25998 amit.gupta 414
		Multipart mp = new MimeMultipart();
27285 tejbeer 415
 
25766 amit.gupta 416
		MimeBodyPart messageBodyPart = new MimeBodyPart();
27285 tejbeer 417
		messageBodyPart.setContent(body, "text/html");
25998 amit.gupta 418
		mp.addBodyPart(messageBodyPart);
27285 tejbeer 419
 
25766 amit.gupta 420
		// helper.setText(body, true);
421
		if (cc != null) {
422
			helper.setCc(cc);
423
		}
424
		helper.setTo(emailTo);
425
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
426
		helper.setFrom(senderAddress);
427
		for (Map.Entry<? extends Serializable, File> entry : map.entrySet()) {
25764 amit.gupta 428
			entry.getKey();
429
			entry.getValue();
430
			MimeBodyPart imagePart = new MimeBodyPart();
431
			imagePart.setHeader("Content-ID", entry.getKey() + "");
432
			imagePart.setDisposition(MimeBodyPart.INLINE);
433
			imagePart.attachFile(entry.getValue());
25766 amit.gupta 434
			mp.addBodyPart(imagePart);
25998 amit.gupta 435
 
25764 amit.gupta 436
		}
25768 amit.gupta 437
		message.setContent(mp);
25767 amit.gupta 438
		mailSender.send(message);
25764 amit.gupta 439
 
440
	}
27285 tejbeer 441
 
32199 amit.gupta 442
	public String html(String string) {
443
		return org.apache.commons.lang.StringEscapeUtils.escapeHtml(String.valueOf(string));
444
	}
445
 
26066 amit.gupta 446
	public String htmlJson(Object object) {
447
		return StringEscapeUtils.escapeHtml4(gson.toJson(object));
448
	}
25764 amit.gupta 449
 
26534 amit.gupta 450
	public static String getHyphenatedString(String s) {
451
		s = s.trim().replaceAll("\\s+", " ");
452
		s = s.replaceAll("\\s", "-");
453
		return s.toLowerCase();
454
	}
27285 tejbeer 455
 
26989 amit.gupta 456
	public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
457
			String body, Attachment... attachments) throws Exception {
458
		MimeMessage message = mailSender.createMimeMessage();
459
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
460
		helper.setSubject(subject);
461
		helper.setText(body);
462
		if (cc != null) {
463
			helper.setCc(cc);
464
		}
465
		helper.setTo(emailTo);
466
		for (Attachment attachment : attachments) {
467
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
468
		}
469
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
470
		helper.setFrom(senderAddress);
471
		mailSender.send(message);
472
	}
30122 amit.gupta 473
 
29552 amit.gupta 474
	public static int compareFloat(float f1, float f2) {
30122 amit.gupta 475
		if ((f1 - f2 < FLOAT_EPSILON) || (f1 - f2 < -FLOAT_EPSILON)) {
29552 amit.gupta 476
			return 0;
477
		}
478
		return Float.compare(f1, f2);
479
	}
26534 amit.gupta 480
 
31177 tejbeer 481
	public static int compareDouble(double d1, double d2) {
482
		if ((d1 - d2 < DOUBLE_EPSILON) || (d1 - d2 < -DOUBLE_EPSILON)) {
483
			return 0;
484
		}
485
		return Double.compare(d1, d2);
486
	}
487
 
30122 amit.gupta 488
	public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
489
		Set<Object> seen = ConcurrentHashMap.newKeySet();
490
		return t -> seen.add(keyExtractor.apply(t));
491
	}
492
 
31903 amit.gupta 493
	public static <T> Predicate<T> smallestByKey(Function<? super T, ?> keyExtractor) {
494
		Set<Object> seen = ConcurrentHashMap.newKeySet();
495
		return t -> seen.add(keyExtractor.apply(t));
496
	}
497
 
26084 amit.gupta 498
}