1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.controller;
17
18 import java.util.Iterator;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionMessage;
27 import org.apache.struts.action.ActionMessages;
28 import org.apache.struts.actions.MappingDispatchAction;
29 import org.seasar.tuigwaa.model.ModelService;
30 import org.seasar.tuigwaa.model.core.TgwAttribute;
31 import org.seasar.tuigwaa.model.core.TgwEntity;
32 import org.seasar.tuigwaa.system.Constants;
33 import org.seasar.tuigwaa.util.TgwContext;
34 import org.seasar.tuigwaa.util.TgwPathResolver;
35 import org.seasar.tuigwaa.view.EntityFormComponent;
36 import org.seasar.tuigwaa.view.HtmlService;
37
38 import com.isenshi.util.HtmlBuffer;
39 import com.isenshi.util.ResourceUtils;
40
41 public class DesignTemplateAction extends MappingDispatchAction {
42
43 private HtmlService htmlService;
44
45 private ModelService model;
46
47 public void setHtmlService(HtmlService htmlService) {
48 this.htmlService = htmlService;
49 }
50
51 public void setModel(ModelService model) {
52 this.model = model;
53 }
54
55 public ActionForward create(ActionMapping mapping, ActionForm form,
56 HttpServletRequest request, HttpServletResponse response)
57 throws Exception {
58
59
60 String entityName = request.getParameter(Constants.PARAM_ENTITY_NAME);
61 String siteName = TgwContext.getSiteName();
62 String type = request.getParameter("type");
63 TgwEntity entity = model.getEntity(siteName, entityName);
64
65
66 String template = null;
67 if ("form".equals(type)) {
68 template = createFormTemplate(entity);
69 } else if("table".equals(type)){
70 template = createTableTemplate(entity);
71 } else{
72 template = "";
73 }
74
75
76 DesignTemplateForm dform = new DesignTemplateForm("", type, entity
77 .getName(), template);
78 bindForm(request, dform, entity);
79 return mapping.findForward("success");
80 }
81
82 public ActionForward save(ActionMapping mapping, ActionForm form,
83 HttpServletRequest request, HttpServletResponse response)
84 throws Exception {
85
86
87 DesignTemplateForm dform = (DesignTemplateForm) form;
88 String siteName = TgwContext.getSiteName();
89 String type = dform.getType();
90 String name = dform.getName();
91 String template = dform.getTemplate();
92 String tableName = dform.getTableName();
93
94
95
96 TgwEntity entity = model.getEntity(siteName, tableName);
97 String path = TgwPathResolver.getTemplatePath(siteName, tableName,
98 type, name, null);
99 ResourceUtils.writeContent(path, template);
100
101
102
103 ActionMessages msgs = new ActionMessages();
104 msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("msg.saved"));
105 saveMessages(request, msgs);
106 bindForm(request, dform, entity);
107 return mapping.findForward("success");
108 }
109
110 public ActionForward edit(ActionMapping mapping, ActionForm form,
111 HttpServletRequest request, HttpServletResponse response)
112 throws Exception {
113
114
115 String templateName = request.getParameter(Constants.PARAM_TEMPLATE);
116 String entityName = request.getParameter(Constants.PARAM_ENTITY_NAME);
117 String type = request.getParameter("type");
118 String siteName = TgwContext.getSiteName();
119
120
121 TgwEntity entity = model.getEntity(siteName, entityName);
122 String path = TgwPathResolver.getTemplatePath(siteName, entityName,
123 type, templateName, null);
124 String content = ResourceUtils.readContent(path);
125
126
127 DesignTemplateForm dform = new DesignTemplateForm(templateName, type,
128 entityName, content);
129 bindForm(request, dform, entity);
130 return mapping.findForward("success");
131 }
132
133
134
135 private void bindForm(HttpServletRequest request, DesignTemplateForm dform,
136 TgwEntity entity) {
137 dform.setEntity(entity);
138 request.setAttribute("designTemplateForm", dform);
139 }
140
141 private String createFormTemplate(TgwEntity entity) {
142 EntityFormComponent formComponent = htmlService.createFormComponent(
143 entity, null);
144 formComponent.setNeedJavaScript(false);
145 formComponent.setJspTemplate(true);
146
147
148
149 String forwardPageName = "$!{bean.forwardPageName}";
150 formComponent.setForwardPageName(forwardPageName);
151 formComponent.setActionPath("${bean.actionPath}");
152
153 return htmlService.toHtml(formComponent);
154 }
155
156 private String createTableTemplate(TgwEntity entity) {
157 HtmlBuffer buf = new HtmlBuffer();
158
159 buf.appendBody(ResourceUtils
160 .readContent("template/parts/tablemetadata.vm"));
161
162 buf.appendBodyLine("############################################"
163 + "\n#### Table Data"
164 + "\n############################################");
165
166 buf.appendStartTag("table");
167
168 buf.appendStartTag("thead");
169 buf.appendStartTag("tr");
170 Iterator itr = entity.getAllFieldList().iterator();
171 while (itr.hasNext()) {
172 TgwAttribute attr = (TgwAttribute) itr.next();
173 String displayName = attr.getDisplayName();
174 buf.appendTh(displayName);
175 }
176 buf.endTag();
177 buf.endTag();
178
179 buf.appendStartTag("tbody");
180
181 buf.appendBodyLine("#foreach ($elem in $bean.resultDataTable.dataList)");
182 buf.appendStartTag("tr");
183 itr = entity.getAllFieldList().iterator();
184 while (itr.hasNext()) {
185 TgwAttribute attr = (TgwAttribute) itr.next();
186 String name = attr.getName();
187 String test = "$elem." + name;
188 buf.appendTd(test);
189 }
190 buf.endTag();
191
192 buf.appendBodyLine("#end");
193
194 buf.endTag();
195 buf.endTag();
196 buf.appendBody("");
197
198 return buf.toString();
199 }
200 }