Subversion Repositories SmartDukaan

Rev

Rev 1679 | Rev 2037 | 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
 
12
import java.util.ArrayList;
13
import java.util.Date;
14
import java.util.List;
15
import java.util.Map;
16
 
17
import org.apache.commons.lang.StringUtils;
18
 
19
 
20
/**
21
 * Command line utility to generate xml file for partners
22
 * 
23
 * @author rajveer
24
 *
25
 */
26
public class ProductListGenerator {
27
 
28
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
29
	CatalogServiceClient catalogServiceClient = null;
30
	in.shop2020.model.v1.catalog.InventoryService.Client client = null;
31
	public static long MOBILE_PHONES = 10001;
32
 
33
 
34
	public static void main(String[] args) throws Exception {
35
 
36
		/*
37
		String[] options = new String[] {"domain", "partner"};
38
		String[] values = new String[] {"saholic.com", "phonecurry"};
39
 
40
		String usage = "Usage: ProductListGenerator ["+ StringUtils.join(options, "|") +"] ["+ StringUtils.join(values, "|") + "] ";
41
 
42
		if(args.length < 1) {
43
			System.out.println(usage);
44
			System.exit(-1);
45
		}
46
		String inputCommand = args[0];
47
		String inputID = null;
48
		if(args.length == 2){
49
			inputID = args[1];
50
		}
51
		*/
52
		ProductListGenerator generator = new ProductListGenerator();
53
		generator.generateXMLList();
54
	}
55
 
56
	public ProductListGenerator() throws Exception {
57
		catalogServiceClient = new CatalogServiceClient();
58
		client = catalogServiceClient.getClient();
59
	}
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){
107
				if(item.getItemStatus() == status.ACTIVE && todate.getTime() > item.getStartDate()){
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);
133
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
134
 
135
 
136
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
137
 
138
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
139
		}
140
	}
141
 
142
	/**
143
	 * Generate the xml list of all the active phones and store in a file
144
	 * @throws Exception
145
	 */
146
	public void generateXMLList() throws Exception {
147
		ArrayList<String> productXMLSnippets = new ArrayList<String>();
148
		productXMLSnippets.add("<saholic.com>");
149
 
150
		getProductsXML(productXMLSnippets);
151
 
152
		productXMLSnippets.add("</saholic.com>");
153
 
154
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
155
 
156
		Utils.info(productDataXML);
157
 
158
		// Write it to file
1705 rajveer 159
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "phonecurry.xml";
1678 rajveer 160
		DBUtils.store(productDataXML, productXMLFilename);
161
	}
162
}