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.Collections;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.apache.commons.validator.FormSet;
25  import org.apache.commons.validator.ValidatorResources;
26  import org.apache.struts.action.ActionMapping;
27  import org.apache.struts.config.FormBeanConfig;
28  import org.apache.struts.config.FormPropertyConfig;
29  import org.seasar.framework.container.S2Container;
30  import org.seasar.framework.container.factory.S2ContainerFactory;
31  import org.seasar.tuigwaa.model.core.TgwAttribute;
32  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
33  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
34  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
35  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
36  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
37  import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
38  
39  import com.isenshi.util.converter.ConverterResource;
40  import com.isenshi.util.converter.function.ConverterFunction;
41  import com.isenshi.util.extlib.StrutsUtil;
42  
43  public class ConfigGenerator {
44  
45  	private static final S2Container PROTOTYPE_CONTAINER = S2ContainerFactory
46  			.create("dicon/local/strutsConfigProto.dicon");
47  
48  	private static final Map CONVERTER_NAME_MAP;
49  
50  	private static final Map CONVERTER_TYPE_MAP;
51  
52  	static {
53  		Map tmp = new HashMap();
54  		tmp.put(DateAttribute.class, "dateConverter");
55  		tmp.put(FkAttribute.class, "id2ObjConverter");
56  		tmp.put(FileAttribute.class, "formFile2BytesConverter");
57  		tmp.put(SetAttribute.class, "many2manyConverter");
58  		tmp.put(SelfAttribute.class, "id2ObjConverter");
59  		tmp.put(TimestampAttribute.class, "dateConverter");
60  		CONVERTER_NAME_MAP = Collections.unmodifiableMap(tmp);
61  
62  		Map tmp2 = new HashMap();
63  		tmp2.put(DateAttribute.class, "java.lang.Integer[]");
64  		tmp2.put(FkAttribute.class, "java.lang.Long");
65  		tmp2.put(FileAttribute.class, "org.apache.struts.upload.FormFile");
66  		tmp2.put(SetAttribute.class, "java.lang.String[]");
67  		tmp2.put(SelfAttribute.class, "java.lang.Long");
68  		tmp2.put(TimestampAttribute.class, "java.lang.Integer[]");
69  		CONVERTER_TYPE_MAP = Collections.unmodifiableMap(tmp2);
70  	}
71  
72  	private ActionMapping actionMapping;
73  
74  	private FormBeanConfig formBeanConfig;
75  
76  	private ModuleConfigMetadata moduleConfigMetadata;
77  
78  	private ConverterResource converterResource;
79  
80  	private Map formSetCache;
81  
82  	private String actionName;
83  
84  	private String formName;
85  
86  	public ConfigGenerator(ModuleConfigMetadata metadata,
87  			ConverterResource resource, Map formSetCache) {
88  		this.moduleConfigMetadata = metadata;
89  		this.converterResource = resource;
90  		this.formSetCache = formSetCache;
91  	}
92  
93  	public ActionMapping getActionMapping() {
94  		return actionMapping;
95  	}
96  
97  	public ModuleConfigMetadata getModuleConfigMetadata() {
98  		return moduleConfigMetadata;
99  	}
100 
101 	public String getFormName() {
102 		return formName;
103 	}
104 
105 	public String getActionName() {
106 		return actionName;
107 	}
108 
109 	public void generate(String formProto, String formName, String actionProto,
110 			String actionName) {
111 		this.formName = formName;
112 		this.actionName = actionName;
113 
114 		initActionMapping(actionProto, formName, actionName);
115 		initFormBeanConfig(formProto, formName);
116 	}
117 
118 	protected void addProperty(String name, String type) {
119 		FormPropertyConfig propConfig = new FormPropertyConfig();
120 		propConfig.setName(name);
121 		propConfig.setType(type);
122 		formBeanConfig.addFormPropertyConfig(propConfig);
123 	}
124 
125 	protected void addConverterResource(TgwAttribute attr, String src,
126 			String target) {
127 
128 		String siteName = moduleConfigMetadata.getSiteName();
129 		String converterName = getConverterName(attr);
130 		String converterType = getConverterType(attr);
131 
132 		addProperty(src, converterType);
133 
134 		ConverterFunction function = converterResource.createConverterFunction(
135 				converterName, src, target);
136 		function.setEntity(attr.getEntity());
137 		converterResource.resiter(siteName, formName, function);
138 
139 		if (function.getExtraProperties() != null) {
140 			Properties props = function.getExtraProperties();
141 			Iterator i = props.keySet().iterator();
142 			while (i.hasNext()) {
143 				String key = (String) i.next();
144 				String value = props.getProperty(key);
145 				addProperty(key, value);
146 			}
147 		}
148 	}
149 
150 	protected synchronized FormSet getFormSet(String schema, String name) {
151 		String key = schema + "." + name;
152 		FormSet formSet = (FormSet) formSetCache.get(key);
153 		if (formSet == null) {
154 			formSet = new FormSet();
155 			formSetCache.put(key, formSet);
156 		}
157 		return formSet;
158 	}
159 
160 	protected void setValidator(FormSet formSet) {
161 		ValidatorResources resource = StrutsUtil
162 				.getValidatorResources(getModuleConfigMetadata()
163 						.getModuleConfig());
164 		resource.addFormSet(formSet);
165 	}
166 
167 	// [Start] ----- Private Method -----
168 
169 	private String getConverterName(TgwAttribute field) {
170 		return (String) CONVERTER_NAME_MAP.get(field.getClass());
171 	}
172 
173 	private String getConverterType(TgwAttribute field) {
174 		return (String) CONVERTER_TYPE_MAP.get(field.getClass());
175 	}
176 
177 	private void initFormBeanConfig(String protoname, String formName) {
178 		formBeanConfig = (FormBeanConfig) PROTOTYPE_CONTAINER
179 				.getComponent(protoname);
180 		formBeanConfig.setName(formName);
181 		moduleConfigMetadata.getModuleConfig()
182 				.addFormBeanConfig(formBeanConfig);
183 	}
184 
185 	private void initActionMapping(String protoname, String formName,
186 			String actionName) {
187 		actionMapping = (ActionMapping) PROTOTYPE_CONTAINER
188 				.getComponent(protoname);
189 		actionMapping.setName(formName);
190 		actionMapping.setPath(actionName);
191 		moduleConfigMetadata.getModuleConfig().addActionConfig(actionMapping);
192 	}
193 }