Subversion Repositories SmartDukaan

Rev

Rev 26638 | Rev 26821 | 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
 
3
import java.io.File;
4
import java.io.IOException;
26089 amit.gupta 5
import java.io.InputStream;
25764 amit.gupta 6
import java.io.Serializable;
26089 amit.gupta 7
import java.net.URISyntaxException;
8
import java.net.URL;
21902 ashik.ali 9
import java.util.ArrayList;
26089 amit.gupta 10
import java.util.Arrays;
21543 ashik.ali 11
import java.util.Collections;
12
import java.util.HashMap;
23509 amit.gupta 13
import java.util.HashSet;
21543 ashik.ali 14
import java.util.List;
15
import java.util.Map;
23296 ashik.ali 16
import java.util.Set;
17
import java.util.TreeSet;
21894 ashik.ali 18
 
25766 amit.gupta 19
import javax.mail.Multipart;
23909 amit.gupta 20
import javax.mail.internet.InternetAddress;
25764 amit.gupta 21
import javax.mail.internet.MimeBodyPart;
23909 amit.gupta 22
import javax.mail.internet.MimeMessage;
25766 amit.gupta 23
import javax.mail.internet.MimeMultipart;
23909 amit.gupta 24
 
21543 ashik.ali 25
import org.apache.commons.io.FileUtils;
26089 amit.gupta 26
import org.apache.commons.io.IOUtils;
26066 amit.gupta 27
import org.apache.commons.lang3.StringEscapeUtils;
26089 amit.gupta 28
import org.apache.commons.lang3.StringUtils;
29
import org.apache.http.client.utils.URIBuilder;
23929 amit.gupta 30
import org.apache.logging.log4j.LogManager;
31
import org.apache.logging.log4j.Logger;
22351 ashik.ali 32
import org.apache.thrift.TException;
33
import org.apache.thrift.transport.TTransportException;
23929 amit.gupta 34
import org.springframework.core.io.InputStreamSource;
23909 amit.gupta 35
import org.springframework.mail.javamail.JavaMailSender;
36
import org.springframework.mail.javamail.MimeMessageHelper;
23074 ashik.ali 37
 
26066 amit.gupta 38
import com.google.gson.Gson;
22215 ashik.ali 39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21894 ashik.ali 40
import com.spice.profitmandi.common.model.GstRate;
22351 ashik.ali 41
import com.spice.profitmandi.common.model.ProfitMandiConstants;
26079 amit.gupta 42
import com.spice.profitmandi.common.web.client.RestClient;
21894 ashik.ali 43
import com.spice.profitmandi.thrift.clients.CatalogClient;
44
import com.spice.profitmandi.thrift.clients.InventoryClient;
45
import com.spice.profitmandi.thrift.clients.TransactionClient;
22351 ashik.ali 46
import com.spice.profitmandi.thrift.clients.UserClient;
23509 amit.gupta 47
import com.spice.profitmandi.thrift.clients.WarehouseClient;
21894 ashik.ali 48
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
21543 ashik.ali 49
 
21894 ashik.ali 50
import in.shop2020.model.v1.catalog.CatalogService;
51
import in.shop2020.model.v1.inventory.InventoryService;
52
import in.shop2020.model.v1.inventory.StateInfo;
23509 amit.gupta 53
import in.shop2020.model.v1.inventory.Warehouse;
23884 amit.gupta 54
import in.shop2020.model.v1.order.OrderStatusGroups;
21894 ashik.ali 55
import in.shop2020.model.v1.order.RechargeOrderStatus;
56
import in.shop2020.model.v1.order.RechargePlan;
57
import in.shop2020.model.v1.order.RechargeType;
23509 amit.gupta 58
import in.shop2020.model.v1.order.SellerInfo;
59
import in.shop2020.model.v1.order.WarehouseAddress;
22351 ashik.ali 60
import in.shop2020.model.v1.user.Sex;
61
import in.shop2020.model.v1.user.User;
62
import in.shop2020.model.v1.user.UserContextException;
23509 amit.gupta 63
import in.shop2020.warehouse.InventoryItem;
64
import in.shop2020.warehouse.WarehouseService;
21543 ashik.ali 65
 
