Subversion Repositories SmartDukaan

Rev

Rev 4382 | Rev 4384 | 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) {
4383 varun.gupt 116
 
4143 varun.gupt 117
		Category category = CategoryManager.getCategoryManager().getCategory(item.getCategory());
4383 varun.gupt 118
//        long parentCategoryId = category.getParent_category_id();
4143 varun.gupt 119
 
120
        return category.getId() == Utils.LAPTOPS_CATEGORY ? true : false;
121
	}
122
 
123
	/**
2367 rajveer 124
	 * 
125
	 * @param item
126
	 * @return
127
	 */
128
	private String getProductTitle(Item item){
2542 rajveer 129
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
130
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
131
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
2561 rajveer 132
		title = title.replaceAll("  ", " ");
2367 rajveer 133
		return title;
134
	}
135
 
136
	/**
1678 rajveer 137
	 * get xml feed for partner sites
138
	 * @param irDataXMLSnippets
139
	 * @throws Exception
140
	 */
2367 rajveer 141
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
142
		for(Long entityId: entityIdItemMap.keySet()){
143
			List<Item> items = entityIdItemMap.get(entityId);
144
			Item firstItem = items.get(0);
145
			if(!isMobile(firstItem)){
1678 rajveer 146
				continue;
147
			}
148
 
149
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
150
 
2367 rajveer 151
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
1678 rajveer 152
 
2367 rajveer 153
			String title = getProductTitle(firstItem);
1678 rajveer 154
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
155
 
2367 rajveer 156
			String url = getProductURL(entityId, firstItem);
3929 mandeep.dh 157
 
158
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
159
			                  "website" + File.separator + entityId + File.separator + "icon.jpg";
160
 
1678 rajveer 161
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
162
 
163
 
2367 rajveer 164
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
1678 rajveer 165
 
3964 rajveer 166
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
167
 
2130 rajveer 168
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
169
 
1678 rajveer 170
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
171
		}
172
	}
173
 
174
	/**
175
	 * Generate the xml list of all the active phones and store in a file
176
	 * @throws Exception
177
	 */
2227 rajveer 178
	public void generateProductsListXML() throws Exception {
1678 rajveer 179
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
180
		productXMLSnippets.add("<saholic.com>");
181
 
182
		getProductsXML(productXMLSnippets);
183
 
184
		productXMLSnippets.add("</saholic.com>");
185
 
186
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
187
 
188
		Utils.info(productDataXML);
189
 
190
		// Write it to file
2130 rajveer 191
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 192
		DBUtils.store(productDataXML, productXMLFilename);
193
	}
2227 rajveer 194
 
195
 
196
 
197
 
198
	/**
199
	 * get xml feed for partner sites
200
	 * @param irDataXMLSnippets
201
	 * @throws Exception
202
	 */
203
	private void getProductsJavascript(StringBuffer sb) throws Exception{
4143 varun.gupt 204
 
205
		Map<String, Long> mobilePhones = new HashMap<String, Long>();
206
		Map<String, Long> laptops = new HashMap<String, Long>();
2367 rajveer 207
 
4143 varun.gupt 208
		for(Long entityId: entityIdItemMap.keySet())	{
209
			Item item = entityIdItemMap.get(entityId).get(0);
210
 
211
			if (isMobile(item))	{
212
				mobilePhones.put(getProductTitle(item), entityId);
2227 rajveer 213
			}
4143 varun.gupt 214
			else if (isLaptop(item))	{
215
				laptops.put(getProductTitle(item), entityId);
2233 rajveer 216
			}
2227 rajveer 217
		}
4143 varun.gupt 218
		Map<String, Map<String, Long>> products = new HashMap<String, Map<String,Long>>();
219
		products.put("mobiles", mobilePhones);
220
		products.put("laptops", laptops);
221
		sb.append(new JSONObject(products));
2227 rajveer 222
	}
223
 
224
 
225
	/**
226
	 * Generate the javascript list of all the active phones and store in a file
227
	 * @throws Exception
228
	 */
229
	public void generateProductListJavascript() throws Exception {
230
		StringBuffer productsJavascript = new StringBuffer();
231
 
4143 varun.gupt 232
		productsJavascript.append("var productIdNames=");
2227 rajveer 233
 
234
		getProductsJavascript(productsJavascript);
235
 
4143 varun.gupt 236
		productsJavascript.append(";");
2227 rajveer 237
 
238
		// Write it to file
239
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
240
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
241
	}
4188 varun.gupt 242
 
243
	public static void main(String[] args) throws Exception {
244
		CatalogClient cl = new CatalogClient();
245
		in.shop2020.model.v1.catalog.InventoryService.Client client = cl.getClient();
246
 
247
		List<Item> items = cl.getClient().getAllItemsByStatus(status.ACTIVE);
248
		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED));
249
		items.addAll(cl.getClient().getAllItemsByStatus(status.PAUSED_BY_RISK));
250
 
251
		Map<Long, List<Item>> entityIdItemMap1 = new HashMap<Long, List<Item>>();
252
		ProductListGenerator pl = new ProductListGenerator(entityIdItemMap1);
253
		pl.populateEntityIdItemMap(items);
254
		pl.generateProductXMLForDisplayAds();
255
	}
256
 
