Subversion Repositories SmartDukaan

Rev

Rev 4384 | Rev 5113 | 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
 
4188 varun.gupt 3
import in.shop2020.metamodel.core.Entity;
4802 amit.gupta 4
import in.shop2020.metamodel.definitions.Catalog;
5
import in.shop2020.metamodel.definitions.DefinitionsContainer;
4188 varun.gupt 6
import in.shop2020.metamodel.util.CreationUtils;
1678 rajveer 7
import in.shop2020.model.v1.catalog.Item;
4188 varun.gupt 8
import in.shop2020.model.v1.catalog.status;
9
import in.shop2020.model.v1.user.ItemCouponDiscount;
10
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.PromotionClient;
12
 
2130 rajveer 13
import java.io.File;
1678 rajveer 14
import java.util.ArrayList;
4188 varun.gupt 15
import java.util.Date;
4143 varun.gupt 16
import java.util.HashMap;
1678 rajveer 17
import java.util.List;
18
import java.util.Map;
19
 
4202 varun.gupt 20
import org.apache.commons.lang.StringEscapeUtils;
1678 rajveer 21
import org.apache.commons.lang.StringUtils;
4143 varun.gupt 22
import org.json.JSONObject;
1678 rajveer 23
 
24
 
25
/**
26
 * Command line utility to generate xml file for partners
27
 * 
28
 * @author rajveer
29
 *
30
 */
31
public class ProductListGenerator {
32
 
33
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
2367 rajveer 34
	public Map<Long, List<Item>> entityIdItemMap;
4802 amit.gupta 35
	DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
1678 rajveer 36
 
2367 rajveer 37
	public ProductListGenerator(Map<Long, List<Item>> entityIdItemMap) throws Exception {
38
		this.entityIdItemMap = entityIdItemMap;
1678 rajveer 39
	}
40
 
41
 
42
	/**
2367 rajveer 43
	 * Get the url of the entity
1678 rajveer 44
	 * @param expEntity
45
	 * @return url
46
	 */
2367 rajveer 47
	private String getProductURL(long entityId, Item item){
4802 amit.gupta 48
		//long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
49
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
50
 
51
		String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
2367 rajveer 52
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
53
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
54
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
55
        + "-" + entityId;
1678 rajveer 56
		productUrl = productUrl.replaceAll("/", "-");
57
		url = url + productUrl;
2367 rajveer 58
		url = url.replaceAll("-+", "-");
1678 rajveer 59
		return url;
60
	}
61
 
2367 rajveer 62
 
63
 
1678 rajveer 64
	/**
3964 rajveer 65
	 * Get the minimum mrp of the items
66
	 * @param items
67
	 * @return
68
	 */
69
	private double getMinMRP(List<Item> items){
70
        double minPrice = Double.MAX_VALUE;
71
        for(Item item: items){
72
            if(minPrice > item.getMrp()){
73
                minPrice =  item.getMrp();
74
            }
75
        }
76
        return minPrice;
77
    }
78
 
79
 
80
 
81
	/**
2367 rajveer 82
	 * Get the minimum price of the items
83
	 * @param items
84
	 * @return
85
	 */
86
	private double getMinPrice(List<Item> items){
87
        double minPrice = Double.MAX_VALUE;
88
        for(Item item: items){
89
            if(minPrice > item.getSellingPrice()){
90
                minPrice = item.getSellingPrice();
91
            }
92
        }
93
        return minPrice;
94
    }
95
 
96
	/**
97
	 * Check whether product is mobile or not
98
	 * @param item
99
	 * @return
100
	 */
101
	private boolean isMobile(Item item){
4802 amit.gupta 102
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
103
		return parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY;
2367 rajveer 104
	}
105
 
106
	/**
4143 varun.gupt 107
	 * Checks whether a product is laptop or not
108
	 */
109
	private boolean isLaptop(Item item) {
4802 amit.gupta 110
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
111
		return parentCategory.getID() == Utils.LAPTOPS_CATEGORY;
4143 varun.gupt 112
	}
113
 
114
	/**
2367 rajveer 115
	 * 
116
	 * @param item
117
	 * @return
118
	 */
119
	private String getProductTitle(Item item){
2542 rajveer 120
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
121
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
122
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
2561 rajveer 123
		title = title.replaceAll("  ", " ");
2367 rajveer 124
		return title;
125
	}
126
 
127
	/**
1678 rajveer 128
	 * get xml feed for partner sites
129
	 * @param irDataXMLSnippets
130
	 * @throws Exception
131
	 */
2367 rajveer 132
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
133
		for(Long entityId: entityIdItemMap.keySet()){
134
			List<Item> items = entityIdItemMap.get(entityId);
135
			Item firstItem = items.get(0);
136
			if(!isMobile(firstItem)){
1678 rajveer 137
				continue;
138
			}
139
 
140
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
141
 
2367 rajveer 142
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
1678 rajveer 143
 
2367 rajveer 144
			String title = getProductTitle(firstItem);
1678 rajveer 145
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
146
 
2367 rajveer 147
			String url = getProductURL(entityId, firstItem);
3929 mandeep.dh 148
 
149
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
150
			                  "website" + File.separator + entityId + File.separator + "icon.jpg";
151
 
1678 rajveer 152
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
153
 
154
 
2367 rajveer 155
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
1678 rajveer 156
 
3964 rajveer 157
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
158
 
2130 rajveer 159
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
160
 
1678 rajveer 161
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
162
		}
163
	}
