Subversion Repositories SmartDukaan

Rev

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