66
public class Utils {
21986 kshitij.so 67
 
23568 govind 68
	private static final Logger logger = LogManager.getLogger(Utils.class);
21986 kshitij.so 69
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
70
	public static final String PRODUCT_PROPERTIES_SNIPPET = "ProductPropertiesSnippet.html";
71
	public static final String DOCUMENT_STORE = "/profitmandi/documents/";
26066 amit.gupta 72
	private Gson gson = new Gson();
23017 ashik.ali 73
	private static final Map<Integer, String> helpMap = new HashMap<>(6);
74
	private static final Map<Integer, String> dthIdAliasMap = new HashMap<>(7);
75
	private static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<>(20);
21986 kshitij.so 76
	private static Map<Long, String> mobileProvidersMap;
77
	private static Map<Long, String> dthProvidersMap;
26673 amit.gupta 78
	private static final String SMS_GATEWAY = "https://bmg.spicedigital.in/SMSGateway/messagePush";
21986 kshitij.so 79
	private static Map<Long, String> allProviders;
23017 ashik.ali 80
	public static Map<RechargeOrderStatus, String> rechargeStatusMap = new HashMap<>();
23884 amit.gupta 81
	public static OrderStatusGroups ORDER_STATUS_GROUPS = new OrderStatusGroups();
25764 amit.gupta 82
	public static final String SYSTEM_PARTNER = "testpxps@gmail.com";
26079 amit.gupta 83
	private static final RestClient rc = new RestClient();
25764 amit.gupta 84
 
21986 kshitij.so 85
	static {
86
		helpMap.put(Integer.valueOf(1), "Your VC number starts with 0 and is 11 digits long.");
87
		helpMap.put(Integer.valueOf(2), "Smart card number starts with 2 and is 12 digits long.");
88
		helpMap.put(Integer.valueOf(3), "Smart card number starts with 4 and is 11 digits long.");
89
		helpMap.put(Integer.valueOf(4), "Subscriber ID starts with 1 and is 10 digits long.");
90
		helpMap.put(Integer.valueOf(5), "For customer ID, SMS ID to 9212012299 from your registered mobile no.");
91
		helpMap.put(Integer.valueOf(26), "Customer ID starts with 3 and is 10 digits long.");
21543 ashik.ali 92
 
21986 kshitij.so 93
		dthIdAliasMap.put(Integer.valueOf(1), "VC Number :");
94
		dthIdAliasMap.put(Integer.valueOf(2), "Smart Card Number :");
95
		dthIdAliasMap.put(Integer.valueOf(3), "Smart Card Number :");
96
		dthIdAliasMap.put(Integer.valueOf(4), "Subscriber Id :");
97
		dthIdAliasMap.put(Integer.valueOf(5), "Customer Id :");
98
		dthIdAliasMap.put(Integer.valueOf(0), "Account Number :");
99
		dthIdAliasMap.put(Integer.valueOf(26), "Customer Id :");
21543 ashik.ali 100
 
21986 kshitij.so 101
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
102
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
103
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
104
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed Refunded");
105
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
106
		rechargeStatusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
107
		rechargeStatusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
108
		rechargeStatusMap.put(RechargeOrderStatus.PAYMENT_PENDING, "Payment Failed");
109
		rechargeStatusMap.put(RechargeOrderStatus.INIT, "Payment Initiated");
110
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_UNKNOWN, "Recharge In Process");
111
		rechargeStatusMap.put(RechargeOrderStatus.RECHARGE_IN_PROCESS, "Recharge In Process");
112
 
113
		TransactionClient tcl;
24440 amit.gupta 114
		try {
21986 kshitij.so 115
			tcl = new TransactionClient();
116
			mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
117
			dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
118
			logger.info("mobileProvidersMap" + mobileProvidersMap);
119
			logger.info("dthProvidersMap" + dthProvidersMap);
22390 amit.gupta 120
			allProviders = new HashMap<>(mobileProvidersMap);
21986 kshitij.so 121
			logger.info("allProviders" + allProviders);
122
			allProviders.putAll(dthProvidersMap);
123
			logger.info("allProviders" + allProviders);
124
 
125
			for (Long operatorId : mobileProvidersMap.keySet()) {
126
				List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId.longValue());
127
				if (!plans.isEmpty())
128
					operatorPlanMap.put(operatorId, plans);
129
			}
130
		} catch (Exception e) {
131
			logger.error("Could not get providers", e);
132
		}
