Subversion Repositories SmartDukaan

Rev

Rev 2037 | Rev 2227 | 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();
54
		generator.generateXMLList();
55
	}
56
 
57
	public ProductListGenerator() throws Exception {
58
		catalogServiceClient = new CatalogServiceClient();
59
		client = catalogServiceClient.getClient();
60
	}
61
 
62
	/**
63
	 * 
64
	 * @param expEntity
65
	 * @return url
66
	 */
67
	private String getEntityURL(ExpandedEntity expEntity){
68
		String url = "http://www.saholic.com/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
69
		String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
70
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
71
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
72
        + "-" + expEntity.getID();
73
		productUrl = productUrl.replaceAll("/", "-");
74
		url = url + productUrl;
75
		url = url.replaceAll("--", "-");
76
		return url;
77
	}
78
 
79
	/**
80
	 * get xml feed for partner sites
81
	 * @param irDataXMLSnippets
82
	 * @throws Exception
83
	 */
84
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{ 	
85
		Map<Long, Entity> entities =  CreationUtils.getEntities();
86
		for(Entity entity: entities.values()){
87
			EntityState state = CreationUtils.getEntityState(entity.getID());
88
 
89
			ExpandedEntity expEntity = new ExpandedEntity(entity);
1679 rajveer 90
			if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
1678 rajveer 91
				continue;
92
			}
93
			if(state.getStatus() != EntityStatus.READY){
94
				continue;
95
			}
96
 
97
			double itemPrice = 0;
98
 
99
			List<Item> items = client.getItemsByCatalogId(entity.getID());
100
			if(items == null){
101
				continue;
102
			}
103
 
104
			Date todate = new Date();
105
			Boolean hasActiveItem = false;
106
			for(Item item: items){
2037 rajveer 107
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
1678 rajveer 108
						hasActiveItem = true;
109
				}
110
 
111
				if(itemPrice == 0){
112
					itemPrice = item.getSellingPrice();
113
				}else if(itemPrice>item.getSellingPrice()){
114
					itemPrice = item.getSellingPrice();
115
				}
116
			}
117
			if(!hasActiveItem){
118
				continue;
119
			}
120
 
121
			long categoryID = expEntity.getCategoryID();
122
			if(categoryID == -1){
123
				continue;
124
			}
125
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
126
 
127
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + expEntity.getID() + "</ProductSKU>");
128
 
129
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
130
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
131
 
132
			String url = getEntityURL(expEntity);
2130 rajveer 133
 
134
			String imageUrl = "http://static" + ((expEntity.getID()+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + expEntity.getID() + File.separator + "icon.jpg";    
135
 
1678 rajveer 136
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
137
 
138
 
139
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
140
 
2130 rajveer 141
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
142
 
1678 rajveer 143
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
144
		}
145
	}
146
 
147
	/**
148
	 * Generate the xml list of all the active phones and store in a file
149
	 * @throws Exception
150
	 */
151
	public void generateXMLList() throws Exception {
152
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
153
		productXMLSnippets.add("<saholic.com>");
154
 
155
		getProductsXML(productXMLSnippets);
156
 
157
		productXMLSnippets.add("</saholic.com>");
158
 
159
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
160
 
161
		Utils.info(productDataXML);
162
 
163
		// Write it to file
2130 rajveer 164
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
1678 rajveer 165
		DBUtils.store(productDataXML, productXMLFilename);
166
	}
167
}