Subversion Repositories SmartDukaan

Rev

Rev 2367 | Rev 2547 | 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 = {"", "    ", "        ", "            ","                "};
23
	public static long MOBILE_PHONES = 10001;
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){
38
		String url = "http://www.saholic.com/" + catm.getCategoryLabel(catm.getCategory(item.getCategory()).getParent_category_id()).toLowerCase().replace(' ', '-') + "/";
39
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
40
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
41
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
42
        + "-" + entityId;
1678 rajveer 43
		productUrl = productUrl.replaceAll("/", "-");
44
		url = url + productUrl;
2367 rajveer 45
		url = url.replaceAll("-+", "-");
1678 rajveer 46
		return url;
47
	}
48
 
2367 rajveer 49
 
50
 
1678 rajveer 51
	/**
2367 rajveer 52
	 * Get the minimum price of the items
53
	 * @param items
54
	 * @return
55
	 */
56
	private double getMinPrice(List<Item> items){
57
        double minPrice = Double.MAX_VALUE;
58
        for(Item item: items){
59
            if(minPrice > item.getSellingPrice()){
60
                minPrice = item.getSellingPrice();
61
            }
62
        }
63
        return minPrice;
64
    }
65
 
66
	/**
67
	 * Check whether product is mobile or not
68
	 * @param item
69
	 * @return
70
	 */
71
	private boolean isMobile(Item item){
72
		long parentCategoryId = CategoryManager.getCategoryManager().getCategory(item.getCategory()).getParent_category_id();
73
		if(parentCategoryId != MOBILE_PHONES){
74
			return false;
75
		}
76
		return true;
77
	}
78
 
79
	/**
80
	 * 
81
	 * @param item
82
	 * @return
83
	 */
84
	private String getProductTitle(Item item){
2542 rajveer 85
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
86
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
87
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
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
}