133
	}
134
 
23509 amit.gupta 135
	public static SellerInfo getSellerInfoBySellerId(int sellerId) throws Exception {
136
		TransactionClient tcl = new TransactionClient();
24440 amit.gupta 137
		return tcl.getClient().getSellerInfo((long) sellerId);
23509 amit.gupta 138
	}
24440 amit.gupta 139
 
21543 ashik.ali 140
	@SuppressWarnings("serial")
24440 amit.gupta 141
	public static final Map<String, String> MIME_TYPE = Collections.unmodifiableMap(new HashMap<String, String>() {
142
		{
143
			put("image/png", "png");
144
			put("image/jpeg", "jpeg");
145
			put("image/pjpeg", "jpeg");
146
			put("application/pdf", "pdf");
147
		}
148
	});
21543 ashik.ali 149
 
24440 amit.gupta 150
	private static String getExportPath() {
26534 amit.gupta 151
		String exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
152
/*		String exportPath = null;
21543 ashik.ali 153
 
24440 amit.gupta 154
		try {
26534 amit.gupta 155
			ConfigClient client = ConfigClient.getClient();
21543 ashik.ali 156
			exportPath = client.get("export_entities_path");
24440 amit.gupta 157
		} catch (Exception ce) {
21543 ashik.ali 158
			logger.error("Unable to read export path from the config client: ", ce);
159
			logger.warn("Setting the default export path");
160
			exportPath = "/var/lib/tomcat7/webapps/export/html/entities/";
26534 amit.gupta 161
		}*/
21543 ashik.ali 162
		return exportPath;
163
	}
21986 kshitij.so 164
 
21646 kshitij.so 165
	public static String getRechargeDisplayStatus(RechargeOrderStatus status) {
21986 kshitij.so 166
		if (status == null) {
167
			status = RechargeOrderStatus.INIT;
168
		}
24440 amit.gupta 169
		String displayStatus = (String) rechargeStatusMap.get(status);
21986 kshitij.so 170
		if (displayStatus == null) {
171
			return "";
172
		}
173
		return displayStatus;
174
	}
21543 ashik.ali 175
 
24440 amit.gupta 176
	public static boolean copyDocument(String documentPath) {
21543 ashik.ali 177
		File source = new File(documentPath);
24440 amit.gupta 178
		File dest = new File(DOCUMENT_STORE + source.getName());
21543 ashik.ali 179
		try {
180
			FileUtils.copyFile(source, dest);
181
		} catch (IOException e) {
182
			e.printStackTrace();
183
			return false;
184
		}
185
		return true;
186
	}
187
 
188
	public static Map<Long, String> getMobileProvidersMap() {
189
		return mobileProvidersMap;
190
	}
191
 
192
	public static Map<Long, String> getDthProvidersMap() {
193
		return dthProvidersMap;
194
	}
21986 kshitij.so 195
 
21543 ashik.ali 196
	public static Map<Long, String> getAllProviders() {
197
		return allProviders;
198
	}
21986 kshitij.so 199
 
21543 ashik.ali 200
	public static String getProvider(long operatorId) {
21986 kshitij.so 201
		return allProviders.get(operatorId);
202
	}
21543 ashik.ali 203
 
26079 amit.gupta 204
	public static void sendSms(String text, String mobileNumber) throws Exception {
26089 amit.gupta 205
		Map<String, String> paramsMap = new HashMap<>();
26673 amit.gupta 206
		paramsMap.put("username", "SmartDukaanT");
207
		paramsMap.put("password", "Smart@91");
208
		paramsMap.put("senderID", "SMTDKN");
209
		paramsMap.put("message", text);
210
		paramsMap.put("mobile", mobileNumber);
211
		paramsMap.put("messageType", "Text");
212
		rc.getResponse(SMS_GATEWAY, paramsMap, null);
21543 ashik.ali 213
	}
