Subversion Repositories SmartDukaan

Rev

Rev 5612 | Rev 5935 | 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);
5930 amit.gupta 370
			in.shop2020.metamodel.definitions.Category category = defContainer.getCategory(item.getCategory());
371
			in.shop2020.metamodel.definitions.Category parentCategory = category.getParentCategory();
372
			String categoryLabel = category.getLabel();
5347 amit.gupta 373
			if(parentCategory.isComparable()){
5930 amit.gupta 374
				categoryLabel = parentCategory.getLabel();
2227 rajveer 375
			}
5930 amit.gupta 376
			if(!products.containsKey(categoryLabel)){
377
				Map<String, Long> catMap = new HashMap<String, Long>();
378
				products.put(categoryLabel, catMap);
379
			}
380
			products.get(categoryLabel).put(getProductTitle(item), entry.getKey());
2227 rajveer 381
		}
4143 varun.gupt 382
		sb.append(new JSONObject(products));
2227 rajveer 383
	}
384
 
385
 
386
	/**
387
	 * Generate the javascript list of all the active phones and store in a file
388
	 * @throws Exception
389
	 */
390
	public void generateProductListJavascript() throws Exception {
391
		StringBuffer productsJavascript = new StringBuffer();
392
 
4143 varun.gupt 393
		productsJavascript.append("var productIdNames=");
2227 rajveer 394
 
395
		getProductsJavascript(productsJavascript);
396
 
4143 varun.gupt 397
		productsJavascript.append(";");
2227 rajveer 398
 
399
		// Write it to file
400
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
401
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
402
	}
4188 varun.gupt 403
 
404
    private void populateEntityIdItemMap(List<Item> items){
405
        Date toDate = new Date();
406
        for(Item item: items){
407
            //TODO Can be removed as we are checking in calling function
408
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
409
                continue;
410
            }
411
            if(toDate.getTime() < item.getStartDate() ||  item.getSellingPrice() == 0){
412
                continue;
413
            }
414
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
415
            if(itemList == null){
416
                itemList = new ArrayList<Item>();
417
            }
418
            itemList.add(item);
419
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
420
        }
421
 
422
        //Remove all items which have not been updated since last content generation.
423
        List<Long> removeEntities = new ArrayList<Long>();
424
        for(Long entityId:entityIdItemMap.keySet()){
425
            boolean isValidEntity = false;
426
            //If any one of the items has been updated before current timestamp, than we generate content for whole entity
427
            for(Item item: entityIdItemMap.get(entityId)){
428
                if(item.getUpdatedOn() > 0){
429
                    isValidEntity = true;
430
                }
431
            }
432
            if(!isValidEntity){
433
                removeEntities.add(entityId);
434
            }
435
        }
436
        for(Long entityId: removeEntities){
437
            entityIdItemMap.remove(entityId);
438
        }
439
    }
5229 varun.gupt 440
 
441
    /**
442
     * Generate XML feed for mobile site
443
     * @throws Exception
444
     */
