Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
5117 varun.gupt 1
package in.shop2020.util;
2
 
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7
 
8
import in.shop2020.metamodel.core.Entity;
9
import in.shop2020.metamodel.core.Slide;
10
import in.shop2020.metamodel.definitions.Catalog;
11
import in.shop2020.metamodel.definitions.Category;
12
import in.shop2020.metamodel.definitions.DefinitionsContainer;
13
import in.shop2020.metamodel.util.CreationUtils;
14
 
15
public class CompatibleAccessoriesIndexGenerator {
16
 
17
	private String[] indentation = {"", "    ", "        ", "            ","                "};
18
	private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
19
	List<Entity> entities;
20
	private Map<Long, Long> categorySlides = new HashMap<Long, Long>();
21
	private Map<Long, String> categoryLabels = new HashMap<Long, String>();
22
 
5155 varun.gupt 23
	public CompatibleAccessoriesIndexGenerator(List<Entity> entities) throws Exception	{
5117 varun.gupt 24
 
25
		categorySlides.put((long) 10014, (long) 130067);
26
		categorySlides.put((long) 10018, (long) 130073);
27
 
28
		categoryLabels.put((long) 10014, "Battery");
29
		categoryLabels.put((long) 10018, "Carrying Case");
30
 
5155 varun.gupt 31
		this.entities = entities;
5117 varun.gupt 32
	}
33
 
34
	public void generate()	{
35
		StringBuilder sb = new StringBuilder();
36
 
37
		for(Entity entity: entities)	{
38
 
39
			long categoryId = entity.getCategoryID();
40
			String url = getProductURL(entity);
41
 
42
			if (url != null && categorySlides.keySet().contains(categoryId))	{
43
 
44
				Slide compatibilitySlide = entity.getSlide(categorySlides.get(categoryId));
45
 
46
				try {
47
					String text = compatibilitySlide.getFreeformContent().getFreeformText();
48
 
49
					if(text.indexOf("<ul>") > -1)	continue;
50
 
51
					for(String name: getHandsetNames(text))	{
52
						sb.append(indentation[1] + "<div>\n");
53
						sb.append(indentation[2] + "<a href='" + url + "'>" + name + " " + categoryLabels.get(categoryId)+ "</a>\n");
54
						sb.append(indentation[1] + "</div>\n");
55
					}
56
 
57
				} catch (NullPointerException e) {
58
					// TODO: handle exception
59
				}
60
			}
61
		}
62
 
63
		String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "compatible-accessories-index.html";
64
 
65
		try	{
66
			DBUtils.store(sb.toString(), indexFilename);
67
 
68
		} catch (Exception e) {
69
			e.printStackTrace();
70
		}
71
	}
72
 
73
	private String getProductURL(Entity entity)	{
74
 
75
		Category category = defContainer.getCategory(entity.getCategoryID());
76
		try {
77
			Category parentCategory = category.getParentCategory();
78
 
79
			String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
80
			String productUrl = ((entity.getBrand() != null) ? entity.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
81
	        + "-" + ((entity.getModelName() != null) ? entity.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
82
		    + "-" + ((entity.getModelNumber() != null ) ? entity.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
83
	        + "-" + entity.getID();
84
			productUrl = productUrl.replaceAll("/", "-");
85
			url = url + productUrl;
86
			url = url.replaceAll("-+", "-");
87
			return url;
88
 
89
		} catch (NullPointerException e) {
90
			return null;
91
		}
92
	}
93
 
94
	private List<String> getHandsetNames(String rawText)	{
95
 
96
		List<String> names = new ArrayList<String>();
97
 
98
		for (String namesWithSameBrand: rawText.trim().split("\n"))	{
99
			boolean isFirst = true;
100
			String brand = "";
101
 
102
			for(String name: namesWithSameBrand.trim().split(","))	{
103
				name = name.trim();
104
 
105
				if(isFirst)	{
106
					names.add(name);
107
					brand = name.split(" ")[0];
108
					isFirst = false;
109
				} else	{
110
					names.add(brand + " " + name);
111
				}
112
			}
113
		}
114
		return names;
115
	}
116
 
117
	public static void main(String[] args) {
118
		try {
5155 varun.gupt 119
			List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
120
			CompatibleAccessoriesIndexGenerator generator = new CompatibleAccessoriesIndexGenerator(entities);
5117 varun.gupt 121
			generator.generate();
122
		} catch (Exception e) {
123
			e.printStackTrace();
124
		}
125
	}
126
}