Subversion Repositories SmartDukaan

Rev

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

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