445
    public void generateXMLFeedForMobileSite() throws Exception	{
446
    	List<String> productXMLSnippets = new ArrayList<String>();
447
		productXMLSnippets.add("<Products>");
448
		List<Long> itemIds = new ArrayList<Long>();
449
 
450
		for(Long entityId: entityIdItemMap.keySet()){
451
			List<Item> items = entityIdItemMap.get(entityId);
452
			Item firstItem = items.get(0);
453
			Utils.info("first Item: " + firstItem);
454
 
455
			if(isMobile(firstItem) || isLaptop(firstItem)) {
456
				itemIds.add(firstItem.getId());
457
			}
458
		}
4188 varun.gupt 459
 
5229 varun.gupt 460
		List<String> descriptions = new ArrayList<String>();
461
		descriptions.add("8MP camera, Super AMOLED Plus display, Android Gingerbread");
462
		descriptions.add("Android OS v2.2 Froyo, 800MHz processor, 3.5\" capacitive display");
463
		descriptions.add("Dual-SIM, Wi-Fi, Bluetooth, Social networking &amp; IM");
464
		descriptions.add("Android Gingerbread, Facebook button, 5MP camera");
465
		descriptions.add("Android Gingerbread, 1GHz processor, 3.7\" Gorilla Glass display");
466
		descriptions.add("2GB RAM, 500GB HDD, Intel Core 2 Duo T6570 processor, DOS");
467
		descriptions.add("4GB RAM, 500GB HDD, Intel Core i5 2410M processor, Windows 7 Basic");
468
		descriptions.add("3.2\" touchscreen, 2MP camera, IM &amp; social networking");
469
 
470
		for(Long entityId: entityIdItemMap.keySet()) {
471
			List<Item> items = entityIdItemMap.get(entityId);
472
			Item firstItem = items.get(0);
473
 
474
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
475
				continue;
476
			}
4188 varun.gupt 477
 
5229 varun.gupt 478
			String url = getProductURL(entityId, firstItem);
479
			String imageUrl = "http://www.saholic.com/images/website" + File.separator + entityId + File.separator + "thumbnail.jpg";
480
			String description = descriptions.get((int) (8 * Math.random()));
481
			double minPrice = getMinPrice(items);
482
 
483
			String productType = "";
484
 
485
			if (firstItem.getCategory() == 10001 || firstItem.getCategory() == 10002 || firstItem.getCategory() == 10003 || firstItem.getCategory() == 10004 || firstItem.getCategory() == 10005)	{
486
				productType = "Mobile Phone";
487
 
488
			} else if (firstItem.getCategory() == 10010)	{
489
				productType = "Tablet";
490
 
491
			} else if (firstItem.getCategory() == 10050)	{
492
				productType = "Laptop";
493
			}
494
 
495
			productXMLSnippets.add(this.xmlIndentation[1] + "<Product>");	
496
 
497
			productXMLSnippets.add(this.xmlIndentation[2] + "<ID>" + entityId + "</ID>");
498
			productXMLSnippets.add(this.xmlIndentation[2] + "<Type>" + productType + "</Type>");
499
			productXMLSnippets.add(this.xmlIndentation[2] + "<Brand>" + firstItem.getBrand() + "</Brand>");
500
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelName>" + firstItem.getModelName() + "</ModelName>");
501
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelNumber>" + firstItem.getModelNumber() + "</ModelNumber>");
502
			productXMLSnippets.add(this.xmlIndentation[2] + "<URL>" + url + "</URL>");
503
			productXMLSnippets.add(this.xmlIndentation[2] + "<ImageURL>" + imageUrl + "</ImageURL>");
504
			productXMLSnippets.add(this.xmlIndentation[2] + "<ShortDesc>" + description + "</ShortDesc>");
505
			productXMLSnippets.add(this.xmlIndentation[2] + "<MRP>" + firstItem.getMrp() + "</MRP>");
506
			productXMLSnippets.add(this.xmlIndentation[2] + "<SellingPrice>" + (int)minPrice  + "</SellingPrice>");
507
 
508
			productXMLSnippets.add(this.xmlIndentation[1] + "</Product>");
509
		}
510
 
511
		productXMLSnippets.add("</Products>");
512
 
513
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
514
 
515
		Utils.info(productDataXML);
516
 
517
		// Write it to file
518
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "msitedata.xml";
519
		DBUtils.store(productDataXML, productXMLFilename);
520
    }
521
 
4188 varun.gupt 522
	/**
523
	 * Generate the xml list of all the active phones and store in a file
524
	 * @throws Exception
525
	 */
526
	public void generateProductXMLForDisplayAds() throws Exception {
4384 varun.gupt 527
		Utils.info("Generating Product XML for display ads");
4188 varun.gupt 528
		List<String> productXMLSnippets = new ArrayList<String>();
529
		productXMLSnippets.add("<saholic.com>");
530
		List<Long> itemIds = new ArrayList<Long>();
4384 varun.gupt 531
 
532
		Utils.info("Entity Ids: " + entityIdItemMap.keySet());
4188 varun.gupt 533
 
534
		for(Long entityId: entityIdItemMap.keySet()){
535
			List<Item> items = entityIdItemMap.get(entityId);
536
			Item firstItem = items.get(0);
4384 varun.gupt 537
			Utils.info("first Item: " + firstItem);
538
 
4383 varun.gupt 539
			if(isMobile(firstItem) || isLaptop(firstItem)) {
4188 varun.gupt 540
				itemIds.add(firstItem.getId());
541
			}
542
		}
543
 
544
		PromotionClient promotionClient = new PromotionClient();
545
		in.shop2020.model.v1.user.PromotionService.Client promotionServiceClient = promotionClient.getClient();
546
 
547
		List<ItemCouponDiscount> itemsCouponsAndDiscounts = promotionServiceClient.getItemDiscountMap(itemIds);
548
 
549
		Map<Long, ItemCouponDiscount> couponsAndDiscounts = new HashMap<Long, ItemCouponDiscount>();
550
 
551
		for (ItemCouponDiscount itemCouponDiscount: itemsCouponsAndDiscounts) {
552
			couponsAndDiscounts.put(itemCouponDiscount.getItemId(), itemCouponDiscount);
553
		}
4384 varun.gupt 554
		Utils.info("Entity IDs: " + entityIdItemMap.keySet());
4188 varun.gupt 555
 
556
		for(Long entityId: entityIdItemMap.keySet()) {
557
			List<Item> items = entityIdItemMap.get(entityId);
558
			Item firstItem = items.get(0);
4384 varun.gupt 559
 
560
			Utils.info("First Item: " + firstItem);
561
 
4382 varun.gupt 562
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
4188 varun.gupt 563
				continue;
564
			}
565
 
566
			productXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
567
 
568
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
569
 
570
			String title = getProductTitle(firstItem);
571
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
572
 
573
			String url = getProductURL(entityId, firstItem);
574
 
575
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
576
 
577
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
578
 
579
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
580
 
581
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + firstItem.getMrp() + "</ProductMRP>");
582
 
