Subversion Repositories SmartDukaan

Rev

Rev 2253 | Rev 2542 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2253 Rev 2367
Line 1... Line 1...
1
package in.shop2020.util;
1
package in.shop2020.util;
2
 
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;
3
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.catalog.status;
4
import in.shop2020.utils.CategoryManager;
10
import in.shop2020.thrift.clients.CatalogServiceClient;
-
 
11
 
5
 
12
import java.io.File;
6
import java.io.File;
13
import java.util.ArrayList;
7
import java.util.ArrayList;
14
import java.util.Date;
-
 
15
import java.util.List;
8
import java.util.List;
16
import java.util.Map;
9
import java.util.Map;
17
 
10
 
18
import org.apache.commons.lang.StringUtils;
11
import org.apache.commons.lang.StringUtils;
19
 
12
 
Line 25... Line 18...
25
 *
18
 *
26
 */
19
 */
27
public class ProductListGenerator {
20
public class ProductListGenerator {
28
	
21
	
29
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
22
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
30
	CatalogServiceClient catalogServiceClient = null;
-
 
31
	in.shop2020.model.v1.catalog.InventoryService.Client client = null;
-
 
32
	public static long MOBILE_PHONES = 10001;
23
	public static long MOBILE_PHONES = 10001;
-
 
24
	public Map<Long, List<Item>> entityIdItemMap;
-
 
25
	CategoryManager catm = CategoryManager.getCategoryManager();
33
	
26
	
34
	
-
 
35
	public static void main(String[] args) throws Exception {
27
	public ProductListGenerator(Map<Long, List<Item>> entityIdItemMap) 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.generateProductsListXML();
28
		this.entityIdItemMap = entityIdItemMap;
55
		generator.generateProductListJavascript();
-
 
56
	}
29
	}
57
	
30
	
58
	public ProductListGenerator() throws Exception {
-
 
59
		catalogServiceClient = new CatalogServiceClient();
-
 
60
		client = catalogServiceClient.getClient();
-
 
61
	}
-
 
62
	
31
	
63
	/**
32
	/**
64
	 * 
33
	 * Get the url of the entity
65
	 * @param expEntity
34
	 * @param expEntity
66
	 * @return url
35
	 * @return url
67
	 */
36
	 */
68
	private String getEntityURL(ExpandedEntity expEntity){
37
	private String getProductURL(long entityId, Item item){
69
		String url = "http://www.saholic.com/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
38
		String url = "http://www.saholic.com/" + catm.getCategoryLabel(catm.getCategory(item.getCategory()).getParent_category_id()).toLowerCase().replace(' ', '-') + "/";
70
		String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
39
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
71
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
40
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
72
	    + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
41
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
73
        + "-" + expEntity.getID();
42
        + "-" + entityId;
74
		productUrl = productUrl.replaceAll("/", "-");
43
		productUrl = productUrl.replaceAll("/", "-");
75
		url = url + productUrl;
44
		url = url + productUrl;
76
		url = url.replaceAll("--", "-");
45
		url = url.replaceAll("-+", "-");
77
		return url;
46
		return url;
78
	}
47
	}
79
 
48
 
-
 
49
	
-
 
50
    
-
 
51
	/**
-
 
52
	 * Get the minimum price of the items
-
 
53
	 * @param items
-
 
54
	 * @return
-
 
55
	 */
-
 
56
	private double getMinPrice(List<Item> items){
-
 
57
        double minPrice = Double.MAX_VALUE;
-
 
58
        for(Item item: items){
-
 
59
            if(minPrice > item.getSellingPrice()){
-
 
60
                minPrice = item.getSellingPrice();
-
 
61
            }
-
 
62
        }
-
 
63
        return minPrice;
-
 
64
    }
-
 
65
 
-
 
66
	/**
-
 
67
	 * Check whether product is mobile or not
-
 
68
	 * @param item
-
 
69
	 * @return
-
 
70
	 */
-
 
71
	private boolean isMobile(Item item){
-
 
72
		long parentCategoryId = CategoryManager.getCategoryManager().getCategory(item.getCategory()).getParent_category_id();
-
 
73
		if(parentCategoryId != MOBILE_PHONES){
-
 
74
			return false;
-
 
75
		}
-
 
76
		return true;
-
 
77
	}
-
 
78
	
-
 
79
	/**
-
 
80
	 * 
-
 
81
	 * @param item
-
 
82
	 * @return
-
 
83
	 */
-
 
84
	private String getProductTitle(Item item){
-
 
85
		String title = ((item.getBrand() != null) ? item.getBrand() + " " : "")
-
 
86
		+ ((item.getModelName() != null) ? item.getModelName() + " " : "")
-
 
87
		+ (( item.getModelNumber() != null ) ? item.getModelNumber() : "" );
-
 
88
		return title;
-
 
89
	}
-
 
90
    
80
	/**
91
	/**
81
	 * get xml feed for partner sites
92
	 * get xml feed for partner sites
82
	 * @param irDataXMLSnippets
93
	 * @param irDataXMLSnippets
83
	 * @throws Exception
94
	 * @throws Exception
84
	 */
95
	 */
85
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{ 	
96
	private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{
86
		Map<Long, Entity> entities =  CreationUtils.getEntities();
-
 
87
		for(Entity entity: entities.values()){
97
		for(Long entityId: entityIdItemMap.keySet()){
88
			EntityState state = CreationUtils.getEntityState(entity.getID());
98
			List<Item> items = entityIdItemMap.get(entityId);
89
			
-
 
90
			ExpandedEntity expEntity = new ExpandedEntity(entity);
99
			Item firstItem = items.get(0);
91
			if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
100
			if(!isMobile(firstItem)){
92
				continue;
101
				continue;
93
			}
102
			}
94
			if(state.getStatus() != EntityStatus.READY){
-
 
95
				continue;
-
 
96
			}
-
 
97
			
-
 
98
			double itemPrice = Double.MAX_VALUE;
-
 
99
			
103
			
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){
-
 
108
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
-
 
109
						hasActiveItem = true;
-
 
110
						if(itemPrice > item.getSellingPrice()){
-
 
111
							itemPrice = item.getSellingPrice();
-
 
112
						}
-
 
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>");	
104
			irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");	
124
			
105
			
125
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + expEntity.getID() + "</ProductSKU>");
106
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");
126
			
107
			
127
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
108
			String title = getProductTitle(firstItem);
128
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
109
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");
129
 
110
 
130
			String url = getEntityURL(expEntity);
111
			String url = getProductURL(entityId, firstItem);
131
			
112
			
132
			String imageUrl = "http://static" + ((expEntity.getID()+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + expEntity.getID() + File.separator + "icon.jpg";    
113
			String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";    
133
			
114
			
134
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
115
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");
135
 
116
 
136
			
117
			
137
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
118
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");
138
			
119
			
139
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
120
			irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
140
			
121
			
141
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
122
			irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");		
142
		}
123
		}
Line 171... Line 152...
171
	 * @param irDataXMLSnippets
152
	 * @param irDataXMLSnippets
172
	 * @throws Exception
153
	 * @throws Exception
173
	 */
154
	 */
174
	private void getProductsJavascript(StringBuffer sb) throws Exception{
155
	private void getProductsJavascript(StringBuffer sb) throws Exception{
175
		boolean isFirst = true;
156
		boolean isFirst = true;
176
		Map<Long, Entity> entities =  CreationUtils.getEntities();
-
 
177
		for(Entity entity: entities.values()){
-
 
178
			EntityState state = CreationUtils.getEntityState(entity.getID());
-
 
179
			
157
		
180
			ExpandedEntity expEntity = new ExpandedEntity(entity);
158
		for(Long entityId: entityIdItemMap.keySet()){
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());
159
			List<Item> items = entityIdItemMap.get(entityId);
189
			if(items == null){
-
 
190
				continue;
-
 
191
			}
-
 
192
			
-
 
193
			Date todate = new Date();
-
 
194
			Boolean hasActiveItem = false;
-
 
195
			for(Item item: items){
160
			Item firstItem = items.get(0);
196
				if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
-
 
197
						hasActiveItem = true;
-
 
198
				}
-
 
199
			}
-
 
200
			if(!hasActiveItem){
161
			if(!isMobile(firstItem)){
201
				continue;
-
 
202
			}
-
 
203
			
-
 
204
			long categoryID = expEntity.getCategoryID();
-
 
205
			if(categoryID == -1){
-
 
206
				continue;
162
				continue;
207
			}
163
			}
208
			String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
164
			String title = getProductTitle(firstItem);
209
			if(isFirst){
165
			if(isFirst){
210
				sb.append("\"" + title + "\":" + entity.getID());
166
				sb.append("\"" + title + "\":" + entityId);
211
				isFirst = false;
167
				isFirst = false;
212
			}else{
168
			}else{
213
				sb.append(",\"" + title + "\":" + entity.getID());
169
				sb.append(",\"" + title + "\":" + entityId);
214
			}
170
			}
215
			
-
 
216
		}
171
		}
217
	}
172
	}
218
	
173
	
219
	
174
	
220
	/**
175
	/**