1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.seasar.tuigwaa.view;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.util.Properties;
25
26 import javax.servlet.RequestDispatcher;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpServletResponseWrapper;
33 import javax.servlet.jsp.JspException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.velocity.Template;
38 import org.apache.velocity.VelocityContext;
39 import org.apache.velocity.app.VelocityEngine;
40 import org.apache.velocity.exception.ParseErrorException;
41 import org.apache.velocity.exception.ResourceNotFoundException;
42 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
43 import org.seasar.tuigwaa.controller.ControllerService;
44 import org.seasar.tuigwaa.database.function.SearchExeFunction;
45 import org.seasar.tuigwaa.database.function.UpdateExeFunction;
46 import org.seasar.tuigwaa.model.core.TgwEntity;
47 import org.seasar.tuigwaa.plugin.database.component.ResultDto;
48 import org.seasar.tuigwaa.util.TgwContext;
49 import org.seasar.tuigwaa.util.TgwNameUtils;
50 import org.seasar.tuigwaa.util.TgwUtils;
51
52 import com.isenshi.util.ResourceUtils;
53 import com.isenshi.util.extlib.JavascriptValidatorUtils;
54
55 /***
56 * @author nishioka
57 */
58 public class HtmlServiceImpl implements HtmlService {
59
60 private VelocityEngine formTemplateEngine = new VelocityEngine();
61
62
63
64 private Template defaultFormTemplate;
65
66 private Template searchFormTemplate;
67
68 private Log log = LogFactory.getLog(getClass().getName());
69
70 public HtmlServiceImpl() {
71 Properties props = new Properties();
72 try {
73 props.load(ResourceUtils.getInputStream("velocity.properties"));
74 formTemplateEngine.init(props);
75
76
77
78 defaultFormTemplate = formTemplateEngine.getTemplate(
79 "template/parts/form.vm", "Windows-31J");
80 searchFormTemplate = formTemplateEngine.getTemplate(
81 "template/parts/sform.vm", "Windows-31J");
82 } catch (IOException e) {
83
84 log.warn("veloctiy plugin load execpetion" + e.getMessage());
85 } catch (Exception e) {
86
87 log.warn("velocity not initialized" + e.getMessage());
88 }
89 }
90
91 public String toHtml(ResultDto dto) {
92 return null;
93 }
94
95 public String toHtml(FormComponent form) {
96 String ret = null;
97 try {
98 StringWriter sw = new StringWriter();
99 VelocityContext context = new VelocityContext();
100 context.put("form", form);
101 if (form instanceof SearchFormComponent) {
102 searchFormTemplate.merge(context, sw);
103 } else {
104 defaultFormTemplate.merge(context, sw);
105 }
106 ret = sw.toString();
107 sw.flush();
108 } catch (ResourceNotFoundException e) {
109 log.warn(e.getMessage());
110 } catch (ParseErrorException e) {
111 log.warn(e.getMessage());
112 } catch (Exception e) {
113 log.warn(e.getMessage());
114 }
115 return ret;
116 }
117
118 public EntityFormComponent createFormComponent(TgwEntity entity,
119 Object valueObject) {
120
121 String javascript = createJavascriptValidator(entity);
122
123 String actionName = TgwNameUtils.toActionName(entity);
124 String formName = TgwNameUtils.toFormName(entity);
125
126 EntityFormComponent component = new EntityFormComponent(javascript,
127 entity, valueObject);
128 component.setActionName(actionName);
129 component.setFormName(formName);
130
131 return component;
132 }
133
134 public CustomFormComponent createCustomFormComponent(TgwEntity entity,
135 Object valueObject, UpdateExeFunction updateExe) {
136
137 ControllerService strutsService = (ControllerService) SingletonS2ContainerFactory
138 .getContainer().getComponent(ControllerService.class);
139 String actionName = strutsService
140 .getCustomActionName(entity, updateExe);
141 String formName = strutsService.getCustomFormName(entity, updateExe);
142
143 String javascript = createJavascriptValidator(formName);
144
145 CustomFormComponent cform = new CustomFormComponent(javascript, entity,
146 valueObject, updateExe);
147 cform.setActionName(actionName);
148 cform.setFormName(formName);
149 return cform;
150 }
151
152 public SearchFormComponent createSearchFormComponent(TgwEntity entity,
153 SearchExeFunction exeFunction) {
154 SearchFormComponent component = new SearchFormComponent(entity,
155 exeFunction);
156 ControllerService strutsService = (ControllerService) SingletonS2ContainerFactory
157 .getContainer().getComponent(ControllerService.class);
158 String actionName = strutsService.getSearchActionName(entity,
159 exeFunction);
160 String formName = strutsService.getSearchFormName(entity, exeFunction);
161 component.setActionName(actionName);
162 component.setFormName(formName);
163 return component;
164 }
165
166 public TableFormComponent createTableFormComponent(TgwEntity entity, UpdateExeFunction optionFunction) {
167
168 EntityFormComponent form = null;
169 String actionName = null;
170 String formName = null;
171 if (optionFunction == null) {
172 form = createFormComponent(entity, null);
173 actionName = "/saveRecordTable";
174 formName = "tableFormForm";
175 } else {
176 form = createCustomFormComponent(entity, null, optionFunction);
177 String origActionName = form.getActionName();
178 actionName = TgwNameUtils
179 .toCustomTableSaveActionName(origActionName);
180 String origFormName = form.getFormName();
181 formName = TgwNameUtils.toCustomTableFormName(origFormName);
182 }
183 String siteName = entity.getDomainName();
184 return new TableFormComponent(siteName, form, actionName, formName);
185 }
186
187 private String createJavascriptValidator(TgwEntity entity) {
188 return createJavascriptValidator(TgwNameUtils.toFormName(entity));
189 }
190
191 private String createJavascriptValidator(String formName) {
192
193 HttpServletRequest request = TgwContext.getRequest();
194 HttpServletResponse response = TgwContext.getResponse();
195
196 String script = null;
197 try {
198 script = JavascriptValidatorUtils.getScript(TgwUtils
199 .getTuigwaaActionServlet(), request, response, formName);
200 } catch (IllegalStateException e) {
201 e.printStackTrace();
202 } catch (IllegalArgumentException e) {
203 e.printStackTrace();
204 } catch (IOException e) {
205 e.printStackTrace();
206 } catch (JspException e) {
207 e.printStackTrace();
208 }
209 return script;
210 }
211
212 public String includeJsp(String path) {
213 path = "/WEB-INF/classes/" + path;
214
215 StringWriter swriter = new StringWriter();
216 ServletContext context = TgwContext.getServletContext();
217 RequestDispatcher rd = context.getRequestDispatcher(path);
218 PrintWriter pw = new PrintWriter(swriter);
219
220 HttpServletRequest request = TgwContext.getRequest();
221 HttpServletResponse response = TgwContext.getResponse();
222
223 try {
224 rd.include(request, new BufferedResponse(response, pw));
225 } catch (ServletException e) {
226 e.printStackTrace();
227 } catch (IOException e) {
228 e.printStackTrace();
229 }
230
231 return swriter.toString();
232 }
233
234 public static class BufferedResponse extends HttpServletResponseWrapper {
235
236 private PrintWriter writer;
237
238 public BufferedResponse(HttpServletResponse response, PrintWriter writer) {
239 super(response);
240 this.writer = writer;
241 }
242
243 public PrintWriter getWriter() throws IOException {
244 return writer;
245 }
246
247 public ServletOutputStream getOutputStream() throws IOException {
248 return super.getOutputStream();
249 }
250 }
251 }