583
			double minPrice = getMinPrice(items);
584
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSellingPrice>" + (int)minPrice  + "</ProductSellingPrice>");
585
 
586
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDiscount>" + (int)((firstItem.getMrp()-minPrice)/firstItem.getMrp()*100) + "</ProductDiscount>");
587
 
588
			long itemId = firstItem.getId();
589
 
590
			if(couponsAndDiscounts.containsKey(itemId))	{
591
 
592
				ItemCouponDiscount itemCouponDiscount = couponsAndDiscounts.get(itemId);
593
 
594
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons>");
595
				productXMLSnippets.add(this.xmlIndentation[3] + "<Coupon>");
596
				productXMLSnippets.add(this.xmlIndentation[4] + "<CouponCode>" + itemCouponDiscount.getCouponCode() + "</CouponCode>");
597
				productXMLSnippets.add(this.xmlIndentation[4] + "<PriceAfterCoupon>" + (int)(minPrice - itemCouponDiscount.getDiscount()) + "</PriceAfterCoupon>");
598
				productXMLSnippets.add(this.xmlIndentation[3] + "</Coupon>");
599
				productXMLSnippets.add(this.xmlIndentation[2] + "</ProductCoupons>");
600
 
601
			} else	{
602
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons />");
603
			}
604
 
605
			String description = "";
606
			Entity entity = CreationUtils.getEntity(entityId);
607
			if(entity!=null){
608
				if(entity.getSlide(130001) !=null){
4202 varun.gupt 609
					description = StringEscapeUtils.escapeXml(entity.getSlide(130001).getFreeformContent().getFreeformText());
4188 varun.gupt 610
				}
611
			}
612
 
613
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDescription>" + description + "</ProductDescription>");
614
 
615
			productXMLSnippets.add(this.xmlIndentation[1] + "</products>");	
616
		}
617
 
618
		productXMLSnippets.add("</saholic.com>");
619
 
620
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
621
 
622
		Utils.info(productDataXML);
623
 
624
		// Write it to file
625
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "advertismentapi.xml";
626
		DBUtils.store(productDataXML, productXMLFilename);
627
	}
5229 varun.gupt 628
 
629
	public static void main(String[] args) throws Exception {
630
		CatalogClient cl = new CatalogClient();
631
		in.shop2020.model.v1.catalog.InventoryService.Client client = cl.getClient();
632
 
5612 amit.gupta 633
//		List<Item> items = cl.getClient().getAllItemsByStatus(status.ACTIVE);
634
		List<Long> items1 = Arrays.asList(1587l,2222l,2258l,2270l,5836l,6718l, 6719l);
635
		List<Item> items = new ArrayList<Item>();
636
		for (Long l : items1) {
637
			items.add(cl.getClient().getItem(l));
638
		}
639
		//items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED));
5229 varun.gupt 640
//		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED_BY_RISK));
641
 
642
		Map<Long, List<Item>> entityIdItemMap1 = new HashMap<Long, List<Item>>();
643
		ProductListGenerator pl = new ProductListGenerator(entityIdItemMap1);
644
		pl.populateEntityIdItemMap(items);
5612 amit.gupta 645
		//pl.generateAccessoriesXML();
646
		pl.generateXMLFeedForMobileSite();
5229 varun.gupt 647
	}
5612 amit.gupta 648
 
649
    private String readFile(String path) throws IOException {
650
		FileInputStream stream = new FileInputStream(new File(path));
651
		try {
652
			FileChannel fc = stream.getChannel();
653
			MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()-1);
654
			/* Instead of using default, pass in a decoder. */
655
			return Charset.defaultCharset().decode(bb).toString();
656
		}
657
		finally {
658
			stream.close();
659
		}
660
	}
4143 varun.gupt 661
}