Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2319 rajveer 1
package in.shop2020.ui.util;
2
 
3
 
4
import in.shop2020.metamodel.core.Helpdoc;
5
import in.shop2020.metamodel.util.CreationUtils;
6
import in.shop2020.util.Utils;
7
 
8
import java.io.BufferedWriter;
9
import java.io.File;
10
import java.io.FileOutputStream;
11
import java.io.OutputStreamWriter;
12
import java.util.LinkedHashMap;
13
import java.util.Map;
2367 rajveer 14
import java.util.TreeMap;
2319 rajveer 15
 
16
import org.apache.velocity.Template;
17
import org.apache.velocity.VelocityContext;
18
import org.apache.velocity.app.Velocity;
19
 
20
 
21
/**
22
 * Command line utility to generate glossary files for our site
23
 * 
24
 * @author rajveer
25
 *
26
 */
27
public class HelpdocsGenerator {
28
	public static void main(String[] args) throws Exception {
29
		HelpdocsGenerator generator = new HelpdocsGenerator();
30
		generator.generateHelpdocFiles();
31
		generator.generateGlossaryFile();
32
	}
33
 
34
	private void generateGlossaryFile() {
35
		try {
36
			Map<Long, Helpdoc> helpdocs = CreationUtils.getHelpdocs();
37
			VelocityContext context = new VelocityContext();
38
			String templateFile = Utils.VTL_SRC_PATH + "glossary.vm";
39
			Map<String, Map<String, String>> params = new LinkedHashMap<String, Map<String,String>>();
2367 rajveer 40
			Map<String, String> section1 = new TreeMap<String, String>();
41
			Map<String, String> section2 = new TreeMap<String, String>();
42
			Map<String, String> section3 = new TreeMap<String, String>();
43
			Map<String, String> section4 = new TreeMap<String, String>();
2319 rajveer 44
			params.put("0 - 9", section1);
45
			params.put("A - H", section2);
46
			params.put("I - P", section3);
47
			params.put("Q - Z", section4);
48
			for(Helpdoc helpdoc: helpdocs.values()){
49
				String docname = helpdoc.getName();
50
				char firstChar = docname.toUpperCase().charAt(0);
51
				if(firstChar >= '0' && firstChar <= '9'){
52
					section1.put(docname, getFileName(docname));
53
				}
54
				if(firstChar >= 'A' && firstChar <= 'H'){
55
					section2.put(docname, getFileName(docname));
56
				}
57
				if(firstChar >= 'I' && firstChar <= 'P'){
58
					section3.put(docname, getFileName(docname));
59
				}
60
				if(firstChar >= 'Q' && firstChar <= 'Z'){
61
					section4.put(docname, getFileName(docname));
62
				}
63
			}
64
			context.put("params", params);
65
			String exportFileName = Utils.EXPORT_HELPDOCS_PATH + "glossary";
66
			File exportFile = new File(exportFileName);
67
			if(!exportFile.exists()) {
68
				exportFile.createNewFile();
69
			}
70
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));
71
			Template template = Velocity.getTemplate(templateFile);
72
			template.merge(context, writer);
73
			writer.flush();
74
			writer.close();
75
		} catch (Exception e) {
76
			// TODO Auto-generated catch block
77
			e.printStackTrace();
78
		}
79
	}
80
 
81
	private void generateHelpdocFiles() {
82
		try {
83
			Map<Long, Helpdoc> helpdocs = CreationUtils.getHelpdocs();
84
			VelocityContext context = new VelocityContext();
85
			String templateFile = Utils.VTL_SRC_PATH + "helpdoc.vm";
86
			for(Helpdoc helpdoc: helpdocs.values()){
87
				context.put("helpdoc", helpdoc);
88
				String exportFileName = Utils.EXPORT_HELPDOCS_PATH + getFileName(helpdoc.getName());
89
				File exportFile = new File(exportFileName);
90
				if(!exportFile.exists()) {
91
					exportFile.createNewFile();
92
				}
93
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));
94
				Template template = Velocity.getTemplate(templateFile);
95
				template.merge(context, writer);
96
				writer.flush();
97
				writer.close();				
98
			}
99
		} catch (Exception e) {
100
			// TODO Auto-generated catch block
101
			e.printStackTrace();
102
		}
103
	}
104
 
105
	private String getFileName(String name){
106
		return name.toLowerCase().replace(' ', '-').replaceAll("/", "");
107
	}
108
 
109
}