Subversion Repositories SmartDukaan

Rev

Rev 6025 | Rev 6040 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1678 rajveer 1
package in.shop2020.util;
2
 
5113 amit.gupta 3
import in.shop2020.logistics.DeliveryType;
4
import in.shop2020.logistics.LogisticsInfo;
5
import in.shop2020.logistics.LogisticsService;
4188 varun.gupt 6
import in.shop2020.metamodel.core.Entity;
4802 amit.gupta 7
import in.shop2020.metamodel.definitions.Catalog;
6025 amit.gupta 8
import in.shop2020.metamodel.definitions.Category;
4802 amit.gupta 9
import in.shop2020.metamodel.definitions.DefinitionsContainer;
4188 varun.gupt 10
import in.shop2020.metamodel.util.CreationUtils;
5945 mandeep.dh 11
import in.shop2020.model.v1.catalog.CatalogService.Client;
1678 rajveer 12
import in.shop2020.model.v1.catalog.Item;
6025 amit.gupta 13
import in.shop2020.model.v1.catalog.ItemShippingInfo;
5612 amit.gupta 14
import in.shop2020.model.v1.catalog.VoucherItemMapping;
4188 varun.gupt 15
import in.shop2020.model.v1.catalog.status;
16
import in.shop2020.model.v1.user.ItemCouponDiscount;
17
import in.shop2020.thrift.clients.CatalogClient;
5113 amit.gupta 18
import in.shop2020.thrift.clients.LogisticsClient;
4188 varun.gupt 19
import in.shop2020.thrift.clients.PromotionClient;
6027 amit.gupta 20
import in.shop2020.utils.ConfigClientKeys;
4188 varun.gupt 21
 
2130 rajveer 22
import java.io.File;
5612 amit.gupta 23
import java.io.FileInputStream;
24
import java.io.IOException;
25
import java.nio.MappedByteBuffer;
26
import java.nio.channels.FileChannel;
27
import java.nio.charset.Charset;
1678 rajveer 28
import java.util.ArrayList;
5612 amit.gupta 29
import java.util.Arrays;
4188 varun.gupt 30
import java.util.Date;
4143 varun.gupt 31
import java.util.HashMap;
1678 rajveer 32
import java.util.List;
33
import java.util.Map;
34
 
4202 varun.gupt 35
import org.apache.commons.lang.StringEscapeUtils;
1678 rajveer 36
import org.apache.commons.lang.StringUtils;
4143 varun.gupt 37
import org.json.JSONObject;
1678 rajveer 38
 
39
/**
40
 * Command line utility to generate xml file for partners
41
 * 
42
 * @author rajveer
43
 *
44
 */