24440 amit.gupta 214
 
215
	public static void sendMailWithAttachments(JavaMailSender mailSender, String emailTo, String[] cc, String subject,
216
			String body, List<File> attachments) throws Exception {
23909 amit.gupta 217
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 218
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
219
		helper.setSubject(subject);
220
		helper.setText(body);
221
		if (cc != null) {
222
			helper.setCc(cc);
223
		}
224
		helper.setTo(emailTo);
225
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
226
		helper.setFrom(senderAddress);
227
		if (attachments != null) {
228
			for (File file : attachments) {
229
				helper.addAttachment(file.getName(), file);
230
			}
231
		}
232
		mailSender.send(message);
22757 amit.gupta 233
	}
25764 amit.gupta 234
 
24593 amit.gupta 235
	public static class Attachment {
236
		public Attachment(String fileName, InputStreamSource inputStreamSource) {
237
			this.fileName = fileName;
238
			this.inputStreamSource = inputStreamSource;
239
		}
25764 amit.gupta 240
 
241
		private String fileName;
24593 amit.gupta 242
		private InputStreamSource inputStreamSource;
25764 amit.gupta 243
 
24593 amit.gupta 244
		public String getFileName() {
245
			return fileName;
246
		}
25764 amit.gupta 247
 
24593 amit.gupta 248
		public void setFileName(String fileName) {
249
			this.fileName = fileName;
250
		}
25764 amit.gupta 251
 
24593 amit.gupta 252
		public InputStreamSource getInputStreamSource() {
253
			return inputStreamSource;
254
		}
25764 amit.gupta 255
 
24593 amit.gupta 256
		public void setInputStreamSource(InputStreamSource inputStreamSource) {
257
			this.inputStreamSource = inputStreamSource;
258
		}
25764 amit.gupta 259
 
24593 amit.gupta 260
		@Override
261
		public String toString() {
262
			return "Attachment [fileName=" + fileName + ", inputStreamSource=" + inputStreamSource + "]";
263
		}
264
	}
25764 amit.gupta 265
 
24440 amit.gupta 266
	public static void sendMailWithAttachment(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
267
			String body, String fileName, InputStreamSource inputStreamSource) throws Exception {
23929 amit.gupta 268
		MimeMessage message = mailSender.createMimeMessage();
24440 amit.gupta 269
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
23929 amit.gupta 270
		helper.setSubject(subject);
271
		helper.setText(body);
24440 amit.gupta 272
		if (cc != null) {
23929 amit.gupta 273
			helper.setCc(cc);
274
		}
275
		helper.setTo(emailTo);
276
		helper.addAttachment(fileName, inputStreamSource);
277
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
278
		helper.setFrom(senderAddress);
279
		mailSender.send(message);
280
	}
25764 amit.gupta 281
 
24593 amit.gupta 282
	public static void sendMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
283
			String body, Attachment... attachments) throws Exception {
284
		MimeMessage message = mailSender.createMimeMessage();
285
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
286
		helper.setSubject(subject);
287
		helper.setText(body);
288
		if (cc != null) {
289
			helper.setCc(cc);
290
		}
291
		helper.setTo(emailTo);
25764 amit.gupta 292
		for (Attachment attachment : attachments) {
24593 amit.gupta 293
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
294
		}
295
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
296
		helper.setFrom(senderAddress);
297
		mailSender.send(message);
298
	}
25764 amit.gupta 299
 
300
	public static void sendHtmlMailWithAttachments(JavaMailSender mailSender, String[] emailTo, String[] cc,
301
			String subject, String body, Attachment... attachments) throws Exception {
25743 amit.gupta 302
		MimeMessage message = mailSender.createMimeMessage();
303
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
304
		helper.setSubject(subject);
25754 amit.gupta 305
		message.setContent(body, "text/html");
25764 amit.gupta 306
		// helper.setText(body, true);
25743 amit.gupta 307
		if (cc != null) {
308
			helper.setCc(cc);
309
		}
310
		helper.setTo(emailTo);
25764 amit.gupta 311
		for (Attachment attachment : attachments) {
312
			if (attachment == null)
313
				break;
25743 amit.gupta 314
			helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
315
		}
316
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
317
		helper.setFrom(senderAddress);
318
		mailSender.send(message);
319
	}
