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.Calendar;
19 import java.util.Date;
20 import java.util.Iterator;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.apache.commons.beanutils.ConvertUtils;
25 import org.apache.struts.action.ActionMapping;
26 import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
27 import org.seasar.tuigwaa.database.function.UpdateExeFunction;
28 import org.seasar.tuigwaa.model.common.EntityUtils;
29 import org.seasar.tuigwaa.model.core.TgwAttribute;
30 import org.seasar.tuigwaa.model.core.TgwEntity;
31 import org.seasar.tuigwaa.model.core.impl.DateAttribute;
32 import org.seasar.tuigwaa.model.core.impl.FileAttribute;
33 import org.seasar.tuigwaa.model.core.impl.SetAttribute;
34 import org.seasar.tuigwaa.util.TgwResource;
35 import org.seasar.tuigwaa.util.ajax.AbstractRowSetForm;
36 import org.seasar.tuigwaa.view.AttributeFormGenerateVisitor;
37
38 import com.isenshi.util.CharUtil;
39 import com.isenshi.util.HtmlBuffer;
40
41 /***
42 * カスタムフォームを作成するフォームに対応
43 *
44 * @author nishioka
45 */
46 public class CustomFormForm extends AbstractRowSetForm {
47
48 private static final long serialVersionUID = 9075767762615784812L;
49
50 private TgwEntity entity;
51
52 private String name;
53
54 private boolean updateOnly = false;
55
56 public CustomFormForm(TgwEntity entity) {
57 this.entity = entity;
58
59 Iterator itr = entity.getFieldIterator();
60 while (itr.hasNext()) {
61 TgwAttribute attr = (TgwAttribute) itr.next();
62 CustomFormRow row = new CustomFormRow(attr);
63 if (EntityUtils.isBuiltinAttribute(attr)
64 || EntityUtils.oneToManyFlag(attr)) {
65 continue;
66 }
67 addRow(row);
68 }
69 }
70
71 public CustomFormForm(TgwEntity entity, UpdateExeFunction function) {
72 this.entity = entity;
73 setName(function.getName());
74 setUpdateOnly(function.isUpdateOnly());
75 Iterator itr = entity.getFieldIterator();
76 while (itr.hasNext()) {
77 TgwAttribute attr = (TgwAttribute) itr.next();
78 CustomFormRow row = new CustomFormRow(attr);
79 if (EntityUtils.isBuiltinAttribute(attr)
80 || EntityUtils.oneToManyFlag(attr)) {
81 continue;
82 }
83 row.setOption(CustomFormRow.OPTION_IGNORE);
84 Object updateValue = function.getUpdateValueMap().get(
85 row.getAttrName());
86 if (updateValue != null) {
87 row.setOption(CustomFormRow.OPTION_VALUE);
88 if (attr instanceof DateAttribute) {
89 Calendar cal = Calendar.getInstance();
90 cal.setTime((Date) updateValue);
91
92 String values[] = { String.valueOf(cal.get(Calendar.YEAR)),
93 String.valueOf(cal.get(Calendar.MONTH) + 1),
94 String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) };
95
96 row.setValues(values);
97 } else if(attr instanceof SetAttribute){
98 String str = updateValue.toString();
99 row.setValues(str.split(","));
100 }else{
101 row.setValue(updateValue.toString());
102 }
103
104 } else {
105 Iterator itr2 = function.getDtoFieldList().iterator();
106 itr2loop: while (itr2.hasNext()) {
107 String fieldName = (String) itr2.next();
108 if (row.getAttrName().equals(fieldName)) {
109 row.setOption(CustomFormRow.OPTION_INPUT);
110 break itr2loop;
111 }
112 }
113 }
114 addRow(row);
115 }
116 }
117
118 public void addRowForEdit(Object row) {
119 addRow(row);
120 }
121
122 public boolean isUpdateOnly() {
123 return updateOnly;
124 }
125
126 public void setUpdateOnly(boolean updateOnly) {
127 this.updateOnly = updateOnly;
128 }
129
130 public TgwEntity getEntity() {
131 return entity;
132 }
133
134 public String getName() {
135 return name;
136 }
137
138 public void setName(String name) {
139 this.name = name;
140 }
141
142 public UpdateExeFunction getUpdateExeFunction() {
143 UpdateExeFunction function = CriteriaFunctionFactory
144 .createUpdateExeFunction(entity, getName());
145 function.setUpdateOnly(isUpdateOnly());
146 for (Iterator i = getRowSet().iterator(); i.hasNext();) {
147 CustomFormRow row = (CustomFormRow) i.next();
148 switch (row.getOption()) {
149 case CustomFormRow.OPTION_INPUT:
150 String fieldName = row.getAttrName();
151 function.addDtoField(fieldName);
152 break;
153 case CustomFormRow.OPTION_VALUE:
154 String valueFieldName = row.getAttrName();
155 Object value = row.getConvertedValue();
156 function.putUpdateValue(valueFieldName, value);
157 break;
158 case CustomFormRow.OPTION_IGNORE:
159 break;
160 }
161 }
162 return function;
163 }
164
165 protected void removeRow(int index, Object row) {
166
167 }
168
169 protected Object createNewRow() {
170
171 return null;
172 }
173
174 protected Object createRow(int index, String value) {
175
176 return null;
177 }
178
179 protected String[] getHtmlData(int index, Object row) {
180 CustomFormRow itm = (CustomFormRow) row;
181 return itm.getHtml(index);
182 }
183
184 public static class CustomFormRow {
185
186 public static final int OPTION_IGNORE = 0;
187
188 public static final int OPTION_INPUT = 1;
189
190 public static final int OPTION_VALUE = 2;
191
192 private TgwAttribute attr;
193
194 private int option;
195
196 private String value;
197
198 public CustomFormRow(TgwAttribute attr) {
199 this.attr = attr;
200 }
201
202 private boolean isArgumentOpetion(int option) {
203 if (this.option == option) {
204 return true;
205 } else if (this.option == -1 && option == OPTION_IGNORE) {
206 return true;
207 }
208 return false;
209 }
210
211 public String[] getHtml(int index) {
212 String header = attr.getDisplayName();
213 HtmlBuffer buf = new HtmlBuffer();
214 buf.appendRadio(getPrefix(index) + "option", "" + OPTION_IGNORE,
215 isArgumentOpetion(OPTION_IGNORE));
216 buf.appendBody(TgwResource.getMessage("msg.customForm.ignore"));
217 buf.appendRadio(getPrefix(index) + "option", "" + OPTION_INPUT,
218 isArgumentOpetion(OPTION_INPUT));
219 buf.appendBody(TgwResource.getMessage("msg.customForm.input"));
220
221 if (!(attr instanceof FileAttribute)) {
222 buf.appendRadio(getPrefix(index) + "option", "" + OPTION_VALUE,
223 isArgumentOpetion(OPTION_VALUE));
224 buf.appendBody(TgwResource.getMessage("msg.customForm.value"));
225
226 AttributeFormGenerateVisitor visitor = AttributeFormGenerateVisitor.DEFAULT_INSTANCE;
227 String elemName;
228 if (!(attr instanceof DateAttribute || attr instanceof SetAttribute)) {
229
230 elemName = getPrefix(index) + "value";
231 } else {
232 elemName = getPrefix(index) + "values";
233 }
234 String form = (String) attr.accept(visitor, elemName);
235 if (attr instanceof DateAttribute) {
236 form = FormValueSetter.getInstance().parseDate(form,
237 elemName, getValues());
238 } else if(attr instanceof SetAttribute){
239 form = FormValueSetter.getInstance().parseSet(form,elemName,getValues());
240 }else {
241 form = FormValueSetter.getInstance().parse(form, elemName,
242 getValue());
243 }
244 buf.appendBody(form);
245 }
246 return new String[] { header, buf.toString() };
247 }
248
249 public void setOption(int option) {
250 this.option = option;
251 }
252
253 public int getOption() {
254 return option;
255 }
256
257 public void setValue(String value) {
258 this.value = value;
259 }
260
261 private String[] values = null;
262
263 public void setValues(String[] values) {
264 this.values = values;
265 }
266
267 public String[] getValues() {
268 return values;
269 }
270
271 public String getValue() {
272 return value;
273 }
274
275 public String getAttrName() {
276 return attr.getName();
277 }
278
279 public Object getConvertedValue() {
280 Class targetClass = EntityUtils.toJavaClass(attr);
281 if (attr instanceof DateAttribute) {
282 Calendar cal = Calendar.getInstance();
283 cal.set(Integer.parseInt(getValues()[0]), Integer
284 .parseInt(getValues()[1]) - 1, Integer
285 .parseInt(getValues()[2]), 0, 0, 0);
286 return cal.getTime();
287 }else if(attr instanceof SetAttribute){
288 return CharUtil.join(getValues(),",");
289 }
290 return ConvertUtils.convert(getValue(), targetClass);
291 }
292 }
293
294 public void reset(ActionMapping arg0, HttpServletRequest arg1) {
295 this.updateOnly = false;
296 super.reset(arg0, arg1);
297 }
298
299 }