45
public class ProductListGenerator {
46
 
47
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
5612 amit.gupta 48
	private Client cc = null;
6025 amit.gupta 49
	List <Long> catalogItemIds = new ArrayList<Long>();
2367 rajveer 50
	public Map<Long, List<Item>> entityIdItemMap;
5113 amit.gupta 51
	private static final String DEFAULT_PINCODE = "110001";
4802 amit.gupta 52
	DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
6025 amit.gupta 53
	private List<Long> validParentCategories = Arrays.asList((long)Utils.LAPTOPS_CATEGORY, (long)Utils.CAMERAS_CATEGORY, (long)Utils.MOBILE_PHONES_CATAGORY, (long)Utils.TABLETS_CATEGORY);
5612 amit.gupta 54
	private List<Long> voucherEntities = new ArrayList<Long>();
1678 rajveer 55
 
2367 rajveer 56
	public ProductListGenerator(Map<Long, List<Item>> entityIdItemMap) throws Exception {
57
		this.entityIdItemMap = entityIdItemMap;
5612 amit.gupta 58
		String vouchers = readFile(Utils.CONTENT_DB_PATH + "voucher-entities.txt");
59
		for (String voucher : vouchers.split(",")){
60
			voucherEntities.add(Long.parseLong(voucher.trim()));
61
		}
1678 rajveer 62
	}
63
 
64
 
65
	/**
2367 rajveer 66
	 * Get the url of the entity
1678 rajveer 67
	 * @param expEntity
68
	 * @return url
69
	 */
2367 rajveer 70
	private String getProductURL(long entityId, Item item){
4802 amit.gupta 71
		//long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
72
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
73
 
74
		String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
2367 rajveer 75
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
76
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
77
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
78
        + "-" + entityId;
1678 rajveer 79
		productUrl = productUrl.replaceAll("/", "-");
80
		url = url + productUrl;
2367 rajveer 81
		url = url.replaceAll("-+", "-");
1678 rajveer 82
		return url;
83
	}
84
 
2367 rajveer 85
 
86
 
1678 rajveer 87
	/**
3964 rajveer 88
	 * Get the minimum mrp of the items
89
	 * @param items
90
	 * @return
91
	 */
92
	private double getMinMRP(List<Item> items){
93
        double minPrice = Double.MAX_VALUE;
94
        for(Item item: items){
95
            if(minPrice > item.getMrp()){
96
                minPrice =  item.getMrp();
97
            }
98
        }
99
        return minPrice;
100
    }
101
 
102
 
103
 
104
	/**
2367 rajveer 105
	 * Get the minimum price of the items
106
	 * @param items
107
	 * @return
108
	 */
109
	private double getMinPrice(List<Item> items){
110
        double minPrice = Double.MAX_VALUE;
5612 amit.gupta 111
        Item it = null;
2367 rajveer 112
        for(Item item: items){
113
            if(minPrice > item.getSellingPrice()){
114
                minPrice = item.getSellingPrice();
5612 amit.gupta 115
                it = item;
2367 rajveer 116
            }
117
        }
5612 amit.gupta 118
        if(it != null && voucherEntities.contains(it.getCatalogItemId())) {
119
    		try {
120
				cc = new CatalogClient().getClient();
121
				List<VoucherItemMapping> mapping;
122
				mapping = cc.getAllItemVouchers(it.getId());
123
				for (VoucherItemMapping vim : mapping) {
124
					minPrice = minPrice - vim.getAmount();
125
				}
126
			} catch (Exception e) {
127
				// TODO Auto-generated catch block
128
				e.printStackTrace();
129
			}
130
        }
2367 rajveer 131
        return minPrice;
132
    }
133
 
134
	/**
135
	 * Check whether product is mobile or not
136
	 * @param item
137
	 * @return
138
	 */
139
	private boolean isMobile(Item item){
4802 amit.gupta 140
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
141
		return parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY;
2367 rajveer 142
	}
143
 
144
	/**
5229 varun.gupt 145
	 * Checks whether a product is tablet or not
146
	 */
147
	private boolean isTablet(Item item) {
148
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
149
		return parentCategory.getID() == Utils.TABLETS_CATEGORY;
150
	}
5935 amit.gupta 151
 
152
	/**
153
	 * Checks whether a product is camera or not
154
	 */
155
	private boolean isCamera(Item item) {
156
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
157
		return parentCategory.getID() == Utils.CAMERAS_CATEGORY;
158
	}
5229 varun.gupt 159
 
160
	/**
4143 varun.gupt 161
	 * Checks whether a product is laptop or not
162
	 */
163
	private boolean isLaptop(Item item) {
4802 amit.gupta 164
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
165
		return parentCategory.getID() == Utils.LAPTOPS_CATEGORY;
4143 varun.gupt 166
	}
167
 
168
	/**
5355 varun.gupt 169
	 * Checks whether a product is accessory or not
170
	 */
171
	private boolean isAccessory(Item item)	{
172
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
173
		return parentCategory.getID() == Utils.MOBILE_ACCESSORIES_CATEGORY || parentCategory.getID() == Utils.LAPTOP_ACCESSORIES_CATEGORY;
174
	}
175
 
176
	/**
2367 rajveer 177
	 * 
178
	 * @param item
179
	 * @return
180
	 */
181
	private String getProductTitle(Item item){
2542 rajveer 182
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
183
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
184
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
2561 rajveer 185
		title = title.replaceAll("  ", " ");
2367 rajveer 186
		return title;
187
	}
188
 
189
	/**
1678 rajveer 190
	 * get xml feed for partner sites
191
	 * @param irDataXMLSnippets
192
	 * @throws Exception
193
	 */
2367 rajveer 194
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
5113 amit.gupta 195
		LogisticsService.Client prod_client = null;
5350 varun.gupt 196
		Map<Long, ItemCouponDiscount> couponsAndDiscounts = new HashMap<Long, ItemCouponDiscount>();
197
 
5113 amit.gupta 198
		try {
199
			LogisticsClient logistics_prod = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
200
			prod_client = logistics_prod.getClient();
5350 varun.gupt 201
 
202
			List<Long> itemIds = new ArrayList<Long>();
203
 
204
			for(Long entityId: entityIdItemMap.keySet()){
205
 
206
				List<Item> items = entityIdItemMap.get(entityId);
207
				Item firstItem = items.get(0);
208
 
209
				if(isMobile(firstItem) || isLaptop(firstItem)) {
210
					itemIds.add(firstItem.getId());
211
				}
212
			}
213
 
214
			PromotionClient promotionClient = new PromotionClient();
215
			in.shop2020.model.v1.user.PromotionService.Client promotionServiceClient = promotionClient.getClient();
216
 
217
			List<ItemCouponDiscount> itemsCouponsAndDiscounts = promotionServiceClient.getItemDiscountMap(itemIds);
218
 
219
			for (ItemCouponDiscount itemCouponDiscount: itemsCouponsAndDiscounts) {
220
				couponsAndDiscounts.put(itemCouponDiscount.getItemId(), itemCouponDiscount);
221
			}
222
 
5113 amit.gupta 223
		} catch (Exception e) {
224
			prod_client = null;
5350 varun.gupt 225
			Utils.info("Logistics estimations can't be fetched as Logistics Service is inaccessible" + e);
5113 amit.gupta 226
		}
5350 varun.gupt 227
 
2367 rajveer 228
		for(Long entityId: entityIdItemMap.keySet()){
229
			List<Item> items = entityIdItemMap.get(entityId);
230
			Item firstItem = items.get(0);
5229 varun.gupt 231
 
232
			if(!isMobile(firstItem) && !isTablet(firstItem))	{
1678 rajveer 233
				continue;
234
			}
235
 
236
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
237
 
2367 rajveer 238
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
1678 rajveer 239
 
2367 rajveer 240
			String title = getProductTitle(firstItem);
1678 rajveer 241
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
242
 
2367 rajveer 243
			String url = getProductURL(entityId, firstItem);
3929 mandeep.dh 244
 
245
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
246
			                  "website" + File.separator + entityId + File.separator + "icon.jpg";
247
 
1678 rajveer 248
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
6025 amit.gupta 249
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<Availability>" + (catalogItemIds.contains(entityId) ? "Y" : "N") + "</Availability>");
5350 varun.gupt 250
			double minPrice = getMinPrice(items);
1678 rajveer 251
 
5350 varun.gupt 252
			if(couponsAndDiscounts.containsKey(firstItem.getId()))	{
253
 
254
				int discountedPrice = (int)(minPrice - couponsAndDiscounts.get(firstItem.getId()).getDiscount());
255
				irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + discountedPrice + "</ProductPrice>");
256
 
257
			} else	{
258
				irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + minPrice + "</ProductPrice>");
259
			}
