Subversion Repositories SmartDukaan

Rev

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