Subversion Repositories SmartDukaan

Rev

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