View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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 org.apache.commons.beanutils.ConvertUtils;
23  import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
24  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
25  import org.seasar.tuigwaa.model.common.EntityUtils;
26  import org.seasar.tuigwaa.model.common.TgwAttributeVisitor;
27  import org.seasar.tuigwaa.model.core.TgwAttribute;
28  import org.seasar.tuigwaa.model.core.TgwEntity;
29  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
30  import org.seasar.tuigwaa.util.TgwResource;
31  import org.seasar.tuigwaa.util.ajax.AbstractRowSetForm;
32  import org.seasar.tuigwaa.view.AttributeFormGenerateVisitor;
33  
34  import com.isenshi.util.HtmlBuffer;
35  
36  /***
37   * 更新ルールを作成するフォームに対応
38   * 
39   * @author nishioka
40   */
41  public class UpdateRuleForm extends AbstractRowSetForm {
42  
43  	private static final long serialVersionUID = 3998751707571170768L;
44  
45  	private TgwEntity entity;
46  
47  	private String name;
48  
49  	public UpdateRuleForm(TgwEntity entity) {
50  		this.entity = entity;
51  	}
52  
53  	public UpdateRuleForm(TgwEntity entity, UpdateExeFunction function) {
54  		this.entity = entity;
55  		setName(function.getName());
56  		Iterator itr = entity.getFieldIterator();
57  		while (itr.hasNext()) {
58  			TgwAttribute attr = (TgwAttribute) itr.next();
59  			if (EntityUtils.isBuiltinAttribute(attr)
60  					|| EntityUtils.oneToManyFlag(attr)) {
61  				continue;
62  			}
63  			UpdateRuleRow row = new UpdateRuleRow(entity, attr);
64  			Object updateValue = function.getUpdateValueMap().get(
65  					row.getAttrName());
66  			if (updateValue != null) {
67  				if (attr instanceof DateAttribute) {
68  					Calendar cal = Calendar.getInstance();
69  					cal.setTime((Date) updateValue);
70  
71  					String values[] = { String.valueOf(cal.get(Calendar.YEAR)),
72  							String.valueOf(cal.get(Calendar.MONTH) + 1),
73  							String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) };
74  
75  					row.setValues(values);
76  				} else {
77  					row.setValue(updateValue.toString());
78  				}
79  				addRow(row);
80  			}
81  
82  		}
83  	}
84  
85  	public TgwEntity getEntity() {
86  		return entity;
87  	}
88  
89  	public String getName() {
90  		return name;
91  	}
92  
93  	public void setName(String name) {
94  		this.name = name;
95  	}
96  
97  	public UpdateExeFunction getUpdateExeFunction() {
98  		UpdateExeFunction function = CriteriaFunctionFactory
99  				.createUpdateExeFunction(entity, getName());
100 		for (Iterator i = getRowSet().iterator(); i.hasNext();) {
101 			UpdateRuleRow row = (UpdateRuleRow) i.next();
102 			String attrName = row.getAttrName();
103 			Object value = row.getConvertedValue();
104 			function.putUpdateValue(attrName, value);
105 		}
106 		return function;
107 	}
108 
109 	protected void removeRow(int index, Object row) {
110 		// do nothing
111 	}
112 
113 	protected Object createNewRow() {
114 		TgwAttribute attr = entity.getFirstNotFkField();
115 		return new UpdateRuleRow(entity, attr);
116 	}
117 
118 	protected Object createRow(int index, String value) {
119 		TgwAttribute attr = entity.getField(value);
120 		return new UpdateRuleRow(entity, attr);
121 	}
122 
123 	protected String[] getHtmlData(int index, Object row) {
124 		UpdateRuleRow itm = (UpdateRuleRow) row;
125 		return itm.getHtml(index);
126 	}
127 
128 	public static class UpdateRuleRow {
129 
130 		private TgwEntity entity;
131 
132 		private TgwAttribute attr;
133 
134 		private String value;
135 
136 		private String[] values;
137 
138 		public UpdateRuleRow(TgwEntity entity, TgwAttribute attr) {
139 			this.entity = entity;
140 			this.attr = attr;
141 		}
142 
143 		protected String createRemoveButton(int index) {
144 			return createRemoveButtonHtml(index, TgwResource
145 					.getMessage("button.remove"));
146 		}
147 
148 		public String[] getHtml(int index) {
149 			TgwAttributeVisitor visitor = AttributeFormGenerateVisitor.DEFAULT_INSTANCE;
150 			String elemName = getPrefix(index) + "value";
151 			if (attr instanceof DateAttribute) {
152 				elemName += "s";
153 			}
154 			String form = (String) attr.accept(visitor, elemName);
155 
156 			if (attr instanceof DateAttribute) {
157 				form = FormValueSetter.getInstance().parseDate(form, elemName,
158 						getValues());
159 			} else {
160 				form = FormValueSetter.getInstance().parse(form, elemName,
161 						getValue());
162 			}
163 
164 			return new String[] { getFieldHtml(index), form + createRemoveButton(index) };
165 		}
166 
167 		public void setValue(String value) {
168 			this.value = value;
169 		}
170 
171 		public String getValue() {
172 			return value;
173 		}
174 
175 		public String getAttrName() {
176 			return attr.getName();
177 		}
178 
179 		public String[] getValues() {
180 			return values;
181 		}
182 
183 		public void setValues(String[] values) {
184 			this.values = values;
185 		}
186 
187 		public Object getConvertedValue() {
188 			Class targetClass = EntityUtils.toJavaClass(attr);
189 			if (attr instanceof DateAttribute) {
190 				Calendar cal = Calendar.getInstance();
191 				cal.set(Integer.parseInt(getValues()[0]), Integer
192 						.parseInt(getValues()[1]) - 1, Integer
193 						.parseInt(getValues()[2]), 0, 0, 0);
194 				return cal.getTime();
195 			}
196 			return ConvertUtils.convert(getValue(), targetClass);
197 		}
198 
199 		private final String getFieldHtml(int index) {
200 			HtmlBuffer buf = new HtmlBuffer();
201 			buf.appendStartTag("select");
202 			buf.appendAttribute("name", getPrefix(index) + "fieldName");
203 			bindTypeEvent(buf);
204 
205 			Iterator itr = entity.getAllFieldList().iterator();
206 
207 			while (itr.hasNext()) {
208 				TgwAttribute anAttr = (TgwAttribute) itr.next();
209 				buf.appendOption(anAttr.getDisplayName(), anAttr.getName(),
210 						attr.getName());
211 			}
212 
213 			buf.endTag();
214 			return buf.toString();
215 		}
216 
217 	}
218 }