Subversion Repositories SmartDukaan

Rev

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