24440 amit.gupta 320
 
321
	public static String[] getOrderStatus(RechargeOrderStatus status) {
322
		if (status == null) {
21543 ashik.ali 323
			status = RechargeOrderStatus.INIT;
324
		}
24440 amit.gupta 325
		if (status.equals(RechargeOrderStatus.PAYMENT_FAILED) || status.equals(RechargeOrderStatus.PAYMENT_PENDING)) {
326
			return new String[] { "false", "PAYMENT FAILED", "Payment failed at the payment gateway." };
327
		} else if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
328
				|| status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
329
			if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
330
				return new String[] { "false", "PAYMENT SUCCESSFUL",
331
						"Your Payment was successful but due to some internal error with the operator's system we are not sure if the recharge was successful."
332
								+ " We have put your recharge under process."
333
								+ " As soon as we get a confirmation on this transaction, we will notify you." };
334
			} else {
335
				return new String[] { "false", "RECHARGE IN PROCESS",
336
						"Your Payment is successful.We have put your recharge under process."
337
								+ " Please wait while we check with the operator." };
21543 ashik.ali 338
			}
24440 amit.gupta 339
		} else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)
340
				|| status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)) {
341
			return new String[] { "false", "RECHARGE FAILED",
342
					"Your Payment was successful but unfortunately the recharge failed.Don't worry your payment is safe with us."
343
							+ " The entire Amount has been refunded to your wallet." };
344
		} else if (status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)) {
345
			return new String[] { "false", "SUCCESS", "Congratulations! Your device is successfully recharged." };
346
		} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED)
347
				|| status.equals(RechargeOrderStatus.REFUNDED)) {
348
			return new String[] { "false", "PAYMENT REFUNDED",
349
					"The payment associated with this recharge order has been refunded." };
21543 ashik.ali 350
		} else {
24440 amit.gupta 351
			return new String[] { "true", "ERROR", "INVALID INPUT" };
21543 ashik.ali 352
		}
353
	}
21986 kshitij.so 354
 
24440 amit.gupta 355
	public static String getIconUrl(int entityId, String host, int port, String webapp) {
25743 amit.gupta 356
		return "";
21986 kshitij.so 357
	}
25764 amit.gupta 358
 
359
	public static Map<Integer, GstRate> getStateTaxRate(List<Integer> itemIds, int stateId)
360
			throws ProfitMandiBusinessException {
23152 ashik.ali 361
		List<Long> longItemIds = new ArrayList<>();
25764 amit.gupta 362
		for (int itemId : itemIds) {
23152 ashik.ali 363
			longItemIds.add(Long.valueOf(itemId));
364
		}
25764 amit.gupta 365
		try {
24444 amit.gupta 366
			CatalogService.Client catalogClient = getCatalogClient();
25764 amit.gupta 367
			Map<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateMap = catalogClient.getStateTaxRate(longItemIds,
368
					stateId);
23147 ashik.ali 369
			Map<Integer, GstRate> itemIdGstRateMap = new HashMap<>();
25764 amit.gupta 370
			for (Map.Entry<Long, in.shop2020.model.v1.catalog.GstRate> totalTaxRateEntry : totalTaxRateMap.entrySet()) {
23147 ashik.ali 371
				GstRate gstRate = new GstRate();
24444 amit.gupta 372
				gstRate.setIgstRate(Double.valueOf(totalTaxRateEntry.getValue().getIgstRate()).floatValue());
373
				gstRate.setCgstRate(Double.valueOf(totalTaxRateEntry.getValue().getCgstRate()).floatValue());
374
				gstRate.setSgstRate(Double.valueOf(totalTaxRateEntry.getValue().getSgstRate()).floatValue());
375
				itemIdGstRateMap.put(totalTaxRateEntry.getKey().intValue(), gstRate);
23147 ashik.ali 376
			}
377
			return itemIdGstRateMap;
25764 amit.gupta 378
		} catch (Exception e) {
23147 ashik.ali 379
			e.printStackTrace();
25764 amit.gupta 380
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID + ", " + ProfitMandiConstants.STATE_ID,
381
					itemIds + ", " + stateId, "THRFT_1000");
23147 ashik.ali 382
		}