164
 
165
	/**
166
	 * Generate the xml list of all the active phones and store in a file
167
	 * @throws Exception
168
	 */
2227 rajveer 169
	public void generateProductsListXML() throws Exception {
1678 rajveer 170
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
171
		productXMLSnippets.add("<saholic.com>");
172
 
173
		getProductsXML(productXMLSnippets);
174
 
175
		productXMLSnippets.add("</saholic.com>");
176
 
177
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
178
 
179
		Utils.info(productDataXML);
180
 
181
		// Write it to file
2130 rajveer 182
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 183
		DBUtils.store(productDataXML, productXMLFilename);
184
	}
2227 rajveer 185
 
186
 
187
 
188
 
189
	/**
190
	 * get xml feed for partner sites
191
	 * @param irDataXMLSnippets
192
	 * @throws Exception
193
	 */
194
	private void getProductsJavascript(StringBuffer sb) throws Exception{
4143 varun.gupt 195
 
196
		Map<String, Long> mobilePhones = new HashMap<String, Long>();
197
		Map<String, Long> laptops = new HashMap<String, Long>();
2367 rajveer 198
 
4143 varun.gupt 199
		for(Long entityId: entityIdItemMap.keySet())	{
200
			Item item = entityIdItemMap.get(entityId).get(0);
201
 
202
			if (isMobile(item))	{
203
				mobilePhones.put(getProductTitle(item), entityId);
2227 rajveer 204
			}
4143 varun.gupt 205
			else if (isLaptop(item))	{
206
				laptops.put(getProductTitle(item), entityId);
2233 rajveer 207
			}
2227 rajveer 208
		}
4143 varun.gupt 209
		Map<String, Map<String, Long>> products = new HashMap<String, Map<String,Long>>();
210
		products.put("mobiles", mobilePhones);
211
		products.put("laptops", laptops);
212
		sb.append(new JSONObject(products));
2227 rajveer 213
	}
214
 
215
 
216
	/**
217
	 * Generate the javascript list of all the active phones and store in a file
218
	 * @throws Exception
219
	 */
220
	public void generateProductListJavascript() throws Exception {
221
		StringBuffer productsJavascript = new StringBuffer();
222
 
4143 varun.gupt 223
		productsJavascript.append("var productIdNames=");
2227 rajveer 224
 
225
		getProductsJavascript(productsJavascript);
226
 
4143 varun.gupt 227
		productsJavascript.append(";");
2227 rajveer 228
 
229
		// Write it to file
230
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
231
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
232
	}
4188 varun.gupt 233
 
234
	public static void main(String[] args) throws Exception {
235
		CatalogClient cl = new CatalogClient();
236
		in.shop2020.model.v1.catalog.InventoryService.Client client = cl.getClient();
237
 
238
		List<Item> items = cl.getClient().getAllItemsByStatus(status.ACTIVE);
239
		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED));
240
		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED_BY_RISK));
241
 
242
		Map<Long, List<Item>> entityIdItemMap1 = new HashMap<Long, List<Item>>();
243
		ProductListGenerator pl = new ProductListGenerator(entityIdItemMap1);
244
		pl.populateEntityIdItemMap(items);
245
		pl.generateProductXMLForDisplayAds();
246
	}
247
 
248
    private void populateEntityIdItemMap(List<Item> items){
249
        Date toDate = new Date();
250
        for(Item item: items){
251
            //TODO Can be removed as we are checking in calling function
252
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
253
                continue;
254
            }
255
            if(toDate.getTime() < item.getStartDate() ||  item.getSellingPrice() == 0){
256
                continue;
257
            }
258
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
259
            if(itemList == null){
260
                itemList = new ArrayList<Item>();
261
            }
262
            itemList.add(item);
263
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
264
        }
265
 
266
        //Remove all items which have not been updated since last content generation.
267
        List<Long> removeEntities = new ArrayList<Long>();
268
        for(Long entityId:entityIdItemMap.keySet()){
269
            boolean isValidEntity = false;
270
            //If any one of the items has been updated before current timestamp, than we generate content for whole entity
271
            for(Item item: entityIdItemMap.get(entityId)){
272
                if(item.getUpdatedOn() > 0){
273
                    isValidEntity = true;
274
                }
275
            }
276
            if(!isValidEntity){
277
                removeEntities.add(entityId);
278
            }
279
        }
