1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.view;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.seasar.tuigwaa.controller.config.ValidatorUtils;
25 import org.seasar.tuigwaa.model.common.EntityUtils;
26 import org.seasar.tuigwaa.model.core.TgwAttribute;
27 import org.seasar.tuigwaa.model.core.TgwEntity;
28 import org.seasar.tuigwaa.model.core.impl.FkAttribute;
29 import org.seasar.tuigwaa.plugin.TgwPluginUtils;
30 import org.seasar.tuigwaa.system.Constants;
31 import org.seasar.tuigwaa.util.TgwContext;
32 import org.seasar.tuigwaa.util.TgwNameUtils;
33
34 import com.isenshi.util.HtmlBuffer;
35
36 public class EntityFormComponent extends AbstractGenericFormComponent {
37
38 private Object valueObject;
39
40 private Map bindingFkObjMap;
41
42 private String prefix;
43
44 private String extraHidden = "";
45
46 private boolean smallFormFlag = false;
47
48 private boolean formclear;
49
50 public EntityFormComponent(String javascript, TgwEntity entity,
51 Object object) {
52 super(entity, javascript);
53 this.valueObject = object;
54 }
55
56
57
58 public String getExtraHidden() {
59 return extraHidden;
60 }
61
62 public void setBindingFkObjMap(Map bindingFkObjMap) {
63 this.bindingFkObjMap = bindingFkObjMap;
64 }
65
66 public Map getBindingFkObjMap() {
67 return bindingFkObjMap;
68 }
69
70 public void setPrefix(String prefix) {
71 this.prefix = prefix;
72 }
73
74 public void setValueObject(Object valueObject) {
75 this.valueObject = valueObject;
76 }
77
78 public final Object getValueObject() {
79 return valueObject;
80 }
81
82 public void setSmallFormFlag(boolean smallFormFlag) {
83 this.smallFormFlag = smallFormFlag;
84 }
85
86 public String getDisplayName() {
87 String displayName = super.getDisplayName();
88 if (displayName == null) {
89 return getEntity().getDisplayName();
90 }
91 return displayName;
92 }
93
94
95
96
97
98 protected String getOnSubmitFunction() {
99 return ValidatorUtils.getOnSumbmitForm(getFormName());
100 }
101
102 protected boolean isMultiPart() {
103 return EntityUtils.isExistFileField(getEntity());
104 }
105
106 private static final String SBACK_PAGE = "_SRESULTBACK_";
107
108 protected Map getHiddenProperties() {
109 Map hiddenProps = new HashMap();
110 hiddenProps.put(Constants.PARAM_PAGENAME, getForwardPageName());
111
112
113 hiddenProps.put(Constants.PARAM_ENTITY_NAME, getEntity().getName());
114
115 if (valueObject != null) {
116 hiddenProps.put(EntityUtils.ESCAPED_ID, "" + getId());
117 } else if (isJspTemplate()) {
118 String jspId = "$!{" + Constants.RATTR_VALUEOBJECT + ".id}";
119 hiddenProps.put(EntityUtils.ESCAPED_ID, jspId);
120 }
121
122 if (SBACK_PAGE.equals(getForwardPageName())) {
123 String searchRedirectUrl = getSearchRedirectUrl();
124 if (searchRedirectUrl != null && !"".equals(searchRedirectUrl)) {
125 hiddenProps.put(Constants.PARAM_SEARCH_REDIREDT_URL,
126 searchRedirectUrl);
127 }
128 }
129 return hiddenProps;
130 }
131
132 public String getSearchRedirectUrl() {
133 return TgwContext.getRequest().getParameter(
134 Constants.PARAM_SEARCH_REDIREDT_URL);
135 }
136
137
138
139 public List getRowElements(String prefix, Object valueObj) {
140 setPrefix(prefix);
141 setValueObject(valueObj);
142 List list = new ArrayList();
143 Iterator itr = getEntity().getFieldIterator();
144 while (itr.hasNext()) {
145 TgwAttribute attr = (TgwAttribute) itr.next();
146 FormElement elem = getRowElement(attr);
147 if (elem != null) {
148 list.add(elem);
149 }
150 }
151 return list;
152 }
153
154 public List getFormElements() {
155 List list = new ArrayList();
156 Iterator itr = getEntity().getFieldIterator();
157 while (itr.hasNext()) {
158 TgwAttribute attr = (TgwAttribute) itr.next();
159 FormElement elem = createFormElement(attr);
160 if (elem != null) {
161 list.add(elem);
162 }
163 String hidden = createExtraHidden(attr);
164 addExtraHidden(hidden);
165 }
166 return list;
167 }
168
169 protected void addExtraHidden(String hidden) {
170 if (hidden != null) {
171 extraHidden += hidden;
172 }
173 }
174
175 public Object clearValueObject(){
176 Object obj = valueObject;
177 valueObject = null;
178 return obj;
179 }
180
181 public FormElement getFormElement(String attrName) {
182 return createFormElement(getEntity().getField(attrName));
183 }
184
185 protected FormElement createFormElement(TgwAttribute attr) {
186
187 if (EntityUtils.isBuiltinAttribute(attr)
188 || EntityUtils.oneToManyFlag(attr)) {
189 return null;
190 }
191
192 String label = attr.getDisplayName();
193 String name = getElementName(attr);
194 AttributeFormGenerateVisitor visitor = null;
195 if (smallFormFlag) {
196 visitor = AttributeFormGenerateVisitor.createTableForm(valueObject);
197 } else if (isJspTemplate()) {
198 visitor = AttributeFormGenerateVisitor.MACRO_INSTANCE;
199 } else {
200 visitor = AttributeFormGenerateVisitor.create(valueObject,
201 bindingFkObjMap);
202 }
203 String element = (String) attr.accept(visitor, name);
204 if (element == null) {
205 return null;
206 }
207
208 boolean required = attr.isRequired();
209 String description = attr.getDescription();
210
211 return new FormElement(attr.getName(), name, label, element, required,
212 description);
213 }
214
215
216
217 public String getName() {
218 return getEntity().getDisplayName();
219 }
220
221 public Map getAttrNameMap() {
222 Iterator itr = getEntity().getFieldIterator();
223 Map map = new HashMap();
224 while (itr.hasNext()) {
225 TgwAttribute attr = (TgwAttribute) itr.next();
226 String newName = getElementName(attr);
227 map.put(attr.getName(), newName);
228 }
229 return map;
230 }
231
232 public void setFormclear(boolean formclear) {
233 this.formclear = formclear;
234 }
235
236 public boolean isFormclear() {
237 return formclear;
238 }
239
240
241
242
243
244
245
246 private String getElementName(TgwAttribute field) {
247 if (EntityUtils.needConverter(field)) {
248 return toDynaConverterName(field);
249 } else {
250 return toDynaPropertyName(field);
251 }
252 }
253
254 private String toDynaPropertyName(TgwAttribute field) {
255 String name = TgwNameUtils.toDynaPropertyName(field.getName());
256 return addPrefix(name);
257 }
258
259 private String toDynaConverterName(TgwAttribute field) {
260 String name = TgwNameUtils.toDynaConverterName(field.getName());
261 return addPrefix(name);
262 }
263
264 private String addPrefix(String name) {
265 if (prefix == null) {
266 return name;
267 }
268 return prefix + name;
269 }
270
271 protected String getId() {
272 return String.valueOf(EntityUtils.getId(valueObject));
273 }
274
275 protected FormElement getRowElement(TgwAttribute attr) {
276 return createFormElement(attr);
277 }
278
279 public String createExtraHidden(TgwAttribute attr) {
280 if (attr instanceof FkAttribute) {
281 FkAttribute fkField = (FkAttribute) attr;
282 if (bindingFkObjMap != null) {
283 Object bindingFkObj = bindingFkObjMap.get(attr.getName());
284 if (bindingFkObj != null) {
285
286 Long id = EntityUtils.getId(bindingFkObj);
287 String bindingName = TgwPluginUtils
288 .createEscapeEntityBindingName(fkField
289 .getRefEntityName(), fkField.getName());
290 HtmlBuffer buf = new HtmlBuffer();
291 buf.appendHidden(getElementName(attr), String.valueOf(id));
292 buf.appendHidden(bindingName, String.valueOf(id));
293 return buf.toString();
294 }
295 }
296 }
297 return null;
298 }
299
300
301 }