383
	}
25764 amit.gupta 384
 
385
	public static Map<Integer, Float> getIgstTaxRate(List<Integer> itemIds) throws ProfitMandiBusinessException {
23152 ashik.ali 386
		List<Long> longItemIds = new ArrayList<>();
25764 amit.gupta 387
		for (int itemId : itemIds) {
23152 ashik.ali 388
			longItemIds.add(Long.valueOf(itemId));
389
		}
25764 amit.gupta 390
		try {
24444 amit.gupta 391
			CatalogService.Client catalogClient = getCatalogClient();
392
			Map<Long, Double> totalTaxRateMap = catalogClient.getIgstTaxRate(longItemIds);
393
			Map<Integer, Float> itemIdIgstRateMap = new HashMap<>();
25764 amit.gupta 394
			for (Map.Entry<Long, Double> totalTaxRateEntry : totalTaxRateMap.entrySet()) {
24444 amit.gupta 395
				itemIdIgstRateMap.put(totalTaxRateEntry.getKey().intValue(), 12f);
396
			}
397
			return itemIdIgstRateMap;
25764 amit.gupta 398
		} catch (Exception e) {
24444 amit.gupta 399
			e.printStackTrace();
400
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ITEM_ID, itemIds, "THRFT_1001");
23147 ashik.ali 401
		}
402
	}
25764 amit.gupta 403
 
404
	private static CatalogService.Client getCatalogClient() throws Exception {
405
		try {
21915 ashik.ali 406
			CatalogClient client = new CatalogClient();
407
			CatalogService.Client catalogClient = client.getClient();
408
			return catalogClient;
25764 amit.gupta 409
		} catch (Exception e) {
21915 ashik.ali 410
			throw e;
411
		}
412
	}
23509 amit.gupta 413
 
24440 amit.gupta 414
	private static WarehouseService.Client getWarehouseClient() throws Exception {
415
		try {
23509 amit.gupta 416
			WarehouseClient client = new WarehouseClient();
417
			WarehouseService.Client warehouseClient = client.getClient();
418
			return warehouseClient;
24440 amit.gupta 419
		} catch (Exception e) {
23509 amit.gupta 420
			throw e;
421
		}
422
	}
24440 amit.gupta 423
 
424
	public static Map<String, Warehouse> getWarehouseByImeis(List<String> imeis) throws Exception {
23509 amit.gupta 425
		List<InventoryItem> inventoryItems = getWarehouseClient().getInventoryItemsBySerailNumbers(imeis);
426
		Map<String, InventoryItem> imeiInventoryItemMap = new HashMap<>();
24440 amit.gupta 427
		for (InventoryItem inventoryItem : inventoryItems) {
23509 amit.gupta 428
			imeiInventoryItemMap.put(inventoryItem.getSerialNumber(), inventoryItem);
429
		}
24440 amit.gupta 430
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
23509 amit.gupta 431
		Map<String, Warehouse> serialNumberWarehouseMap = new HashMap<>();
432
		InventoryClient client = new InventoryClient();
433
		InventoryService.Client inventoryClient = client.getClient();
434
		logger.info("[imeiInventoryItemMap] {}", imeiInventoryItemMap);
24440 amit.gupta 435
		for (InventoryItem inventory : new HashSet<>(imeiInventoryItemMap.values())) {
23509 amit.gupta 436
			Warehouse phWarehouse = null;
24440 amit.gupta 437
			if (warehouseMap.containsKey(inventory.getPhysicalWarehouseId())) {
438
				phWarehouse = warehouseMap.get((int) inventory.getPhysicalWarehouseId());
23509 amit.gupta 439
			} else {
440
				phWarehouse = inventoryClient.getWarehouse(inventory.getPhysicalWarehouseId());
24440 amit.gupta 441
				warehouseMap.put((int) inventory.getPhysicalWarehouseId(), phWarehouse);
23509 amit.gupta 442
			}
443
			serialNumberWarehouseMap.put(inventory.getSerialNumber(), phWarehouse);
24440 amit.gupta 444
 
23509 amit.gupta 445
		}
446
		return serialNumberWarehouseMap;
447
	}