280
        for(Long entityId: removeEntities){
281
            entityIdItemMap.remove(entityId);
282
        }
283
    }
284
 
285
 
286
	/**
287
	 * Generate the xml list of all the active phones and store in a file
288
	 * @throws Exception
289
	 */
290
	public void generateProductXMLForDisplayAds() throws Exception {
4384 varun.gupt 291
		Utils.info("Generating Product XML for display ads");
4188 varun.gupt 292
		List<String> productXMLSnippets = new ArrayList<String>();
293
		productXMLSnippets.add("<saholic.com>");
294
		List<Long> itemIds = new ArrayList<Long>();
4384 varun.gupt 295
 
296
		Utils.info("Entity Ids: " + entityIdItemMap.keySet());
4188 varun.gupt 297
 
298
		for(Long entityId: entityIdItemMap.keySet()){
299
			List<Item> items = entityIdItemMap.get(entityId);
300
			Item firstItem = items.get(0);
4384 varun.gupt 301
			Utils.info("first Item: " + firstItem);
302
 
4383 varun.gupt 303
			if(isMobile(firstItem) || isLaptop(firstItem)) {
4188 varun.gupt 304
				itemIds.add(firstItem.getId());
305
			}
306
		}
307
 
308
		PromotionClient promotionClient = new PromotionClient();
309
		in.shop2020.model.v1.user.PromotionService.Client promotionServiceClient = promotionClient.getClient();
310
 
311
		List<ItemCouponDiscount> itemsCouponsAndDiscounts = promotionServiceClient.getItemDiscountMap(itemIds);
312
 
313
		Map<Long, ItemCouponDiscount> couponsAndDiscounts = new HashMap<Long, ItemCouponDiscount>();
314
 
315
		for (ItemCouponDiscount itemCouponDiscount: itemsCouponsAndDiscounts) {
316
			couponsAndDiscounts.put(itemCouponDiscount.getItemId(), itemCouponDiscount);
317
		}
4384 varun.gupt 318
		Utils.info("Entity IDs: " + entityIdItemMap.keySet());
4188 varun.gupt 319
 
320
		for(Long entityId: entityIdItemMap.keySet()) {
321
			List<Item> items = entityIdItemMap.get(entityId);
322
			Item firstItem = items.get(0);
4384 varun.gupt 323
 
324
			Utils.info("First Item: " + firstItem);
325
 
4382 varun.gupt 326
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
4188 varun.gupt 327
				continue;
328
			}
329
 
330
			productXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
331
 
332
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
333
 
334
			String title = getProductTitle(firstItem);
335
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
336
 
337
			String url = getProductURL(entityId, firstItem);
338
 
339
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
340
 
341
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
342
 
343
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
344
 
345
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + firstItem.getMrp() + "</ProductMRP>");
346
 
347
			double minPrice = getMinPrice(items);
348
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSellingPrice>" + (int)minPrice  + "</ProductSellingPrice>");
349
 
350
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDiscount>" + (int)((firstItem.getMrp()-minPrice)/firstItem.getMrp()*100) + "</ProductDiscount>");
351
 
352
			long itemId = firstItem.getId();
353
 
354
			if(couponsAndDiscounts.containsKey(itemId))	{
355
 
356
				ItemCouponDiscount itemCouponDiscount = couponsAndDiscounts.get(itemId);
357
 
358
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons>");
359
				productXMLSnippets.add(this.xmlIndentation[3] + "<Coupon>");
360
				productXMLSnippets.add(this.xmlIndentation[4] + "<CouponCode>" + itemCouponDiscount.getCouponCode() + "</CouponCode>");
361
				productXMLSnippets.add(this.xmlIndentation[4] + "<PriceAfterCoupon>" + (int)(minPrice - itemCouponDiscount.getDiscount()) + "</PriceAfterCoupon>");
362
				productXMLSnippets.add(this.xmlIndentation[3] + "</Coupon>");
363
				productXMLSnippets.add(this.xmlIndentation[2] + "</ProductCoupons>");
364
 
365
			} else	{
366
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons />");
367
			}
368
 
369
			String description = "";
370
			Entity entity = CreationUtils.getEntity(entityId);
371
			if(entity!=null){
372
				if(entity.getSlide(130001) !=null){
4202 varun.gupt 373
					description = StringEscapeUtils.escapeXml(entity.getSlide(130001).getFreeformContent().getFreeformText());
4188 varun.gupt 374
				}
375
			}
376
 
377
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDescription>" + description + "</ProductDescription>");
378
 
379
			productXMLSnippets.add(this.xmlIndentation[1] + "</products>");	
380
		}
381
 
382
		productXMLSnippets.add("</saholic.com>");
383
 
384
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
385
 
386
		Utils.info(productDataXML);
387
 
388
		// Write it to file
389
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "advertismentapi.xml";
390
		DBUtils.store(productDataXML, productXMLFilename);
391
	}
4143 varun.gupt 392
}