260
 
3964 rajveer 261
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
262
 
5113 amit.gupta 263
			LogisticsInfo logisticsInfo = null;
264
			if(prod_client != null){
265
				try {
266
					Long itemId = prod_client.getEntityLogisticsEstimation(
267
							entityId, DEFAULT_PINCODE, DeliveryType.PREPAID)
268
							.get(0);
269
					logisticsInfo = prod_client.getLogisticsEstimation(itemId,
270
							DEFAULT_PINCODE, DeliveryType.PREPAID);
271
					irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductDeliveryEstimate>" + logisticsInfo.getDeliveryTime() + "</ProductDeliveryEstimate>");
272
				} catch (Exception e1) {
273
					Utils.info("Unable to get Estimation for Entity: " + entityId + "\n" + e1);
274
				}
275
			}
2130 rajveer 276
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
277
 
1678 rajveer 278
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
279
		}
280
	}
281
 
282
	/**
283
	 * Generate the xml list of all the active phones and store in a file
284
	 * @throws Exception
285
	 */
2227 rajveer 286
	public void generateProductsListXML() throws Exception {
1678 rajveer 287
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
288
		productXMLSnippets.add("<saholic.com>");
289
 
290
		getProductsXML(productXMLSnippets);
291
 
292
		productXMLSnippets.add("</saholic.com>");
293
 
294
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
295
 
296
		Utils.info(productDataXML);
297
 
298
		// Write it to file
2130 rajveer 299
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 300
		DBUtils.store(productDataXML, productXMLFilename);
301
	}
2227 rajveer 302
 
5355 varun.gupt 303
	/**
304
	 * Generate the xml list of all the active accessories and store in a file
305
	 * @throws Exception
306
	 */
