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.config;
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.apache.commons.validator.Arg;
25  import org.apache.commons.validator.Field;
26  import org.apache.commons.validator.Form;
27  import org.apache.commons.validator.Var;
28  import org.seasar.tuigwaa.model.common.EntityUtils;
29  import org.seasar.tuigwaa.model.core.TgwAttribute;
30  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
31  import org.seasar.tuigwaa.model.core.impl.FloatAttribute;
32  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
33  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
34  import org.seasar.tuigwaa.model.core.impl.StringAttribute;
35  import org.seasar.tuigwaa.util.TgwNameUtils;
36  
37  import com.isenshi.util.CharUtil;
38  
39  public class ValidatorUtils {
40  
41  	// private static final String FUNC_DELETECONFIRMATION =
42  	// "javascript:confirmBeforeDelete(encodeURI('${0}'));";
43  
44  	private static final String RULE_REQUIRED = "required";
45  
46  	private static final String RULE_INTEGER = "integer";
47  
48  	private static final String RULE_FLOAT = "float";
49  
50  	private static final String RULE_MAXLENGTH = "maxlength";
51  
52  	private static final String RULE_EMAIL = "email";
53  
54  	private static final String RULE_URL = "url";
55  
56  	private static final int STRING_MAX_LENGTH = 100;
57  
58  	private static final String FUNC_LINKBUTTON = "<input type=\"button\" value=\"${value}\" onclick=\"javascript:window.location='${url}';\"/>";
59  
60  	private static final String FUNC_DELETECONFIRMATION = "javascript:confirmBeforeDelete('${0}');";
61  
62  	private static final String FUNC_ADDSTRINGARRY = "addStringArray(${ROW}, ${COLUMN}, ${DEFAULT_INPUT_SIZE});";
63  
64  	private static final String FUNC_REMOVESTRINGARRY = "removeStringArray(${ROW}, ${COLUMN});";
65  
66  	private static final String ONCHNAGECALENDAR = "onChangeCalendar(document.getElementsByName('${elemName}')[0], document.getElementsByName('${elemName}')[1],document.getElementsByName('${elemName}')[2],${nullOption});";
67  
68  	private static final String ON_FORM_SUBMIT = "return validate${FORMNAME}(this);";
69  
70  	public static String getAddStringArrayFunction(int row, int column,
71  			int defaultInputSize) {
72  		Map map = new HashMap();
73  		map.put("ROW", String.valueOf(row));
74  		map.put("COLUMN", String.valueOf(column));
75  		map.put("DEFAULT_INPUT_SIZE", String.valueOf(defaultInputSize));
76  		return CharUtil.replace(FUNC_ADDSTRINGARRY, map);
77  	}
78  
79  	public static String getLinkButton(String value, String url) {
80  		Map map = new HashMap();
81  		map.put("value", value);
82  		map.put("url", url);
83  		return CharUtil.replace(FUNC_LINKBUTTON, map);
84  	}
85  
86  	public static String getRemoveStringArrayFunction(int row, int column) {
87  		Map map = new HashMap();
88  		map.put("ROW", String.valueOf(row));
89  		map.put("COLUMN", String.valueOf(column));
90  		return CharUtil.replace(FUNC_REMOVESTRINGARRY, map);
91  	}
92  
93  	public static String getOnSumbmitForm(String formName) {
94  		return CharUtil.replace(ON_FORM_SUBMIT, "FORMNAME", CharUtil
95  				.toUpperCaseAtFisrtChar(formName));
96  	}
97  
98  	public static String getOnChangeCalendar(String elemName, boolean nullOption) {
99  		Map map = new HashMap();
100 		map.put("elemName", elemName);
101 		map.put("nullOption", String.valueOf(nullOption));
102 		return CharUtil.replace(ONCHNAGECALENDAR, map);
103 	}
104 
105 	public static void bindField(Form form, TgwAttribute attr) {
106 		bindField(form, attr, null);
107 	}
108 
109 	public static String getDeleteConfirmationFunc(String page,
110 			boolean needEncode) {
111 		if (needEncode) {
112 			page = CharUtil.urlEncode(page);
113 		}
114 		return CharUtil.replace(FUNC_DELETECONFIRMATION, page);
115 	}
116 
117 	public static void bindField(Form form, TgwAttribute attr,
118 			String indexedListProperty) {
119 		RuleList rules = createDepends(attr);
120 		String columnName = null;
121 
122 		if (EntityUtils.needConverter(attr)) {
123 			if (attr instanceof SetAttribute || attr instanceof FileAttribute) {
124 				columnName = TgwNameUtils.toDynaConverterName(attr.getName());
125 			} else {
126 				return;
127 			}
128 		} else {
129 			columnName = TgwNameUtils.toDynaPropertyName(attr.getName());
130 		}
131 
132 		if (rules.size() > 0) {
133 			String displayName = attr.getDisplayName();
134 			bindField(form, rules, columnName, displayName, indexedListProperty);
135 		}
136 	}
137 
138 	public static void bindField(Form form, RuleList rules, String columnName,
139 			String displayName, String indexedLIstProperty) {
140 
141 		Field newField = rules.createField(columnName);
142 		if (indexedLIstProperty != null) {
143 			newField.setIndexedListProperty(indexedLIstProperty);
144 		}
145 
146 		Arg arg = new Arg();
147 		arg.setKey(displayName);
148 		arg.setResource(false);
149 		newField.addArg(arg);
150 
151 		// for(Iterator i=rules.extraArgs.iterator();i.hasNext();){
152 		// newField.addArg((Arg)i.next());
153 		// }
154 		form.addField(newField);
155 	}
156 
157 	public static RuleList createDepends(TgwAttribute attr) {
158 		RuleList rules = new RuleList();
159 
160 		if (attr.isRequired()) {
161 			rules.add(RULE_REQUIRED);
162 		}
163 
164 		if (attr instanceof IntegerAttribute) {
165 			rules.add(RULE_INTEGER);
166 		} else if (attr instanceof FloatAttribute) {
167 			rules.add(RULE_FLOAT);
168 		} else if (attr instanceof StringAttribute) {
169 			StringAttribute sField = (StringAttribute) attr;
170 
171 			int length = sField.getLength();
172 			if (length <= 0) {
173 				length = STRING_MAX_LENGTH;
174 			}
175 			rules.add(RULE_MAXLENGTH);
176 			Var var = new Var();
177 			var.setName("maxlength");
178 			var.setValue("" + length);
179 			rules.addVar(var);
180 
181 			Arg arg = new Arg();
182 			arg.setKey("" + length);
183 			arg.setResource(false);
184 			rules.extraArgs.add(arg);
185 
186 			String mask = sField.getMask();
187 			if (mask != null && mask.equals(StringAttribute.MASK_EMAIL)) {
188 				rules.add(RULE_EMAIL);
189 			} else if (mask != null && mask.equals(StringAttribute.MASK_URL)) {
190 				rules.add("url");
191 			}
192 		}
193 		return rules;
194 	}
195 
196 	static class RuleList {
197 
198 		private List ruleNames = new ArrayList();
199 
200 		private List vars = new ArrayList();
201 
202 		private List extraArgs = new ArrayList();
203 
204 		public void add(String ruleName) {
205 			ruleNames.add(ruleName);
206 		}
207 
208 		public void addVar(Var var) {
209 			vars.add(var);
210 		}
211 
212 		public int size() {
213 			return ruleNames.size();
214 		}
215 
216 		public List getExtraArgs() {
217 			return extraArgs;
218 		}
219 
220 		public Field createField(String columnName) {
221 			Field newField = new Field();
222 			newField.setProperty(columnName);
223 			StringBuffer buf = new StringBuffer();
224 			boolean first = true;
225 			for (Iterator i = ruleNames.iterator(); i.hasNext();) {
226 				if (first) {
227 					first = false;
228 				} else {
229 					buf.append(",");
230 				}
231 				buf.append(i.next());
232 			}
233 			newField.setDepends(buf.toString());
234 
235 			for (Iterator i = vars.iterator(); i.hasNext();) {
236 				newField.addVar((Var) i.next());
237 			}
238 			return newField;
239 		}
240 
241 		public boolean contains(String ruleName) {
242 			return ruleNames.contains(ruleName);
243 		}
244 	}
245 }