Subversion Repositories SmartDukaan

Rev

Rev 2233 | Rev 2367 | 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
 
2253 rajveer 98
			double itemPrice = Double.MAX_VALUE;
1678 rajveer 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;
2253 rajveer 110
						if(itemPrice > item.getSellingPrice()){
111
							itemPrice = item.getSellingPrice();
112
						}
1678 rajveer 113
				}
114
			}
115
			if(!hasActiveItem){
116
				continue;
117
			}
118
 
119
			long categoryID = expEntity.getCategoryID();
120
			if(categoryID == -1){
121
				continue;
122
			}
123
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
124
 
125
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + expEntity.getID() + "</ProductSKU>");
126
 
127
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
128
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
129
 
130
			String url = getEntityURL(expEntity);
2130 rajveer 131
 
132
			String imageUrl = "http://static" + ((expEntity.getID()+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + expEntity.getID() + File.separator + "icon.jpg";    
133
 
1678 rajveer 134
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
135
 
136
 
137
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
138
 
2130 rajveer 139
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
140
 
1678 rajveer 141
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
142
		}
143
	}
144
 
145
	/**
146
	 * Generate the xml list of all the active phones and store in a file
147
	 * @throws Exception
148
	 */
2227 rajveer 149
	public void generateProductsListXML() throws Exception {
1678 rajveer 150
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
151
		productXMLSnippets.add("<saholic.com>");
152
 
153
		getProductsXML(productXMLSnippets);
154
 
155
		productXMLSnippets.add("</saholic.com>");
156
 
157
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
158
 
159
		Utils.info(productDataXML);
160
 
161
		// Write it to file
2130 rajveer 162
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 163
		DBUtils.store(productDataXML, productXMLFilename);
164
	}
2227 rajveer 165
 
166
 
167
 
168
 
169
	/**
170
	 * get xml feed for partner sites
171
	 * @param irDataXMLSnippets
172
	 * @throws Exception
173
	 */
174
	private void getProductsJavascript(StringBuffer sb) throws Exception{
2233 rajveer 175
		boolean isFirst = true;
2227 rajveer 176
		Map<Long, Entity> entities =  CreationUtils.getEntities();
177
		for(Entity entity: entities.values()){
178
			EntityState state = CreationUtils.getEntityState(entity.getID());
179
 
180
			ExpandedEntity expEntity = new ExpandedEntity(entity);
181
			if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
182
				continue;
183
			}
184
			if(state.getStatus() != EntityStatus.READY){
185
				continue;
186
			}
187
 
188
			List<Item> items = client.getItemsByCatalogId(entity.getID());
189
			if(items == null){
190
				continue;
191
			}
192
 
193
			Date todate = new Date();
194
			Boolean hasActiveItem = false;
195
			for(Item item: items){
196
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
197
						hasActiveItem = true;
198
				}
199
			}
200
			if(!hasActiveItem){
201
				continue;
202
			}
203
 
204
			long categoryID = expEntity.getCategoryID();
205
			if(categoryID == -1){
206
				continue;
207
			}
208
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
2233 rajveer 209
			if(isFirst){
210
				sb.append("\"" + title + "\":" + entity.getID());
211
				isFirst = false;
212
			}else{
213
				sb.append(",\"" + title + "\":" + entity.getID());
214
			}
215
 
2227 rajveer 216
		}
217
	}
218
 
219
 
220
	/**
221
	 * Generate the javascript list of all the active phones and store in a file
222
	 * @throws Exception
223
	 */
224
	public void generateProductListJavascript() throws Exception {
225
		StringBuffer productsJavascript = new StringBuffer();
226
 
227
		productsJavascript.append("var productIdNames={");
228
 
229
		getProductsJavascript(productsJavascript);
230
 
231
		productsJavascript.append("};");
232
 
233
		// Write it to file
234
		String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
235
		DBUtils.store(productsJavascript.toString(), productXMLFilename);
236
	}
237
 
1678 rajveer 238
}