307
	public void generateAccessoriesXML() throws Exception	{
308
		LogisticsService.Client prod_client = null;
309
 
310
		try	{
311
			LogisticsClient logistics_prod = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
312
			prod_client = logistics_prod.getClient();
313
 
314
		} catch (Exception e) {
315
			prod_client = null;
316
			Utils.info("Logistics estimations can't be fetched as Logistics Service is inaccessible" + e);
317
		}
318
		List<String> accessoriesXMLSnippets = new ArrayList<String>();
319
 
320
		accessoriesXMLSnippets.add("<saholic.com>");
321
 
322
		for(Long entityId: entityIdItemMap.keySet())	{
323
 
324
			List<Item> items = entityIdItemMap.get(entityId);
325
			Item firstItem = items.get(0);
326
 
327
			if(! isAccessory(firstItem))	{
328
				continue;
329
			}
330
			String title = getProductTitle(firstItem);
331
			String url = getProductURL(entityId, firstItem);
332
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
333
            "website" + File.separator + entityId + File.separator + "icon.jpg";
334
 
335
			accessoriesXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
336
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
5362 varun.gupt 337
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + StringEscapeUtils.escapeXml(title) + "</ProductName>");
5363 varun.gupt 338
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + StringEscapeUtils.escapeXml(url) + "</ProductURL>");
5355 varun.gupt 339
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
340
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
341
 
342
			LogisticsInfo logisticsInfo = null;
343
			if(prod_client != null){
344
				try {
345
					Long itemId = prod_client.getEntityLogisticsEstimation(entityId, DEFAULT_PINCODE, DeliveryType.PREPAID).get(0);
346
					logisticsInfo = prod_client.getLogisticsEstimation(itemId, DEFAULT_PINCODE, DeliveryType.PREPAID);
347
					accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductDeliveryEstimate>" + logisticsInfo.getDeliveryTime() + "</ProductDeliveryEstimate>");
348
				} catch (Exception e1) {
349
					Utils.info("Unable to get Estimation for Entity: " + entityId + "\n" + e1);
350
				}
351
			}
352
			accessoriesXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
353
			accessoriesXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
354
		}
355
 
356
		accessoriesXMLSnippets.add("</saholic.com>");
2227 rajveer 357
 
5355 varun.gupt 358
		String accessoriesXML = StringUtils.join(accessoriesXMLSnippets, "\n");
359
 
360
		Utils.info(accessoriesXML);
361
 
362
		// Write it to file
363
		String accessoriesXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicaccessories.xml";
364
		DBUtils.store(accessoriesXML, accessoriesXMLFilename);
365
	}
2227 rajveer 366
 
5935 amit.gupta 367
 
368
 
2227 rajveer 369
	/**
5935 amit.gupta 370
	 * Generate the xml list of all the active cameras and store in a file
371
	 * @throws Exception
372
	 */
373
	public void generateCamerasXML() throws Exception	{
374
		LogisticsService.Client prod_client = null;
375
 
376
		try	{
377
			LogisticsClient logistics_prod = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
378
			prod_client = logistics_prod.getClient();
379
 
380
		} catch (Exception e) {
381
			prod_client = null;
382
			Utils.info("Logistics estimations can't be fetched as Logistics Service is inaccessible" + e);
383
		}
384
		List<String> camerasXMLSnippets = new ArrayList<String>();
385
 
386
		camerasXMLSnippets.add("<saholic.com>");
387
 
388
		for(Long entityId: entityIdItemMap.keySet())	{
389
 
390
			List<Item> items = entityIdItemMap.get(entityId);
391
			Item firstItem = items.get(0);
392
 
393
			if(! isCamera(firstItem))	{
394
				continue;
395
			}
396
			String title = getProductTitle(firstItem);
397
			String url = getProductURL(entityId, firstItem);
398
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
399
			"website" + File.separator + entityId + File.separator + "icon.jpg";
400
 
401
			camerasXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
402
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
403
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + StringEscapeUtils.escapeXml(title) + "</ProductName>");
404
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + StringEscapeUtils.escapeXml(url) + "</ProductURL>");
405
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
406
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
6025 amit.gupta 407
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<Availability>" + (catalogItemIds.contains(entityId) ? "Y" : "N") + "</Availability>");
5935 amit.gupta 408
 
409
			LogisticsInfo logisticsInfo = null;
410
			if(prod_client != null){
411
				try {
412
					Long itemId = prod_client.getEntityLogisticsEstimation(entityId, DEFAULT_PINCODE, DeliveryType.PREPAID).get(0);
413
					logisticsInfo = prod_client.getLogisticsEstimation(itemId, DEFAULT_PINCODE, DeliveryType.PREPAID);
414
					camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductDeliveryEstimate>" + logisticsInfo.getDeliveryTime() + "</ProductDeliveryEstimate>");
415
				} catch (Exception e1) {
416
					Utils.info("Unable to get Estimation for Entity: " + entityId + "\n" + e1);
417
				}
418
			}
