Subversion Repositories SmartDukaan

Rev

Rev 2829 | Rev 3929 | 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);
2130 rajveer 118
 
2367 rajveer 119
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
2130 rajveer 120
 
1678 rajveer 121
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
122
 
123
 
2367 rajveer 124
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
1678 rajveer 125
 
2130 rajveer 126
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
127
 
1678 rajveer 128
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
129
		}
130
	}
131
 
132
	/**
133
	 * Generate the xml list of all the active phones and store in a file
134
	 * @throws Exception
135
	 */
2227 rajveer 136
	public void generateProductsListXML() throws Exception {
1678 rajveer 137
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
138
		productXMLSnippets.add("<saholic.com>");
139
 
140
		getProductsXML(productXMLSnippets);
141
 
142
		productXMLSnippets.add("</saholic.com>");
143
 
144
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
145
 
146
		Utils.info(productDataXML);
147
 
148
		// Write it to file
2130 rajveer 149
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 150
		DBUtils.store(productDataXML, productXMLFilename);
151
	}
2227 rajveer 152
 
153
 
154
 
155
 
156
	/**
157
	 * get xml feed for partner sites
158
	 * @param irDataXMLSnippets
159
	 * @throws Exception
160
	 */
161
	private void getProductsJavascript(StringBuffer sb) throws Exception{
2233 rajveer 162
		boolean isFirst = true;
2367 rajveer 163
 
164
		for(Long entityId: entityIdItemMap.keySet()){
165
			List<Item> items = entityIdItemMap.get(entityId);
166
			Item firstItem = items.get(0);
167
			if(!isMobile(firstItem)){
2227 rajveer 168
				continue;
169
			}
2367 rajveer 170
			String title = getProductTitle(firstItem);
2233 rajveer 171
			if(isFirst){
2367 rajveer 172
				sb.append("\"" + title + "\":" + entityId);
2233 rajveer 173
				isFirst = false;
174
			}else{
2367 rajveer 175
				sb.append(",\"" + title + "\":" + entityId);
2233 rajveer 176
			}
2227 rajveer 177
		}
178
	}
179
 
180
 
181
	/**
182
	 * Generate the javascript list of all the active phones and store in a file
183
	 * @throws Exception
184
	 */
185
	public void generateProductListJavascript() throws Exception {
186
		StringBuffer productsJavascript = new StringBuffer();
187
 
188
		productsJavascript.append("var productIdNames={");
189
 
190
		getProductsJavascript(productsJavascript);
191
 
192
		productsJavascript.append("};");
193
 
194
		// Write it to file
195
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
196
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
197
	}
198
 
1678 rajveer 199
}