Subversion Repositories SmartDukaan

Rev

Rev 3719 | Rev 3964 | 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
	/**
2367 rajveer 56
	 * Get the minimum price of the items
57
	 * @param items
58
	 * @return
59
	 */
60
	private double getMinPrice(List<Item> items){
61
        double minPrice = Double.MAX_VALUE;
62
        for(Item item: items){
63
            if(minPrice > item.getSellingPrice()){
64
                minPrice = item.getSellingPrice();
65
            }
66
        }
67
        return minPrice;
68
    }
69
 
70
	/**
71
	 * Check whether product is mobile or not
72
	 * @param item
73
	 * @return
74
	 */
75
	private boolean isMobile(Item item){
3719 mandeep.dh 76
		Category category = CategoryManager.getCategoryManager().getCategory(item.getCategory());
77
        long parentCategoryId = category.getParent_category_id();
78
		if(parentCategoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || category.getId() == Utils.LAPTOPS_CATEGORY){
2367 rajveer 79
			return false;
80
		}
81
		return true;
82
	}
83
 
84
	/**
85
	 * 
86
	 * @param item
87
	 * @return
88
	 */
89
	private String getProductTitle(Item item){
2542 rajveer 90
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
91
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
92
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
2561 rajveer 93
		title = title.replaceAll("  ", " ");
2367 rajveer 94
		return title;
95
	}
96
 
97
	/**
1678 rajveer 98
	 * get xml feed for partner sites
99
	 * @param irDataXMLSnippets
100
	 * @throws Exception
101
	 */
2367 rajveer 102
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
103
		for(Long entityId: entityIdItemMap.keySet()){
104
			List<Item> items = entityIdItemMap.get(entityId);
105
			Item firstItem = items.get(0);
106
			if(!isMobile(firstItem)){
1678 rajveer 107
				continue;
108
			}
109
 
110
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
111
 
2367 rajveer 112
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
1678 rajveer 113
 
2367 rajveer 114
			String title = getProductTitle(firstItem);
1678 rajveer 115
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
116
 
2367 rajveer 117
			String url = getProductURL(entityId, firstItem);
3929 mandeep.dh 118
 
119
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator +
120
			                  "website" + File.separator + entityId + File.separator + "icon.jpg";
121
 
1678 rajveer 122
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
123
 
124
 
2367 rajveer 125
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
1678 rajveer 126
 
2130 rajveer 127
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
128
 
1678 rajveer 129
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
130
		}
131
	}
132
 
133
	/**
134
	 * Generate the xml list of all the active phones and store in a file
135
	 * @throws Exception
136
	 */
2227 rajveer 137
	public void generateProductsListXML() throws Exception {
1678 rajveer 138
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
139
		productXMLSnippets.add("<saholic.com>");
140
 
141
		getProductsXML(productXMLSnippets);
142
 
143
		productXMLSnippets.add("</saholic.com>");
144
 
145
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
146
 
147
		Utils.info(productDataXML);
148
 
149
		// Write it to file
2130 rajveer 150
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 151
		DBUtils.store(productDataXML, productXMLFilename);
152
	}
2227 rajveer 153
 
154
 
155
 
156
 
157
	/**
158
	 * get xml feed for partner sites
159
	 * @param irDataXMLSnippets
160
	 * @throws Exception
161
	 */
162
	private void getProductsJavascript(StringBuffer sb) throws Exception{
2233 rajveer 163
		boolean isFirst = true;
2367 rajveer 164
 
165
		for(Long entityId: entityIdItemMap.keySet()){
166
			List<Item> items = entityIdItemMap.get(entityId);
167
			Item firstItem = items.get(0);
168
			if(!isMobile(firstItem)){
2227 rajveer 169
				continue;
170
			}
2367 rajveer 171
			String title = getProductTitle(firstItem);
2233 rajveer 172
			if(isFirst){
2367 rajveer 173
				sb.append("\"" + title + "\":" + entityId);
2233 rajveer 174
				isFirst = false;
175
			}else{
2367 rajveer 176
				sb.append(",\"" + title + "\":" + entityId);
2233 rajveer 177
			}
2227 rajveer 178
		}
179
	}
180
 
181
 
182
	/**
183
	 * Generate the javascript list of all the active phones and store in a file
184
	 * @throws Exception
185
	 */
186
	public void generateProductListJavascript() throws Exception {
187
		StringBuffer productsJavascript = new StringBuffer();
188
 
189
		productsJavascript.append("var productIdNames={");
190
 
191
		getProductsJavascript(productsJavascript);
192
 
193
		productsJavascript.append("};");
194
 
195
		// Write it to file
196
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
197
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
198
	}
199
 
1678 rajveer 200
}