419
			camerasXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
420
			camerasXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
421
		}
422
 
423
		camerasXMLSnippets.add("</saholic.com>");
424
 
425
		String camerasXML = StringUtils.join(camerasXMLSnippets, "\n");
426
 
427
		Utils.info(camerasXML);
428
 
429
		// Write it to file
430
		String accessoriesXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholiccameras.xml";
431
		DBUtils.store(camerasXML, accessoriesXMLFilename);
432
	}
433
 
434
	/**
2227 rajveer 435
	 * get xml feed for partner sites
436
	 * @param irDataXMLSnippets
437
	 * @throws Exception
438
	 */
439
	private void getProductsJavascript(StringBuffer sb) throws Exception{
4143 varun.gupt 440
 
5347 amit.gupta 441
		Map<String, Map<String, Long>> products = new HashMap<String, Map<String,Long>>();
2367 rajveer 442
 
5347 amit.gupta 443
		for(Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet())	{
444
			Item item = entry.getValue().get(0);
5930 amit.gupta 445
			in.shop2020.metamodel.definitions.Category category = defContainer.getCategory(item.getCategory());
446
			in.shop2020.metamodel.definitions.Category parentCategory = category.getParentCategory();
447
			String categoryLabel = category.getLabel();
5347 amit.gupta 448
			if(parentCategory.isComparable()){
5930 amit.gupta 449
				categoryLabel = parentCategory.getLabel();
2227 rajveer 450
			}
5930 amit.gupta 451
			if(!products.containsKey(categoryLabel)){
452
				Map<String, Long> catMap = new HashMap<String, Long>();
453
				products.put(categoryLabel, catMap);
454
			}
455
			products.get(categoryLabel).put(getProductTitle(item), entry.getKey());
2227 rajveer 456
		}
4143 varun.gupt 457
		sb.append(new JSONObject(products));
2227 rajveer 458
	}
459
 
460
 
461
	/**
462
	 * Generate the javascript list of all the active phones and store in a file
463
	 * @throws Exception
464
	 */
465
	public void generateProductListJavascript() throws Exception {
466
		StringBuffer productsJavascript = new StringBuffer();
467
 
4143 varun.gupt 468
		productsJavascript.append("var productIdNames=");
2227 rajveer 469
 
470
		getProductsJavascript(productsJavascript);
471
 
4143 varun.gupt 472
		productsJavascript.append(";");
2227 rajveer 473
 
474
		// Write it to file
475
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
476
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
477
	}
4188 varun.gupt 478
 
479
    private void populateEntityIdItemMap(List<Item> items){
480
        Date toDate = new Date();
481
        for(Item item: items){
482
            //TODO Can be removed as we are checking in calling function
483
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
484
                continue;
485
            }
486
            if(toDate.getTime() < item.getStartDate() ||  item.getSellingPrice() == 0){
487
                continue;
488
            }
489
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
490
            if(itemList == null){
491
                itemList = new ArrayList<Item>();
492
            }
493
            itemList.add(item);
494
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
495
        }
496
 
497
        //Remove all items which have not been updated since last content generation.
498
        List<Long> removeEntities = new ArrayList<Long>();
499
        for(Long entityId:entityIdItemMap.keySet()){
500
            boolean isValidEntity = false;
501
            //If any one of the items has been updated before current timestamp, than we generate content for whole entity
502
            for(Item item: entityIdItemMap.get(entityId)){
503
                if(item.getUpdatedOn() > 0){
504
                    isValidEntity = true;
505
                }
506
            }
507
            if(!isValidEntity){
508
                removeEntities.add(entityId);
509
            }
510
        }
511
        for(Long entityId: removeEntities){
512
            entityIdItemMap.remove(entityId);
513
        }
514
    }
5229 varun.gupt 515
 
516
    /**
517
     * Generate XML feed for mobile site
518
     * @throws Exception
519
     */