24440 amit.gupta 448
 
23615 amit.gupta 449
	public static Map<Integer, Warehouse> getWarehousesByIds(Set<Integer> warehouseIds) throws Exception {
450
		InventoryClient client = new InventoryClient();
451
		InventoryService.Client inventoryClient = client.getClient();
452
		Map<Integer, Warehouse> warehouseMap = new HashMap<>();
453
		for (Integer warehouseId : warehouseIds) {
454
			warehouseMap.put(warehouseId, inventoryClient.getWarehouse(warehouseId));
455
		}
456
		return warehouseMap;
457
	}
24440 amit.gupta 458
 
459
	public static String getStateCode(String stateName) throws Exception {
21902 ashik.ali 460
		return getStateInfo(stateName).getStateCode();
461
	}
24440 amit.gupta 462
 
23539 amit.gupta 463
	public static long getStateId(String stateName) throws ProfitMandiBusinessException {
464
		try {
465
			return getStateInfo(stateName).getId();
24440 amit.gupta 466
		} catch (Exception e) {
23539 amit.gupta 467
			e.printStackTrace();
468
			throw new ProfitMandiBusinessException("State Name", stateName, "Could not fetch state");
469
		}
21902 ashik.ali 470
	}
24440 amit.gupta 471
 
472
	public static StateInfo getStateByStateId(long stateId) throws Exception {
23509 amit.gupta 473
		InventoryClient client = new InventoryClient();
474
		InventoryService.Client inventoryClient = client.getClient();
475
		Map<Long, StateInfo> map = inventoryClient.getStateMaster();
476
		return map.get(stateId);
477
	}
24440 amit.gupta 478
 
479
	public static StateInfo getStateInfo(String stateName) throws Exception {
480
		try {
21894 ashik.ali 481
			InventoryClient client = new InventoryClient();
482
			InventoryService.Client inventoryClient = client.getClient();
483
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
21902 ashik.ali 484
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
22664 amit.gupta 485
			logger.info("State Name: {}", stateName);
24440 amit.gupta 486
			for (StateInfo stateInfo : stateInfos) {
22664 amit.gupta 487
				logger.info("State Name from service: {}", stateInfo.getStateName());
24440 amit.gupta 488
				if (stateName.toUpperCase().equals(stateInfo.getStateName().toUpperCase())) {
21902 ashik.ali 489
					return stateInfo;
21894 ashik.ali 490
				}
491
			}
21902 ashik.ali 492
			throw new Exception("Not found");
24440 amit.gupta 493
		} catch (Exception e) {
21894 ashik.ali 494
			e.printStackTrace();
495
			throw e;
496
		}
497
	}
24440 amit.gupta 498
 
499
	public static List<String> getAllStateNames() {
23296 ashik.ali 500
		List<String> sortedStateNames = new ArrayList<>();
24440 amit.gupta 501
		try {
23296 ashik.ali 502
			InventoryClient client = new InventoryClient();
503
			InventoryService.Client inventoryClient = client.getClient();
504
			Map<Long, StateInfo> map = inventoryClient.getStateMaster();
505
			List<StateInfo> stateInfos = new ArrayList<>(map.values());
506
			Set<String> stateNames = new TreeSet<>();
507
 
24440 amit.gupta 508
			for (StateInfo stateInfo : stateInfos) {
23296 ashik.ali 509
				stateNames.add(stateInfo.getStateName());
510
			}
511
			sortedStateNames = new ArrayList<>(stateNames);
512
			return sortedStateNames;
24440 amit.gupta 513
		} catch (Exception e) {
23296 ashik.ali 514
			e.printStackTrace();
515
			return sortedStateNames;
516
		}
517
	}
23509 amit.gupta 518
 
24440 amit.gupta 519
	public static Map<Long, StateInfo> getAllStatesMap() throws Exception {
23509 amit.gupta 520
		InventoryClient client = new InventoryClient();
521
		InventoryService.Client inventoryClient = client.getClient();
24440 amit.gupta 522
		return inventoryClient.getStateMaster();
23509 amit.gupta 523
 
524
	}
