View Javadoc

1   package at.ac.tuwien.ifs.bpse.basic.export_import;
2   
3   import java.io.FileWriter;
4   import java.io.IOException;
5   import java.util.List;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
11  
12  
13  /**
14   * Simple implementation of the Export-Interface writing to a HTML-File.
15   * <b>Warning:</b> The implementation of HTML Generation in this class is only
16   * for extremly simple test purposes!! In a real-world project you would
17   * <i>never</i> generate HTML using String concatenation because this is far
18   * too error prone and unstable!<br>
19   * You could use template engines like Apache Velocity. This was not done here
20   * for keeping the code as simple and understandable as possible.
21   * 
22   * @author The SE-Team
23   * @version 1.0
24   * @see Export
25   */
26  public class HtmlExport implements Export {
27  
28  	/**
29  	 * Retrieves the logger for this class.
30  	 */
31  	private static Log log = LogFactory.getLog(HtmlExport.class);
32  
33  	/**
34  	 * Start of html document (header...).
35  	 */
36  	private String html_start;
37  
38  	/**
39  	 * End of html document.
40  	 */
41  	private String html_end;
42  
43  	/**
44  	 * Actual content of html document.
45  	 */
46  	private StringBuffer html_body;
47  
48  	/**
49  	 * Title of document.
50  	 */
51  	private String title;
52  
53  	/**
54  	 * Creates a new instance, nothing special here.
55  	 */
56  	public HtmlExport() {
57  		super();
58  	}
59  
60  	/**
61  	 * Used for display purposes in the UI models. Very important as it is
62  	 * implemented to stringify this object
63  	 * 
64  	 * @return String "HTML" for this export
65  	 * 
66  	 */
67  	public String toString() {
68  		return "HTML";
69  	}
70  
71  	/**
72  	 * This method is required to give the GUI components Feedback which File
73  	 * extension this Export Type creates.
74  	 * 
75  	 * @return String "html" for this export
76  	 */
77  	public String getExtension() {
78  		return "html";
79  	}
80  
81  	/**
82  	 * Returns the Title of the HTML-File.
83  	 * 
84  	 * @return The content of the title-tag.
85  	 */
86  	public String getTitle() {
87  		return title;
88  	}
89  
90  	/**
91  	 * Set the Title of the HTML-File.
92  	 * 
93  	 * @param title
94  	 *            The new content of the title-tag.
95  	 */
96  	public void setTitle(String title) {
97  		this.title = title;
98  	}
99  
100 	/**
101 	 * Initialise Document (generate new one).
102 	 */
103 	private void newDoc() {
104 		html_start = "<html>\n<head>\n<title>" + title + "</title>\n</head>\n"
105 				+ "<body>\n";
106 		html_body = new StringBuffer();
107 		html_end = "\n</body>\n</html>";
108 	}
109 
110 	/**
111 	 * Render html code for one student (actually one table row).
112 	 * 
113 	 * @param s
114 	 *            Student object
115 	 * @return html code of one Student data (table row)
116 	 */
117 	private String renderStudent(Student s) {
118 		log.debug("Rendering Student \"" + s.getFirstname() + " "
119 				+ s.getLastname() + "\"");
120 		StringBuffer h = new StringBuffer();
121 		h.append("<tr>\n");
122 		h.append("<td>" + s.getId() + "</td>\n");
123 		h.append("<td>" + s.getFirstname() + " " + s.getLastname() + "</td>\n");
124 		h.append("<td>" + s.getMatnr() + "</td>\n");
125 		h.append("<td>" + s.getEmail() + "</td>\n");
126 		h.append("</tr>\n");
127 		return h.toString();
128 	}
129 
130 	/**
131 	 * Export list of students to html document. This is the implemented Method
132 	 * from the Export interface.
133 	 * 
134 	 * @param studenten
135 	 *            List of students
136 	 * @param filename
137 	 *            filename of html document to be generated
138 	 */
139 	public void write(List<Student> studenten, String filename)
140 			throws IOException {
141 		log.info("Exporting " + studenten.size() + " Students to HTML \""
142 				+ filename + "\"");
143 		newDoc();
144 		html_body.append("<table width=\"75%\" border=\"1\">\n");
145 		html_body.append("<tr>\n");
146 		html_body
147 				.append("<th>ID</th>\n<th>Name</th>\n<th>Matrikelnummer</th>\n<th>Email</th>\n");
148 		html_body.append("</tr>\n");
149 		for (Student s: studenten) {
150 			html_body.append(renderStudent(s));
151 		}
152 		html_body.append("</table>");
153 		log.debug("Writing HTML File");
154 		FileWriter fw = new FileWriter(filename);
155 		fw.write(html_start + html_body.toString() + html_end);
156 		fw.close();
157 		log.info("Finished Export");
158 	}
159 
160 }