Subversion Repositories SmartDukaan

Rev

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