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.Iterator;
19  import java.util.Map;
20  
21  import org.apache.commons.validator.Form;
22  import org.apache.commons.validator.FormSet;
23  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
24  import org.seasar.tuigwaa.model.core.TgwAttribute;
25  import org.seasar.tuigwaa.model.core.TgwEntity;
26  import org.seasar.tuigwaa.system.Constants;
27  
28  import com.isenshi.util.converter.ConverterResource;
29  
30  public class CustomFormConfigGenerator extends EntityConfigGenerator {
31  
32  	private UpdateExeFunction updateExe;
33  
34  	public CustomFormConfigGenerator(ConverterResource resource,
35  			TgwEntity entity, ModuleConfigMetadata metadata,
36  			UpdateExeFunction updateExe, Map formSetCache) {
37  		super(resource, entity, metadata, formSetCache);
38  		this.updateExe = updateExe;
39  	}
40  
41  	public void generate() {
42  		String formName = getModuleConfigMetadata().generateCusotmFormName(
43  				getEntity(), updateExe);
44  		String actionName = getModuleConfigMetadata().generateCustomActionName(
45  				getEntity(), updateExe);
46  		generate(PROTO_FORM, formName, PROTO_ACTION, actionName);
47  		addProperties();
48  
49  		FormSet formSet = createFormSet();
50  		setValidator(formSet);
51  	}
52  
53  	public boolean hasField(String attrName){
54  		return updateExe.getDtoFieldList().contains(attrName);
55  	}
56  	
57  	private void addProperties() {
58  		Iterator itr = getEntity().getFieldIterator();
59  		while (itr.hasNext()) {
60  			TgwAttribute field = (TgwAttribute) itr.next();
61  			String fieldName = field.getName();
62  
63  			if (hasField(fieldName)){
64  				bindFormProperty(field);
65  			}
66  		}
67  		addProperty(Constants.PARAM_PAGENAME, "java.lang.String");
68  	}
69  
70  	private FormSet createFormSet() {
71  		FormSet fs = getFormSet(getEntity().getDomainName(), getFormName());
72  		Form form = new Form();
73  		form.setName(getActionMapping().getName());
74  		Iterator itr = getEntity().getFieldIterator();
75  		while (itr.hasNext()) {
76  			TgwAttribute attr = (TgwAttribute) itr.next();
77  			String attrName = attr.getName();
78  
79  			if (hasField(attrName)){
80  				ValidatorUtils.bindField(form, attr);
81  			}
82  		}
83  		fs.addForm(form);
84  		return fs;
85  	}
86  }