Subversion Repositories SmartDukaan

Rev

Rev 3964 | Rev 4188 | 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
 
3719 mandeep.dh 3
import in.shop2020.model.v1.catalog.Category;
1678 rajveer 4
import in.shop2020.model.v1.catalog.Item;
2367 rajveer 5
import in.shop2020.utils.CategoryManager;
1678 rajveer 6
 
2130 rajveer 7
import java.io.File;
1678 rajveer 8
import java.util.ArrayList;
4143 varun.gupt 9
import java.util.HashMap;
1678 rajveer 10
import java.util.List;
11
import java.util.Map;
12
 
13
import org.apache.commons.lang.StringUtils;
4143 varun.gupt 14
import org.json.JSONObject;
1678 rajveer 15
 
16
 
17
/**
18
 * Command line utility to generate xml file for partners
19
 * 
20
 * @author rajveer
21
 *
22
 */
23
public class ProductListGenerator {
24
 
25
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
2367 rajveer 26
	public Map<Long, List<Item>> entityIdItemMap;
27
	CategoryManager catm = CategoryManager.getCategoryManager();
1678 rajveer 28
 
2367 rajveer 29
	public ProductListGenerator(Map<Long, List<Item>> entityIdItemMap) throws Exception {
30
		this.entityIdItemMap = entityIdItemMap;
1678 rajveer 31
	}
32
 
33
 
34
	/**
2367 rajveer 35
	 * Get the url of the entity
1678 rajveer 36
	 * @param expEntity
37
	 * @return url
38
	 */
2367 rajveer 39
	private String getProductURL(long entityId, Item item){
2829 rajveer 40
		long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
41
		if(rootCategoryId == Utils.ROOT_CATAGOEY){
42
			rootCategoryId = item.getCategory(); 	
43
		}
44
		String url = "http://www.saholic.com/" + catm.getCategoryLabel(rootCategoryId).toLowerCase().replace(' ', '-') + "/";
2367 rajveer 45
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
46
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
47
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
48
        + "-" + entityId;
1678 rajveer 49
		productUrl = productUrl.replaceAll("/", "-");
50
		url = url + productUrl;
2367 rajveer 51
		url = url.replaceAll("-+", "-");
1678 rajveer 52
		return url;
53
	}
54
 
2367 rajveer 55
 
56
 
1678 rajveer 57
	/**
3964 rajveer 58
	 * Get the minimum mrp of the items
59
	 * @param items
60
	 * @return
61
	 */
62
	private double getMinMRP(List<Item> items){
63
        double minPrice = Double.MAX_VALUE;
64
        for(Item item: items){
65
            if(minPrice > item.getMrp()){
66
                minPrice =  item.getMrp();
67
            }
68
        }
69
        return minPrice;
70
    }
71
 
72
 
73
 
74
	/**
2367 rajveer 75
	 * Get the minimum price of the items
76
	 * @param items
77
	 * @return
78
	 */
79
	private double getMinPrice(List<Item> items){
80
        double minPrice = Double.MAX_VALUE;
81
        for(Item item: items){
82
            if(minPrice > item.getSellingPrice()){
83
                minPrice = item.getSellingPrice();
84
            }
85
        }
86
        return minPrice;
87
    }
88
 
89
	/**
90
	 * Check whether product is mobile or not
91
	 * @param item
92
	 * @return
93
	 */
94
	private boolean isMobile(Item item){
3719 mandeep.dh 95
		Category category = CategoryManager.getCategoryManager().getCategory(item.getCategory());
96
        long parentCategoryId = category.getParent_category_id();
97
		if(parentCategoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || category.getId() == Utils.LAPTOPS_CATEGORY){
2367 rajveer 98
			return false;
99
		}
100
		return true;
101
	}
102
 
103
	/**
4143 varun.gupt 104
	 * Checks whether a product is laptop or not
105
	 */
106
	private boolean isLaptop(Item item) {
107
		Category category = CategoryManager.getCategoryManager().getCategory(item.getCategory());
108
        long parentCategoryId = category.getParent_category_id();
109
 
110
        return category.getId() == Utils.LAPTOPS_CATEGORY ? true : false;
111
	}
112
 
113
	/**
2367 rajveer 114
	 * 
115
	 * @param item
116
	 * @return
117
	 */
118
	private String getProductTitle(Item item){
2542 rajveer 119
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
120
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
121
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
2561 rajveer 122
		title = title.replaceAll("  ", " ");
2367 rajveer 123
		return title;
124
	}
125
 
126
	/**
1678 rajveer 127
	 * get xml feed for partner sites
128
	 * @param irDataXMLSnippets
129
	 * @throws Exception
130
	 */
2367 rajveer 131
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
132
		for(Long entityId: entityIdItemMap.keySet()){
133
			List<Item> items = entityIdItemMap.get(entityId);
134
			Item firstItem = items.get(0);
135
			if(!isMobile(firstItem)){
1678 rajveer 136
				continue;
137
			}
138
 
139
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
140
 
2367 rajveer 141
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
1678 rajveer 142
 
2367 rajveer 143
			String title = getProductTitle(firstItem);
1678 rajveer 144
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
145
 
2367 rajveer 146
			String url = getProductURL(entityId, firstItem);
3929 mandeep.dh 147
 
148
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
149
			                  "website" + File.separator + entityId + File.separator + "icon.jpg";
150
 
1678 rajveer 151
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
152
 
153
 
2367 rajveer 154
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
1678 rajveer 155
 
3964 rajveer 156
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductMRP>" + getMinMRP(items) + "</ProductMRP>");
157
 
2130 rajveer 158
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
159
 
1678 rajveer 160
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
161
		}
162
	}