520
    public void generateXMLFeedForMobileSite() throws Exception	{
521
    	List<String> productXMLSnippets = new ArrayList<String>();
522
		productXMLSnippets.add("<Products>");
523
		List<Long> itemIds = new ArrayList<Long>();
524
 
525
		for(Long entityId: entityIdItemMap.keySet()){
526
			List<Item> items = entityIdItemMap.get(entityId);
527
			Item firstItem = items.get(0);
528
			Utils.info("first Item: " + firstItem);
529
 
530
			if(isMobile(firstItem) || isLaptop(firstItem)) {
531
				itemIds.add(firstItem.getId());
532
			}
533
		}
4188 varun.gupt 534
 
5229 varun.gupt 535
		List<String> descriptions = new ArrayList<String>();
536
		descriptions.add("8MP camera, Super AMOLED Plus display, Android Gingerbread");
537
		descriptions.add("Android OS v2.2 Froyo, 800MHz processor, 3.5\" capacitive display");
538
		descriptions.add("Dual-SIM, Wi-Fi, Bluetooth, Social networking &amp; IM");
539
		descriptions.add("Android Gingerbread, Facebook button, 5MP camera");
540
		descriptions.add("Android Gingerbread, 1GHz processor, 3.7\" Gorilla Glass display");
541
		descriptions.add("2GB RAM, 500GB HDD, Intel Core 2 Duo T6570 processor, DOS");
542
		descriptions.add("4GB RAM, 500GB HDD, Intel Core i5 2410M processor, Windows 7 Basic");
543
		descriptions.add("3.2\" touchscreen, 2MP camera, IM &amp; social networking");
544
 
545
		for(Long entityId: entityIdItemMap.keySet()) {
546
			List<Item> items = entityIdItemMap.get(entityId);
547
			Item firstItem = items.get(0);
548
 
549
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
550
				continue;
551
			}
4188 varun.gupt 552
 
5229 varun.gupt 553
			String url = getProductURL(entityId, firstItem);
554
			String imageUrl = "http://www.saholic.com/images/website" + File.separator + entityId + File.separator + "thumbnail.jpg";
555
			String description = descriptions.get((int) (8 * Math.random()));
556
			double minPrice = getMinPrice(items);
557
 
558
			String productType = "";
559
 
560
			if (firstItem.getCategory() == 10001 || firstItem.getCategory() == 10002 || firstItem.getCategory() == 10003 || firstItem.getCategory() == 10004 || firstItem.getCategory() == 10005)	{
561
				productType = "Mobile Phone";
562
 
563
			} else if (firstItem.getCategory() == 10010)	{
564
				productType = "Tablet";
565
 
566
			} else if (firstItem.getCategory() == 10050)	{
567
				productType = "Laptop";
568
			}
569
 
570
			productXMLSnippets.add(this.xmlIndentation[1] + "<Product>");	
571
 
572
			productXMLSnippets.add(this.xmlIndentation[2] + "<ID>" + entityId + "</ID>");
573
			productXMLSnippets.add(this.xmlIndentation[2] + "<Type>" + productType + "</Type>");
574
			productXMLSnippets.add(this.xmlIndentation[2] + "<Brand>" + firstItem.getBrand() + "</Brand>");
575
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelName>" + firstItem.getModelName() + "</ModelName>");
576
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelNumber>" + firstItem.getModelNumber() + "</ModelNumber>");
577
			productXMLSnippets.add(this.xmlIndentation[2] + "<URL>" + url + "</URL>");
578
			productXMLSnippets.add(this.xmlIndentation[2] + "<ImageURL>" + imageUrl + "</ImageURL>");
579
			productXMLSnippets.add(this.xmlIndentation[2] + "<ShortDesc>" + description + "</ShortDesc>");
580
			productXMLSnippets.add(this.xmlIndentation[2] + "<MRP>" + firstItem.getMrp() + "</MRP>");
581
			productXMLSnippets.add(this.xmlIndentation[2] + "<SellingPrice>" + (int)minPrice  + "</SellingPrice>");
582
 
583
			productXMLSnippets.add(this.xmlIndentation[1] + "</Product>");
584
		}
585
 
586
		productXMLSnippets.add("</Products>");
587
 
588
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
589
 
590
		Utils.info(productDataXML);
591
 
592
		// Write it to file
593
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "msitedata.xml";
594
		DBUtils.store(productDataXML, productXMLFilename);
595
    }
596
 
4188 varun.gupt 597
	/**
598
	 * Generate the xml list of all the active phones and store in a file
599
	 * @throws Exception
600
	 */
