Subversion Repositories SmartDukaan

Rev

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