Subversion Repositories SmartDukaan

Rev

Rev 5425 | Rev 5502 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5314 varun.gupt 1
package in.shop2020.util;
2
 
5497 amit.gupta 3
import in.shop2020.metamodel.core.Bullet;
4
import in.shop2020.metamodel.core.Feature;
5
import in.shop2020.metamodel.core.PrimitiveDataObject;
6
import in.shop2020.metamodel.core.Slide;
5346 amit.gupta 7
import in.shop2020.metamodel.definitions.Category;
8
import in.shop2020.metamodel.util.CreationUtils;
9
import in.shop2020.metamodel.util.ExpandedEntity;
10
 
5425 amit.gupta 11
import java.util.ArrayList;
5497 amit.gupta 12
import java.util.Arrays;
13
import java.util.HashMap;
5425 amit.gupta 14
import java.util.List;
5346 amit.gupta 15
import java.util.Map;
16
 
5497 amit.gupta 17
import org.apache.commons.lang.StringUtils;
5346 amit.gupta 18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
 
5314 varun.gupt 21
public class MostComparedIndexGenerator {
5497 amit.gupta 22
 
5314 varun.gupt 23
	private String[] indentation = {"", "    ", "        ", "            ","                "};
5497 amit.gupta 24
	private Map<String, List<String>> brandSynonyms = new HashMap<String, List<String>>();
25
	private static Log log = LogFactory.getLog(MostComparedIndexGenerator.class);
26
 
5425 amit.gupta 27
	private List<Long> validEntityIds;
28
 
5497 amit.gupta 29
 
5425 amit.gupta 30
	public MostComparedIndexGenerator(List<Long> valiEntityIds) {
5497 amit.gupta 31
		Map<String, String> brSynonyms = new HashMap<String, String>();
32
 
5425 amit.gupta 33
		this.validEntityIds = valiEntityIds;
5497 amit.gupta 34
		try {
35
			brSynonyms =  CreationUtils.getSynonyms().get("brand");
36
			for(String brand : brSynonyms.keySet()){
37
				List<String> brandSyns = Arrays.asList((brand + "," + StringUtils.defaultString(brSynonyms.get(brand))).split(","));
38
				brandSynonyms.put(brand, brandSyns);
39
			}
40
		} catch (Exception e) {
41
			// TODO: handle exception
42
		}
5425 amit.gupta 43
	}
44
 
5497 amit.gupta 45
 
46
 
5314 varun.gupt 47
	public void generate()	{
48
		StringBuilder sb = new StringBuilder();
5346 amit.gupta 49
		try {
50
			sb.append(getAutoGeneratedComparisonLinks());
51
		} catch(Exception e){
52
			log.error("Could not generate Autogenerated Comparison Links");
53
			e.printStackTrace();
54
		}
5314 varun.gupt 55
		String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-index.html";
56
 
57
		try	{
58
			DBUtils.store(sb.toString(), indexFilename);
59
 
60
		} catch (Exception e) {
61
			e.printStackTrace();
62
		}
63
	}
64
 
65
	public static void main(String[] args) {
5425 amit.gupta 66
		List<Long> list = new ArrayList<Long>();
67
		MostComparedIndexGenerator g = new MostComparedIndexGenerator(list);
5314 varun.gupt 68
		g.generate();
69
	}
5346 amit.gupta 70
 
71
	private StringBuffer getAutoGeneratedComparisonLinks() throws Exception {
72
		Map<Long, Map<Long, Long>> comparisonStats = null;
5497 amit.gupta 73
		StringBuffer sbProds = new StringBuffer();
5346 amit.gupta 74
		comparisonStats = CreationUtils.getComparisonStats();
75
		if(comparisonStats != null){
76
			for(Map.Entry<Long, Map<Long, Long>> entry : comparisonStats.entrySet()){
77
				Long entityId = entry.getKey();
78
				ExpandedEntity expandedEntity = new ExpandedEntity(CreationUtils.getEntity(entityId));
79
				Category category = expandedEntity.getCategory();
80
				if(category == null){
81
					continue;
82
				}
5425 amit.gupta 83
				if(this.validEntityIds.contains(entityId) && category.getParentCategory().getID()==Utils.MOBILE_PHONES_CATAGORY) {
5497 amit.gupta 84
					Slide slide = expandedEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
85
					List<String> modelNameSyn = new ArrayList<String>();
86
					modelNameSyn.add(expandedEntity.getModelName());
87
					List<String> modelNumberSyn = new ArrayList<String>();
88
					modelNumberSyn.add(expandedEntity.getModelNumber());
89
 
90
					for(Feature feature: slide.getFeatures())	{
91
 
92
						if(feature.getFeatureDefinitionID() == 120156)	{
93
							List<Bullet> bullets =  feature.getBullets();
94
 
95
							if(bullets != null)	{
96
								for(Bullet bullet: bullets){
97
									PrimitiveDataObject pdo = (PrimitiveDataObject) bullet.getDataObject();
98
									modelNameSyn.add(pdo.getValue());
99
								}
100
							}
101
						} else if(feature.getFeatureDefinitionID() == 120157)	{
102
							List<Bullet> bullets =  feature.getBullets();
103
 
104
							if(bullets != null)	{
105
								for(Bullet bullet: bullets)	{
106
									PrimitiveDataObject pdo = (PrimitiveDataObject)bullet.getDataObject();
107
									modelNumberSyn.add(pdo.getValue());
108
								}
109
							}
110
						}
111
					}
112
 
5346 amit.gupta 113
					String entityName = EntityUtils.getProductName(expandedEntity);
5358 amit.gupta 114
					String hyphendatedName = entityName.replaceAll(" +", "-").replaceAll("/+", "-").replaceAll("-+", "-").toLowerCase();
5497 amit.gupta 115
					sbProds.append(indentation[1] + "<div>\n");
116
					sbProds.append(indentation[2] + "<a href='http://www.saholic.com/generated/compare-" + hyphendatedName 
117
							+"?p1=" + entityId + "'>Compare " + entityName +"</a>\n");
118
					sbProds.append(indentation[1] + "</div>\n");
119
					StringBuffer sbProds1 = new StringBuffer();
120
					StringBuffer sbProds2 = new StringBuffer();
5346 amit.gupta 121
					for (Long anotherEntityId : entry.getValue().keySet()) {
5497 amit.gupta 122
						if(entityId.equals(anotherEntityId)){
123
							continue;
124
						}
5346 amit.gupta 125
						ExpandedEntity anotherExpandedEntity = new ExpandedEntity(CreationUtils.getEntity(anotherEntityId));
126
						Category anotherCategory = anotherExpandedEntity.getCategory();
127
						if(anotherCategory == null){
128
							continue;
129
						}
5497 amit.gupta 130
 
5425 amit.gupta 131
						if(this.validEntityIds.contains(anotherEntityId) && anotherCategory.getParentCategory().getID() == Utils.MOBILE_PHONES_CATAGORY){
5497 amit.gupta 132
							Slide slide1 = anotherExpandedEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
133
							List<String> modelNameSyn1 = new ArrayList<String>();
134
							modelNameSyn1.add(anotherExpandedEntity.getModelName());
135
							List<String> modelNumberSyn1 = new ArrayList<String>();
136
							modelNumberSyn1.add(anotherExpandedEntity.getModelNumber());
137
 
138
							for(Feature feature: slide1.getFeatures())	{
139
 
140
								if(feature.getFeatureDefinitionID() == 120156)	{
141
									List<Bullet> bullets =  feature.getBullets();
142
 
143
									if(bullets != null)	{
144
										for(Bullet bullet: bullets){
145
											PrimitiveDataObject pdo = (PrimitiveDataObject) bullet.getDataObject();
146
											modelNameSyn1.add(pdo.getValue());
147
										}
148
									}
149
								} else if(feature.getFeatureDefinitionID() == 120157)	{
150
									List<Bullet> bullets =  feature.getBullets();
151
 
152
									if(bullets != null)	{
153
										for(Bullet bullet: bullets)	{
154
											PrimitiveDataObject pdo = (PrimitiveDataObject)bullet.getDataObject();
155
											modelNumberSyn1.add(pdo.getValue());
156
										}
157
									}
158
								}
159
							}
5346 amit.gupta 160
							String anotherEntityName = EntityUtils.getProductName(anotherExpandedEntity);
5358 amit.gupta 161
							String anotherHyphenatedName = anotherEntityName.replaceAll(" +", "-").replaceAll("/+", "-").replaceAll("-+", "-").toLowerCase();
5497 amit.gupta 162
 
163
							sbProds1.append(indentation[1] + "<div>\n");
164
							sbProds1.append(indentation[2] + "<a href='http://www.saholic.com/compare-mobile-phones/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
165
									+"?p1=" + entityId +"&p2="+ anotherEntityId+"'>" + entityName + " Vs "+ anotherEntityName + "</a>\n");
166
							sbProds1.append(indentation[1] + "</div>\n");
167
							sbProds2.append(indentation[1] + "<div>\n");
168
							sbProds2.append(indentation[2] + "<a href='http://www.saholic.com/generated/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
169
									+"?p1=" + entityId +"&p2="+ anotherEntityId+" '>" + entityName + " Vs "+ anotherEntityName + "</a>\n");
170
							sbProds2.append(indentation[1] + "</div>\n");
171
 
172
							StringBuffer sb = new StringBuffer();
173
							for(String br : getBrandSynonyms(expandedEntity.getBrand())){
174
								for (String mName : modelNameSyn){
175
									for (String mNumber : modelNumberSyn){
176
										for(String br1 : getBrandSynonyms(anotherExpandedEntity.getBrand())) {
177
											for (String mName1 : modelNameSyn1){
178
												for (String mNumber1 : modelNumberSyn1){
179
													sb.append(indentation[1] + "<div>\n");
180
													sb.append(indentation[2] + "<a href='http://www.saholic.com/compare-mobile-phones/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
181
															+"?p1=" + entityId +"&p2="+ anotherEntityId+" '>" + br + " " + mName + " " + mNumber 
182
															+ " Vs "+ br1 + " " + mName1 + " " + mNumber1 + "</a>\n");
183
													sb.append(indentation[1] + "</div>\n");
184
												}
185
											}
186
										}
187
									}
188
								}
189
							}
190
							String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-" + entityId + "-vs-" + anotherEntityId + ".html";
191
 
192
							try	{
193
								DBUtils.store(sb.toString(), indexFilename);
194
 
195
							} catch (Exception e) {
196
								e.printStackTrace();
197
							}
5346 amit.gupta 198
						}
199
					}
5497 amit.gupta 200
					String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-" + entityId + ".html";
201
 
202
					try	{
203
						DBUtils.store(sbProds1.append(sbProds2).toString(), indexFilename);
204
 
205
					} catch (Exception e) {
206
						e.printStackTrace();
207
					}
5346 amit.gupta 208
				}
209
			}
210
		}
211
 
5497 amit.gupta 212
		return sbProds;
5346 amit.gupta 213
 
214
	}
5497 amit.gupta 215
 
216
 
217
	private List<String> getBrandSynonyms(String brand) {
218
		if(!brandSynonyms.containsKey(brand)){
219
			List<String> list = new ArrayList<String>();
220
			list.add(brand);
221
			brandSynonyms.put(brand, list);
222
		}
223
		return brandSynonyms.get(brand);
224
	}
5314 varun.gupt 225
}