601
	public void generateProductXMLForDisplayAds() throws Exception {
4384 varun.gupt 602
		Utils.info("Generating Product XML for display ads");
4188 varun.gupt 603
		List<String> productXMLSnippets = new ArrayList<String>();
604
		productXMLSnippets.add("<saholic.com>");
605
		List<Long> itemIds = new ArrayList<Long>();
4384 varun.gupt 606
 
607
		Utils.info("Entity Ids: " + entityIdItemMap.keySet());
4188 varun.gupt 608
 
609
		for(Long entityId: entityIdItemMap.keySet()){
610
			List<Item> items = entityIdItemMap.get(entityId);
611
			Item firstItem = items.get(0);
4384 varun.gupt 612
			Utils.info("first Item: " + firstItem);
613
 
4383 varun.gupt 614
			if(isMobile(firstItem) || isLaptop(firstItem)) {
4188 varun.gupt 615
				itemIds.add(firstItem.getId());
616
			}
617
		}
618
 
619
		PromotionClient promotionClient = new PromotionClient();
620
		in.shop2020.model.v1.user.PromotionService.Client promotionServiceClient = promotionClient.getClient();
621
 
622
		List<ItemCouponDiscount> itemsCouponsAndDiscounts = promotionServiceClient.getItemDiscountMap(itemIds);
623
 
624
		Map<Long, ItemCouponDiscount> couponsAndDiscounts = new HashMap<Long, ItemCouponDiscount>();
625
 
626
		for (ItemCouponDiscount itemCouponDiscount: itemsCouponsAndDiscounts) {
627
			couponsAndDiscounts.put(itemCouponDiscount.getItemId(), itemCouponDiscount);
628
		}
4384 varun.gupt 629
		Utils.info("Entity IDs: " + entityIdItemMap.keySet());
4188 varun.gupt 630
 
631
		for(Long entityId: entityIdItemMap.keySet()) {
632
			List<Item> items = entityIdItemMap.get(entityId);
633
			Item firstItem = items.get(0);
4384 varun.gupt 634
 
635
			Utils.info("First Item: " + firstItem);
636
 
4382 varun.gupt 637
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
4188 varun.gupt 638
				continue;
639
			}
640
 
641
			productXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
642
 
643
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
644
 
645
			String title = getProductTitle(firstItem);
646
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
647
 
648
			String url = getProductURL(entityId, firstItem);
649
 
650
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
651
 
652
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
653
 
654
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
655
 
656
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + firstItem.getMrp() + "</ProductMRP>");
657
 
658
			double minPrice = getMinPrice(items);
659
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSellingPrice>" + (int)minPrice  + "</ProductSellingPrice>");
6025 amit.gupta 660
			productXMLSnippets.add(this.xmlIndentation[2] + "<Availability>" + (catalogItemIds.contains(entityId) ? "Y" : "N") + "</Availability>");
4188 varun.gupt 661
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDiscount>" + (int)((firstItem.getMrp()-minPrice)/firstItem.getMrp()*100) + "</ProductDiscount>");
662
 
663
			long itemId = firstItem.getId();
664
 
665
			if(couponsAndDiscounts.containsKey(itemId))	{
666
 
667
				ItemCouponDiscount itemCouponDiscount = couponsAndDiscounts.get(itemId);
668
 
669
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons>");
670
				productXMLSnippets.add(this.xmlIndentation[3] + "<Coupon>");
671
				productXMLSnippets.add(this.xmlIndentation[4] + "<CouponCode>" + itemCouponDiscount.getCouponCode() + "</CouponCode>");
672
				productXMLSnippets.add(this.xmlIndentation[4] + "<PriceAfterCoupon>" + (int)(minPrice - itemCouponDiscount.getDiscount()) + "</PriceAfterCoupon>");
673
				productXMLSnippets.add(this.xmlIndentation[3] + "</Coupon>");
674
				productXMLSnippets.add(this.xmlIndentation[2] + "</ProductCoupons>");
675
 
676
			} else	{
677
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons />");
678
			}
679
 
680
			String description = "";
681
			Entity entity = CreationUtils.getEntity(entityId);
682
			if(entity!=null){
683
				if(entity.getSlide(130001) !=null){
4202 varun.gupt 684
					description = StringEscapeUtils.escapeXml(entity.getSlide(130001).getFreeformContent().getFreeformText());
4188 varun.gupt 685
				}
686
			}
687
 
688
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDescription>" + description + "</ProductDescription>");
689
 
690
			productXMLSnippets.add(this.xmlIndentation[1] + "</products>");	
691
		}
692
 
693
		productXMLSnippets.add("</saholic.com>");
694
 
695
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
696
 
697
		Utils.info(productDataXML);
698
 
699
		// Write it to file
