Subversion Repositories SmartDukaan

Rev

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