Subversion Repositories SmartDukaan

Rev

Rev 5566 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5566 amit.gupta 1
package in.shop2020.util;
2
 
3
import java.text.MessageFormat;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.HashMap;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10
 
11
 
12
 
13
public class LevelHierarchy {
14
	private static final int MAX_LIMIT = 60;
15
 
16
 
17
	private String alphabeticalTemplate = "Product starting with <b>{0}</b>";
18
	private String levelOneTitle = "Accessories Compatibility Index";
19
	//All urls will start with <i>generated</i> for SEOs 
20
	private String levelOneUrlTemplate;
21
	private String levelTwoTitle;
22
	private String path = Utils.EXPORT_PATH;
23
	private List<String> data; 
5600 amit.gupta 24
	private Map<String, List<List<String>>> dataMap;
5566 amit.gupta 25
	private String[] indentation = {"", "    ", "        ", "            ","                "};
26
 
27
	public LevelHierarchy(String alphabeticalTemplate, String levelOneTitle,
5600 amit.gupta 28
			String levelTwoTitle, Map<String, List<List<String>>> dataMap) {
5566 amit.gupta 29
		super();
30
		this.alphabeticalTemplate = alphabeticalTemplate;
31
		this.levelOneTitle = levelOneTitle;
32
		this.levelTwoTitle = levelTwoTitle;
33
		this.dataMap = dataMap;
34
	}
35
 
36
	public void generatePages() {
37
		String hyphenatedTitle = levelOneTitle.toLowerCase().replaceAll(" +", "-");
38
		StringBuilder sb = new StringBuilder();
39
		String levelOneFileName = Utils.EXPORT_PARTNERS_CONTENT_PATH + hyphenatedTitle + ".html";
40
		List<String> sortedList = new ArrayList<String>(dataMap.keySet()); 
41
		Collections.sort(sortedList);
42
		for(String key : sortedList){
5600 amit.gupta 43
			List<List<String>> list = dataMap.get(key);
5566 amit.gupta 44
			sb.append("<h3>").append(MessageFormat.format(alphabeticalTemplate, key)).append("</h3>");
5600 amit.gupta 45
			int counts =  list.size()/MAX_LIMIT + (list.size()%MAX_LIMIT==0 ? 0 : 1);
5566 amit.gupta 46
			counts = counts > MAX_LIMIT ? MAX_LIMIT : counts;
5600 amit.gupta 47
			Iterator<List<String>> it = list.listIterator();
5566 amit.gupta 48
			for(int i=1; i<=counts; i++){
49
				sb.append("<a style='padding:5px 3px 15px 3px;display:inline-block' href='/generated/").append(hyphenatedTitle)
50
				.append("-" + key).append("-").append(i).append("'>").append(i).append("</a> ");
51
				StringBuilder sb1 = new StringBuilder();
52
				String levelTwoFileName = Utils.EXPORT_PARTNERS_CONTENT_PATH + hyphenatedTitle + "-" + key + "-" + i + ".html";
53
				for(int j=0; j<MAX_LIMIT; j++){
54
					if(it.hasNext()){
5600 amit.gupta 55
						List<String> tuple = it.next();
56
						String name = tuple.get(0);
57
						String url = tuple.get(1);
5566 amit.gupta 58
						sb1.append(indentation[1] + "<div>\n");
59
						sb1.append(indentation[2] + "<a href='" + url + "'>" + name + "</a>\n");
60
						sb1.append(indentation[1] + "</div>\n");
61
					}else {
62
						break;
63
					}
64
				}
65
				try	{
66
					DBUtils.store(sb1.toString(), levelTwoFileName);
67
 
68
				} catch (Exception e) {
69
					e.printStackTrace();
70
				}
71
			}
72
		}
73
		try	{
74
			DBUtils.store(sb.toString(), levelOneFileName);
75
 
76
		} catch (Exception e) {
77
			e.printStackTrace();
78
		}
79
 
80
	}
81
 
82
	public String getAlphabeticalTemplate() {
83
		return alphabeticalTemplate;
84
	}
85
	public List<String> getData() {
86
		return data;
87
	}
88
	public void setData(List<String> data) {
89
		this.data = data;
90
	}
91
	public void setAlphabeticalTemplate(String alphabeticalTemplate) {
92
		this.alphabeticalTemplate = alphabeticalTemplate;
93
	}
94
	public String getLevelOneTitle() {
95
		return levelOneTitle;
96
	}
97
	public void setLevelOneTitle(String levelOneTitle) {
98
		this.levelOneTitle = levelOneTitle;
99
	}
100
	public String getLevelOneUrlTemplate() {
101
		return levelOneUrlTemplate;
102
	}
103
	public void setLevelOneUrlTemplate(String levelOneUrlTemplate) {
104
		this.levelOneUrlTemplate = levelOneUrlTemplate;
105
	}
106
	public String getLevelTwoTitle() {
107
		return levelTwoTitle;
108
	}
109
	public void setLevelTwoTitle(String levelTwoTitle) {
110
		this.levelTwoTitle = levelTwoTitle;
111
	}
112
 
5600 amit.gupta 113
	public Map<String, List<List<String>>> getDataMap() {
5566 amit.gupta 114
		return dataMap;
115
	}
116
 
5600 amit.gupta 117
	public void setDataMap(Map<String, List<List<String>>> dataMap) {
5566 amit.gupta 118
		this.dataMap = dataMap;
119
	}
120
	public String getPath() {
121
		return path;
122
	}
123
	public void setPath(String path) {
124
		this.path = path;
125
	}
126
 
127
	public static void main(String args[]){
128
		String alphabeticalTemplate = "Product starting with <b>{0}</b>";
129
		String levelOneTitle = "Accessories Compatibility Index";
130
		String levelTwoTitle = "";
5600 amit.gupta 131
		Map<String, List<List<String>>>  dataMap = new HashMap<String, List<List<String>>> ();
132
		List<List<String>> m1 = new ArrayList<List<String>>();
133
		List<List<String>> m2 = new ArrayList<List<String>>();
134
		List<String>m11 = new ArrayList<String>();
135
		List<String>m12 = new ArrayList<String>();
136
		m11.add("abc");
137
		m11.add("abc-d");
138
		m12.add("abc");
139
		m12.add("abc-d");
140
 
5566 amit.gupta 141
		dataMap.put("a", m1);
142
		LevelHierarchy lh = new LevelHierarchy(alphabeticalTemplate, levelOneTitle, levelTwoTitle, dataMap);
143
		lh.generatePages();
144
	}
145
 
146
}