163
 
164
	/**
165
	 * Generate the xml list of all the active phones and store in a file
166
	 * @throws Exception
167
	 */
2227 rajveer 168
	public void generateProductsListXML() throws Exception {
1678 rajveer 169
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
170
		productXMLSnippets.add("<saholic.com>");
171
 
172
		getProductsXML(productXMLSnippets);
173
 
174
		productXMLSnippets.add("</saholic.com>");
175
 
176
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
177
 
178
		Utils.info(productDataXML);
179
 
180
		// Write it to file
2130 rajveer 181
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 182
		DBUtils.store(productDataXML, productXMLFilename);
183
	}
2227 rajveer 184
 
185
 
186
 
187
 
188
	/**
189
	 * get xml feed for partner sites
190
	 * @param irDataXMLSnippets
191
	 * @throws Exception
192
	 */
193
	private void getProductsJavascript(StringBuffer sb) throws Exception{
4143 varun.gupt 194
 
195
		Map<String, Long> mobilePhones = new HashMap<String, Long>();
196
		Map<String, Long> laptops = new HashMap<String, Long>();
2367 rajveer 197
 
4143 varun.gupt 198
		for(Long entityId: entityIdItemMap.keySet())	{
199
			Item item = entityIdItemMap.get(entityId).get(0);
200
 
201
			if (isMobile(item))	{
202
				mobilePhones.put(getProductTitle(item), entityId);
2227 rajveer 203
			}
4143 varun.gupt 204
			else if (isLaptop(item))	{
205
				laptops.put(getProductTitle(item), entityId);
2233 rajveer 206
			}
2227 rajveer 207
		}
4143 varun.gupt 208
		Map<String, Map<String, Long>> products = new HashMap<String, Map<String,Long>>();
209
		products.put("mobiles", mobilePhones);
210
		products.put("laptops", laptops);
211
		sb.append(new JSONObject(products));
2227 rajveer 212
	}
213
 
214
 
215
	/**
216
	 * Generate the javascript list of all the active phones and store in a file
217
	 * @throws Exception
218
	 */
219
	public void generateProductListJavascript() throws Exception {
220
		StringBuffer productsJavascript = new StringBuffer();
221
 
4143 varun.gupt 222
		productsJavascript.append("var productIdNames=");
2227 rajveer 223
 
224
		getProductsJavascript(productsJavascript);
225
 
4143 varun.gupt 226
		productsJavascript.append(";");
2227 rajveer 227
 
228
		// Write it to file
229
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
230
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
231
	}
4143 varun.gupt 232
}