24440 amit.gupta 525
 
526
	public static User createSaholicUser(String emailId) throws ProfitMandiBusinessException {
22351 ashik.ali 527
		in.shop2020.model.v1.user.User user = new in.shop2020.model.v1.user.User();
528
		user.setEmail(emailId);
529
		user.setPassword("");
530
		user.setCommunicationEmail(emailId);
531
		user.setSex(Sex.WONT_SAY);
532
 
533
		try {
534
			UserClient userContextServiceClient = new UserClient();
535
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536
			user = userClient.createUser(user);
537
			return user;
24440 amit.gupta 538
		} catch (UserContextException ux) {
22351 ashik.ali 539
			logger.error("Unable to register user: " + ux.getMessage());
540
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
541
		} catch (TTransportException e) {
542
			logger.error("Unable to register user." + e);
543
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
24440 amit.gupta 544
		} catch (TException e) {
22351 ashik.ali 545
			logger.error("Unable to register user." + e);
546
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, emailId, "");
24440 amit.gupta 547
		}
22351 ashik.ali 548
	}
24440 amit.gupta 549
 
550
	public static void main(String[] args) throws Exception {
22757 amit.gupta 551
		Utils.sendSms("Hello", "9990381569");
552
	}
23509 amit.gupta 553
 
24440 amit.gupta 554
	public static WarehouseAddress getWarehouseByWarehouseId(int warehouseAddressId) throws Exception {
23509 amit.gupta 555
		TransactionClient tcl = new TransactionClient();
24440 amit.gupta 556
		return tcl.getClient().getWarehouseAddress((long) warehouseAddressId);
23509 amit.gupta 557
	}
24440 amit.gupta 558
 
25766 amit.gupta 559
	public static void sendEmbeddedHtmlMail(JavaMailSender mailSender, String[] emailTo, String[] cc, String subject,
560
			String body, Map<? extends Serializable, File> map) throws Exception {
561
		MimeMessage message = mailSender.createMimeMessage();
562
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
563
		helper.setSubject(subject);
25998 amit.gupta 564
		Multipart mp = new MimeMultipart();
26089 amit.gupta 565
 
25766 amit.gupta 566
		MimeBodyPart messageBodyPart = new MimeBodyPart();
26089 amit.gupta 567
        messageBodyPart.setContent(body, "text/html");
25998 amit.gupta 568
		mp.addBodyPart(messageBodyPart);
26089 amit.gupta 569
 
25766 amit.gupta 570
		// helper.setText(body, true);
571
		if (cc != null) {
572
			helper.setCc(cc);
573
		}
574
		helper.setTo(emailTo);
575
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
576
		helper.setFrom(senderAddress);
577
		for (Map.Entry<? extends Serializable, File> entry : map.entrySet()) {
25764 amit.gupta 578
			entry.getKey();
579
			entry.getValue();
580
			MimeBodyPart imagePart = new MimeBodyPart();
581
			imagePart.setHeader("Content-ID", entry.getKey() + "");
582
			imagePart.setDisposition(MimeBodyPart.INLINE);
583
			imagePart.attachFile(entry.getValue());
25766 amit.gupta 584
			mp.addBodyPart(imagePart);
25998 amit.gupta 585
 
25764 amit.gupta 586
		}
25768 amit.gupta 587
		message.setContent(mp);
25767 amit.gupta 588
		mailSender.send(message);
25764 amit.gupta 589
 
590
	}
26089 amit.gupta 591
 
26066 amit.gupta 592
	public String htmlJson(Object object) {
593
		return StringEscapeUtils.escapeHtml4(gson.toJson(object));
594
	}
26534 amit.gupta 595
 
25764 amit.gupta 596
 
26534 amit.gupta 597
	public static String getHyphenatedString(String s) {
598
		s = s.trim().replaceAll("\\s+", " ");
599
		s = s.replaceAll("\\s", "-");
600
		return s.toLowerCase();
601
	}
602
 
26084 amit.gupta 603
}