257
    private void populateEntityIdItemMap(List<Item> items){
258
        Date toDate = new Date();
259
        for(Item item: items){
260
            //TODO Can be removed as we are checking in calling function
261
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
262
                continue;
263
            }
264
            if(toDate.getTime() < item.getStartDate() ||  item.getSellingPrice() == 0){
265
                continue;
266
            }
267
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
268
            if(itemList == null){
269
                itemList = new ArrayList<Item>();
270
            }
271
            itemList.add(item);
272
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
273
        }
274
 
275
        //Remove all items which have not been updated since last content generation.
276
        List<Long> removeEntities = new ArrayList<Long>();
277
        for(Long entityId:entityIdItemMap.keySet()){
278
            boolean isValidEntity = false;
279
            //If any one of the items has been updated before current timestamp, than we generate content for whole entity
280
            for(Item item: entityIdItemMap.get(entityId)){
281
                if(item.getUpdatedOn() > 0){
282
                    isValidEntity = true;
283
                }
284
            }
285
            if(!isValidEntity){
286
                removeEntities.add(entityId);
287
            }
288
        }
289
        for(Long entityId: removeEntities){
290
            entityIdItemMap.remove(entityId);
291
        }
292
    }
293
 
294
 
295
	/**
296
	 * Generate the xml list of all the active phones and store in a file
297
	 * @throws Exception
298
	 */
299
	public void generateProductXMLForDisplayAds() throws Exception {
300
 
301
		List<String> productXMLSnippets = new ArrayList<String>();
302
		productXMLSnippets.add("<saholic.com>");
303
		List<Long> itemIds = new ArrayList<Long>();
304
 
305
		for(Long entityId: entityIdItemMap.keySet()){
306
			List<Item> items = entityIdItemMap.get(entityId);
307
			Item firstItem = items.get(0);
4383 varun.gupt 308
			if(isMobile(firstItem) || isLaptop(firstItem)) {
4188 varun.gupt 309
				itemIds.add(firstItem.getId());
310
			}
311
		}
312
 
313
		PromotionClient promotionClient = new PromotionClient();
314
		in.shop2020.model.v1.user.PromotionService.Client promotionServiceClient = promotionClient.getClient();
315
 
316
		List<ItemCouponDiscount> itemsCouponsAndDiscounts = promotionServiceClient.getItemDiscountMap(itemIds);
317
 
318
		Map<Long, ItemCouponDiscount> couponsAndDiscounts = new HashMap<Long, ItemCouponDiscount>();
319
 
320
		for (ItemCouponDiscount itemCouponDiscount: itemsCouponsAndDiscounts) {
321
			couponsAndDiscounts.put(itemCouponDiscount.getItemId(), itemCouponDiscount);
322
		}
323
 
324
		for(Long entityId: entityIdItemMap.keySet()) {
325
			List<Item> items = entityIdItemMap.get(entityId);
326
			Item firstItem = items.get(0);
4382 varun.gupt 327
			if(!isMobile(firstItem) && !isLaptop(firstItem)){
4188 varun.gupt 328
				continue;
329
			}
330
 
331
			productXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
332
 
333
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
334
 
335
			String title = getProductTitle(firstItem);
336
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
337
 
338
			String url = getProductURL(entityId, firstItem);
339
 
340
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
341
 
342
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
343
 
344
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
345
 
346
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + firstItem.getMrp() + "</ProductMRP>");
347
 
348
			double minPrice = getMinPrice(items);
349
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductSellingPrice>" + (int)minPrice  + "</ProductSellingPrice>");
350
 
351
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDiscount>" + (int)((firstItem.getMrp()-minPrice)/firstItem.getMrp()*100) + "</ProductDiscount>");
352
 
353
			long itemId = firstItem.getId();
354
 
355
			if(couponsAndDiscounts.containsKey(itemId))	{
356
 
357
				ItemCouponDiscount itemCouponDiscount = couponsAndDiscounts.get(itemId);
358
 
359
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons>");
360
				productXMLSnippets.add(this.xmlIndentation[3] + "<Coupon>");
361
				productXMLSnippets.add(this.xmlIndentation[4] + "<CouponCode>" + itemCouponDiscount.getCouponCode() + "</CouponCode>");
362
				productXMLSnippets.add(this.xmlIndentation[4] + "<PriceAfterCoupon>" + (int)(minPrice - itemCouponDiscount.getDiscount()) + "</PriceAfterCoupon>");
363
				productXMLSnippets.add(this.xmlIndentation[3] + "</Coupon>");
364
				productXMLSnippets.add(this.xmlIndentation[2] + "</ProductCoupons>");
365
 
366
			} else	{
367
				productXMLSnippets.add(this.xmlIndentation[2] + "<ProductCoupons />");
368
			}
369
 
370
			String description = "";
371
			Entity entity = CreationUtils.getEntity(entityId);
372
			if(entity!=null){
373
				if(entity.getSlide(130001) !=null){
4202 varun.gupt 374
					description = StringEscapeUtils.escapeXml(entity.getSlide(130001).getFreeformContent().getFreeformText());
4188 varun.gupt 375
				}
376
			}
377
 
378
			productXMLSnippets.add(this.xmlIndentation[2] + "<ProductDescription>" + description + "</ProductDescription>");
379
 
380
			productXMLSnippets.add(this.xmlIndentation[1] + "</products>");	
381
		}
382
 
383
		productXMLSnippets.add("</saholic.com>");
384
 
385
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
386
 
387
		Utils.info(productDataXML);
388
 
389
		// Write it to file
390
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "advertismentapi.xml";
391
		DBUtils.store(productDataXML, productXMLFilename);
392
	}
4143 varun.gupt 393
}