Subversion Repositories SmartDukaan

Rev

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