700
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "advertismentapi.xml";
701
		DBUtils.store(productDataXML, productXMLFilename);
702
	}
5229 varun.gupt 703
 
704
	public static void main(String[] args) throws Exception {
705
		CatalogClient cl = new CatalogClient();
5945 mandeep.dh 706
		in.shop2020.model.v1.catalog.CatalogService.Client client = cl.getClient();
5229 varun.gupt 707
 
5612 amit.gupta 708
//		List<Item> items = cl.getClient().getAllItemsByStatus(status.ACTIVE);
709
		List<Long> items1 = Arrays.asList(1587l,2222l,2258l,2270l,5836l,6718l, 6719l);
710
		List<Item> items = new ArrayList<Item>();
711
		for (Long l : items1) {
712
			items.add(cl.getClient().getItem(l));
713
		}
714
		//items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED));
5229 varun.gupt 715
//		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED_BY_RISK));
716
 
717
		Map<Long, List<Item>> entityIdItemMap1 = new HashMap<Long, List<Item>>();
718
		ProductListGenerator pl = new ProductListGenerator(entityIdItemMap1);
6025 amit.gupta 719
		//pl.generateThinkDigitFeed();
5229 varun.gupt 720
		pl.populateEntityIdItemMap(items);
5612 amit.gupta 721
		//pl.generateAccessoriesXML();
6025 amit.gupta 722
		//pl.generateXMLFeedForMobileSite();
5229 varun.gupt 723
	}
5612 amit.gupta 724
 
725
    private String readFile(String path) throws IOException {
726
		FileInputStream stream = new FileInputStream(new File(path));
727
		try {
728
			FileChannel fc = stream.getChannel();
729
			MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()-1);
730
			/* Instead of using default, pass in a decoder. */
731
			return Charset.defaultCharset().decode(bb).toString();
732
		}
733
		finally {
734
			stream.close();
735
		}
736
	}
6025 amit.gupta 737
    /**
738
     * Feed generated for thinkdigit that only contains the live items
739
     * feed should have s
740
     */
741
    public void generateThinkDigitFeed() throws Exception{
742
 
743
    	List<String> productXMLSnippets = new ArrayList<String>();
744
    	productXMLSnippets.add("<root>");
745
    	String thinkDigitFeedFileName = Utils.EXPORT_PARTNERS_CONTENT_PATH + "all-categories.xml";
746
    	cc = new CatalogClient().getClient();
747
    	List<Item> items = cc.getAllItemsByStatus(status.ACTIVE);
748
    	Client catalogClientProd = new CatalogClient(ConfigClientKeys.catalog_service_server_host_prod.toString(), ConfigClientKeys.catalog_service_server_port.toString()).getClient();
749
    	for (Item item : items){
750
    		Category parentCategory = Catalog.getInstance().getDefinitionsContainer().getCategory(item.getCategory()).getParentCategory(); 
751
    		boolean isActive = true;
752
    		if(validParentCategories.contains(parentCategory.getID()) && !catalogItemIds.contains(item.getCatalogItemId())) {
753
    			if(item.isRisky()){
754
    				try {
755
    					ItemShippingInfo isi = catalogClientProd.isActive(item.getId());
756
    					isActive = isi.isIsActive();
757
    				} catch (Exception e) {
758
    					isActive = true;
759
    				}
760
    			}
761
    			if(isActive) {
762
	    			productXMLSnippets.add(this.xmlIndentation[1] + "<products>");
763
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<id>" + item.getCatalogItemId() + "</id>");
764
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<product>" + getProductTitle(item) + "</product>");
765
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<Product_URL>" + getProductURL(item.getCatalogItemId(), item) + "</Product_URL>");
766
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<Product_Price>" + (int)item.getSellingPrice() + "</Product_Price>");
767
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<Merchant_Name>" + "saholic.com" + "</Merchant_Name>");
768
	    			productXMLSnippets.add(this.xmlIndentation[2] + "<Category_Name>" + parentCategory.getLabel() + "</Category_Name>");
769
	    			productXMLSnippets.add(this.xmlIndentation[1] + "</products>");
770
	    			catalogItemIds.add(item.getCatalogItemId());
771
    			}
772
    		}
773
    	}
774
    	productXMLSnippets.add("</root>");
775
    	String productDataXML = StringUtils.join(productXMLSnippets, "\n");
776
    	DBUtils.store(productDataXML, thinkDigitFeedFileName);
777
 
778
    }
4143 varun.gupt 779
}