Subversion Repositories SmartDukaan

Rev

Rev 2227 | Rev 2253 | 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.metamodel.core.Entity;
4
import in.shop2020.metamodel.core.EntityState;
5
import in.shop2020.metamodel.core.EntityStatus;
6
import in.shop2020.metamodel.util.CreationUtils;
7
import in.shop2020.metamodel.util.ExpandedEntity;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.catalog.status;
10
import in.shop2020.thrift.clients.CatalogServiceClient;
11
 
2130 rajveer 12
import java.io.File;
1678 rajveer 13
import java.util.ArrayList;
14
import java.util.Date;
15
import java.util.List;
16
import java.util.Map;
17
 
18
import org.apache.commons.lang.StringUtils;
19
 
20
 
21
/**
22
 * Command line utility to generate xml file for partners
23
 * 
24
 * @author rajveer
25
 *
26
 */
27
public class ProductListGenerator {
28
 
29
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
30
	CatalogServiceClient catalogServiceClient = null;
31
	in.shop2020.model.v1.catalog.InventoryService.Client client = null;
32
	public static long MOBILE_PHONES = 10001;
33
 
34
 
35
	public static void main(String[] args) throws Exception {
36
 
37
		/*
38
		String[] options = new String[] {"domain", "partner"};
39
		String[] values = new String[] {"saholic.com", "phonecurry"};
40
 
41
		String usage = "Usage: ProductListGenerator ["+ StringUtils.join(options, "|") +"] ["+ StringUtils.join(values, "|") + "] ";
42
 
43
		if(args.length < 1) {
44
			System.out.println(usage);
45
			System.exit(-1);
46
		}
47
		String inputCommand = args[0];
48
		String inputID = null;
49
		if(args.length == 2){
50
			inputID = args[1];
51
		}
52
		*/
53
		ProductListGenerator generator = new ProductListGenerator();
2227 rajveer 54
		generator.generateProductsListXML();
55
		generator.generateProductListJavascript();
1678 rajveer 56
	}
57
 
58
	public ProductListGenerator() throws Exception {
59
		catalogServiceClient = new CatalogServiceClient();
60
		client = catalogServiceClient.getClient();
61
	}
62
 
63
	/**
64
	 * 
65
	 * @param expEntity
66
	 * @return url
67
	 */
68
	private String getEntityURL(ExpandedEntity expEntity){
69
		String url = "http://www.saholic.com/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
70
		String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
71
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
72
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
73
        + "-" + expEntity.getID();
74
		productUrl = productUrl.replaceAll("/", "-");
75
		url = url + productUrl;
76
		url = url.replaceAll("--", "-");
77
		return url;
78
	}
79
 
80
	/**
81
	 * get xml feed for partner sites
82
	 * @param irDataXMLSnippets
83
	 * @throws Exception
84
	 */
85
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{ 	
86
		Map<Long, Entity> entities =  CreationUtils.getEntities();
87
		for(Entity entity: entities.values()){
88
			EntityState state = CreationUtils.getEntityState(entity.getID());
89
 
90
			ExpandedEntity expEntity = new ExpandedEntity(entity);
1679 rajveer 91
			if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
1678 rajveer 92
				continue;
93
			}
94
			if(state.getStatus() != EntityStatus.READY){
95
				continue;
96
			}
97
 
98
			double itemPrice = 0;
99
 
100
			List<Item> items = client.getItemsByCatalogId(entity.getID());
101
			if(items == null){
102
				continue;
103
			}
104
 
105
			Date todate = new Date();
106
			Boolean hasActiveItem = false;
107
			for(Item item: items){
2037 rajveer 108
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
1678 rajveer 109
						hasActiveItem = true;
110
				}
111
 
112
				if(itemPrice == 0){
113
					itemPrice = item.getSellingPrice();
114
				}else if(itemPrice>item.getSellingPrice()){
115
					itemPrice = item.getSellingPrice();
116
				}
117
			}
118
			if(!hasActiveItem){
119
				continue;
120
			}
121
 
122
			long categoryID = expEntity.getCategoryID();
123
			if(categoryID == -1){
124
				continue;
125
			}
126
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
127
 
128
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + expEntity.getID() + "</ProductSKU>");
129
 
130
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
131
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
132
 
133
			String url = getEntityURL(expEntity);
2130 rajveer 134
 
135
			String imageUrl = "http://static" + ((expEntity.getID()+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + expEntity.getID() + File.separator + "icon.jpg";    
136
 
1678 rajveer 137
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
138
 
139
 
140
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
141
 
2130 rajveer 142
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
143
 
1678 rajveer 144
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
145
		}
146
	}
147
 
148
	/**
149
	 * Generate the xml list of all the active phones and store in a file
150
	 * @throws Exception
151
	 */
2227 rajveer 152
	public void generateProductsListXML() throws Exception {
1678 rajveer 153
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
154
		productXMLSnippets.add("<saholic.com>");
155
 
156
		getProductsXML(productXMLSnippets);
157
 
158
		productXMLSnippets.add("</saholic.com>");
159
 
160
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
161
 
162
		Utils.info(productDataXML);
163
 
164
		// Write it to file
2130 rajveer 165
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 166
		DBUtils.store(productDataXML, productXMLFilename);
167
	}
2227 rajveer 168
 
169
 
170
 
171
 
172
	/**
173
	 * get xml feed for partner sites
174
	 * @param irDataXMLSnippets
175
	 * @throws Exception
176
	 */
177
	private void getProductsJavascript(StringBuffer sb) throws Exception{
2233 rajveer 178
		boolean isFirst = true;
2227 rajveer 179
		Map<Long, Entity> entities =  CreationUtils.getEntities();
180
		for(Entity entity: entities.values()){
181
			EntityState state = CreationUtils.getEntityState(entity.getID());
182
 
183
			ExpandedEntity expEntity = new ExpandedEntity(entity);
184
			if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
185
				continue;
186
			}
187
			if(state.getStatus() != EntityStatus.READY){
188
				continue;
189
			}
190
 
191
			double itemPrice = 0;
192
 
193
			List<Item> items = client.getItemsByCatalogId(entity.getID());
194
			if(items == null){
195
				continue;
196
			}
197
 
198
			Date todate = new Date();
199
			Boolean hasActiveItem = false;
200
			for(Item item: items){
201
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
202
						hasActiveItem = true;
203
				}
204
 
205
				if(itemPrice == 0){
206
					itemPrice = item.getSellingPrice();
207
				}else if(itemPrice>item.getSellingPrice()){
208
					itemPrice = item.getSellingPrice();
209
				}
210
			}
211
			if(!hasActiveItem){
212
				continue;
213
			}
214
 
215
			long categoryID = expEntity.getCategoryID();
216
			if(categoryID == -1){
217
				continue;
218
			}
219
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
2233 rajveer 220
			if(isFirst){
221
				sb.append("\"" + title + "\":" + entity.getID());
222
				isFirst = false;
223
			}else{
224
				sb.append(",\"" + title + "\":" + entity.getID());
225
			}
226
 
2227 rajveer 227
		}
228
	}
229
 
230
 
231
	/**
232
	 * Generate the javascript list of all the active phones and store in a file
233
	 * @throws Exception
234
	 */
235
	public void generateProductListJavascript() throws Exception {
236
		StringBuffer productsJavascript = new StringBuffer();
237
 
238
		productsJavascript.append("var productIdNames={");
239
 
240
		getProductsJavascript(productsJavascript);
241
 
242
		productsJavascript.append("};");
243
 
244
		// Write it to file
245
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
246
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
247
	}
248
 
1678 rajveer 249
}