Subversion Repositories SmartDukaan

Rev

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