Subversion Repositories SmartDukaan

Rev

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