1 package org.gnomekr.potron.web.view;
2
3 import static org.gnomekr.potron.util.NavigationCases.NAV_TEMPLATE_ADD;
4 import static org.gnomekr.potron.util.NavigationCases.NAV_TEMPLATE_UPDATE;
5 import static org.gnomekr.potron.util.NavigationCases.NAV_TEMPLATE_LIST;
6 import static org.gnomekr.potron.util.PotronConstants.DEFAULT_FILE_ENCODING;
7
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.Reader;
11 import java.io.UnsupportedEncodingException;
12 import java.util.ArrayList;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.faces.application.FacesMessage;
18 import javax.faces.context.FacesContext;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.myfaces.custom.fileupload.UploadedFile;
23 import org.apache.myfaces.util.MessageUtils;
24 import org.gnomekr.potron.data.Project;
25 import org.gnomekr.potron.data.Template;
26 import org.gnomekr.potron.parser.ParseException;
27 import org.gnomekr.potron.service.IProjectManager;
28
29 /***
30 * TemplateView.java
31 * @author Xavier Cho
32 * @version $Revision 1.1 $ $Date: 2005/09/11 05:49:45 $
33 * @jsf.bean
34 * name = "template"
35 * scope = "request"
36 * @jsf.navigation
37 * from = "*"
38 * to = "/admin/template/add.jsf"
39 * result = "template.add"
40 * @jsf.navigation
41 * from = "*"
42 * to = "/admin/template/update.jsf"
43 * result = "template.update"
44 * @jsf.navigation
45 * from = "*"
46 * to = "/admin/template/list.jsf"
47 * result = "template.list"
48 * @jsf.navigation
49 * from = "*"
50 * to = "/user/template.jsf"
51 * result = "template.info"
52 */
53 public class TemplateView extends AbstractFacesView {
54
55 private IProjectManager manager;
56
57 private long id = -1;
58 private String projectId;
59 private String name;
60 private Date registeredDate;
61 private String comment;
62 private String encoding = DEFAULT_FILE_ENCODING;
63 private String description;
64 private Project project;
65 private UploadedFile file;
66
67 /***
68 * @return
69 * @jsf.managed-property
70 * value = "#{projectManager}"
71 */
72 public IProjectManager getManager() {
73 return manager;
74 }
75
76 public String addTemplate() {
77 FacesContext context = FacesContext.getCurrentInstance();
78 Map params = context.getExternalContext().getRequestParameterMap();
79
80 if (projectId == null) {
81 this.projectId = (String) params.get("projectId");
82 }
83
84 Reader reader;
85
86 try {
87 reader = new InputStreamReader(file.getInputStream(), encoding);
88 manager.addTemplate(projectId, name, description, reader);
89 } catch (UnsupportedEncodingException e) {
90 MessageUtils.addMessage(
91 FacesMessage.SEVERITY_WARN,
92 "error.unknown.encoding",
93 null);
94
95 return NAV_TEMPLATE_ADD;
96 } catch (IOException e) {
97 Log log = LogFactory.getLog(getClass());
98 if (log.isErrorEnabled()) {
99 log.error(e);
100 }
101
102 MessageUtils.addMessage(
103 FacesMessage.SEVERITY_ERROR,
104 "error.file.upload",
105 null);
106
107 return NAV_TEMPLATE_ADD;
108 } catch (ParseException e) {
109 Log log = LogFactory.getLog(getClass());
110 if (log.isErrorEnabled()) {
111 log.error(e);
112 }
113
114 MessageUtils.addMessage(
115 FacesMessage.SEVERITY_ERROR,
116 "error.template.parse",
117 null);
118
119 return NAV_TEMPLATE_ADD;
120 }
121
122 MessageUtils.addMessage(
123 FacesMessage.SEVERITY_INFO,
124 "msg.template.add.successful",
125 null);
126
127 refreshNavigationTree();
128
129 return NAV_TEMPLATE_LIST;
130 }
131
132 public String updateTemplate() {
133 if (file == null || file.getSize() <= 0) {
134 manager.updateTemplate(id, name, description, comment);
135 } else {
136 Reader reader;
137
138 try {
139 reader = new InputStreamReader(file.getInputStream(), encoding);
140 manager.updateTemplate(id, name, description, reader);
141 } catch (UnsupportedEncodingException e) {
142 MessageUtils.addMessage(
143 FacesMessage.SEVERITY_WARN,
144 "error.unknown.encoding",
145 null);
146
147 return NAV_TEMPLATE_UPDATE;
148 } catch (IOException e) {
149 Log log = LogFactory.getLog(getClass());
150 if (log.isErrorEnabled()) {
151 log.error(e);
152 }
153
154 MessageUtils.addMessage(
155 FacesMessage.SEVERITY_ERROR,
156 "error.file.upload",
157 null);
158
159 return NAV_TEMPLATE_UPDATE;
160 } catch (ParseException e) {
161 Log log = LogFactory.getLog(getClass());
162 if (log.isErrorEnabled()) {
163 log.error(e);
164 }
165
166 MessageUtils.addMessage(
167 FacesMessage.SEVERITY_ERROR,
168 "error.template.parse",
169 null);
170
171 return NAV_TEMPLATE_UPDATE;
172 }
173 }
174
175 MessageUtils.addMessage(
176 FacesMessage.SEVERITY_INFO,
177 "msg.template.update.successful",
178 null);
179
180 refreshNavigationTree();
181
182 return NAV_TEMPLATE_LIST;
183 }
184
185 public String removeTemplate() {
186 manager.removeTemplate(id);
187
188 MessageUtils.addMessage(
189 FacesMessage.SEVERITY_INFO,
190 "msg.template.remove.successful",
191 null);
192
193 refreshNavigationTree();
194
195 return NAV_TEMPLATE_LIST;
196 }
197
198 public List<Template> getTemplates() {
199 FacesContext context = FacesContext.getCurrentInstance();
200 Map params = context.getExternalContext().getRequestParameterMap();
201
202 if (projectId == null) {
203 this.projectId = (String) params.get("projectId");
204 }
205
206 this.project = manager.getProject(projectId);
207
208 return new ArrayList<Template>(project.getTemplates());
209 }
210
211 /***
212 * @param manager The manager to set.
213 */
214 public void setManager(IProjectManager manager) {
215 this.manager = manager;
216 }
217
218 /***
219 * @return
220 */
221 public String getComment() {
222 return comment;
223 }
224
225 /***
226 * @param comment
227 */
228 public void setComment(String comment) {
229 this.comment = comment;
230 }
231
232 /***
233 * @return
234 */
235 public String getDescription() {
236 return description;
237 }
238
239 /***
240 * @param description
241 */
242 public void setDescription(String description) {
243 this.description = description;
244 }
245
246 /***
247 * @return
248 */
249 public String getEncoding() {
250 return encoding;
251 }
252
253 /***
254 * @param encoding
255 */
256 public void setEncoding(String encoding) {
257 this.encoding = encoding;
258 }
259
260 /***
261 * @return
262 */
263 public UploadedFile getFile() {
264 return file;
265 }
266
267 /***
268 * @param file
269 */
270 public void setFile(UploadedFile file) {
271 this.file = file;
272 }
273
274 /***
275 * @return
276 */
277 public long getId() {
278 return id;
279 }
280
281 /***
282 * @param id
283 */
284 public void setId(long id) {
285 this.id = id;
286
287 Template template = manager.getTemplate(id);
288
289 if (template != null) {
290 this.name = template.getName();
291 this.description = template.getDescription();
292 this.registeredDate = template.getRegisteredDate();
293 this.comment = template.getComment();
294 this.project = template.getProject();
295 this.projectId = project.getId();
296 }
297 }
298
299 /***
300 * @return Returns the projectId.
301 */
302 public String getProjectId() {
303 return projectId;
304 }
305
306 /***
307 * @param projectId The projectId to set.
308 */
309 public void setProjectId(String projectId) {
310 this.projectId = projectId;
311 }
312
313 /***
314 * @return
315 */
316 public String getName() {
317 return name;
318 }
319
320 /***
321 * @param name
322 */
323 public void setName(String name) {
324 this.name = name;
325 }
326
327 /***
328 * @return
329 */
330 public Project getProject() {
331 return project;
332 }
333
334 /***
335 * @param project
336 */
337 public void setProject(Project project) {
338 this.project = project;
339 }
340
341 /***
342 * @return
343 */
344 public Date getRegisteredDate() {
345 return registeredDate;
346 }
347
348 /***
349 * @param registeredDate
350 */
351 public void setRegisteredDate(Date registeredDate) {
352 this.registeredDate = registeredDate;
353 }
354 }