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.apache.struts.action.ActionMapping;
24  import org.seasar.tuigwaa.model.common.EntityUtils;
25  import org.seasar.tuigwaa.model.core.TgwAttribute;
26  import org.seasar.tuigwaa.model.core.TgwEntity;
27  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
28  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
29  import org.seasar.tuigwaa.system.Constants;
30  import org.seasar.tuigwaa.util.TgwNameUtils;
31  
32  import com.isenshi.util.converter.ConverterResource;
33  
34  public class EntityConfigGenerator extends ConfigGenerator {
35  
36  	private TgwEntity entity;
37  
38  	protected static final String PROTO_FORM = "dataForm";
39  
40  	protected static final String PROTO_ACTION = "actionSave";
41  
42  	public EntityConfigGenerator(ConverterResource resource, TgwEntity entity,
43  			ModuleConfigMetadata metadata, Map formSetCache) {
44  		super(metadata, resource, formSetCache);
45  		this.entity = entity;
46  	}
47  
48  	public void generate() {
49  		String formName = TgwNameUtils.toFormName(entity);
50  		String actionName = TgwNameUtils.toActionName(entity);
51  		generate(PROTO_FORM, formName, PROTO_ACTION, actionName);
52  		addProperties();
53  
54  		// validator
55  		FormSet fs = createValidatorFormSet(entity, getActionMapping());
56  		setValidator(fs);
57  	}
58  
59  	public TgwEntity getEntity() {
60  		return entity;
61  	}
62  
63  	private void addProperties() {
64  		Iterator itr = entity.getFieldIterator();
65  		while (itr.hasNext()) {
66  			TgwAttribute field = (TgwAttribute) itr.next();
67  			bindFormProperty(field);
68  		}
69  		addProperty(Constants.PARAM_PAGENAME, "java.lang.String");
70  	}
71  
72  	protected void bindFormProperty(TgwAttribute attr) {
73  		if (EntityUtils.isBuiltinAttribute(attr)) {
74  			return;
75  		}
76  
77  		bindNormalFormProperty(attr);
78  
79  		if (EntityUtils.needConverter(attr)) {
80  			String src = TgwNameUtils.toDynaConverterName(attr.getName());
81  			String target = TgwNameUtils.toDynaPropertyName(attr.getName());
82  			addConverterResource(attr, src, target);
83  		}
84  
85  		if (attr instanceof FkAttribute) {
86  			addProperty(TgwPluginUtils.createEscapeEntityBindingName(
87  					((FkAttribute) attr).getRefEntityName(), attr.getName()),
88  					Long.class.getName());
89  		}
90  	}
91  
92  	private void bindNormalFormProperty(TgwAttribute field) {
93  		String attrName = field.getName();
94  		String propName = TgwNameUtils.toDynaPropertyName(attrName);
95  		String javaType = EntityUtils.toJavaType(field);
96  		addProperty(propName, javaType);
97  	}
98  
99  	// ----- validator -----
100 
101 	private FormSet createValidatorFormSet(TgwEntity entity,
102 			ActionMapping mapping) {
103 		FormSet fs = getFormSet(entity.getDomainName(), entity.getName());
104 		Form form = new Form();
105 		form.setName(mapping.getName());
106 		Iterator itr = entity.getFieldIterator();
107 		while (itr.hasNext()) {
108 			TgwAttribute field = (TgwAttribute) itr.next();
109 			ValidatorUtils.bindField(form, field);
110 		}
111 		// overide
112 		fs.addForm(form);
113 		return fs;
114 	}
115 }