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  
20  import org.seasar.tuigwaa.database.function.SearchExeFunction;
21  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
22  import org.seasar.tuigwaa.database.function.criteria.SubcriteriaFunction;
23  import org.seasar.tuigwaa.model.common.EntityUtils;
24  import org.seasar.tuigwaa.model.core.TgwAttribute;
25  import org.seasar.tuigwaa.model.core.TgwEntity;
26  import org.seasar.tuigwaa.system.Constants;
27  import org.seasar.tuigwaa.util.TgwNameUtils;
28  
29  import com.isenshi.util.converter.ConverterResource;
30  
31  public class SearchConfigGenerator extends ConfigGenerator {
32  
33  	private TgwEntity entity;
34  
35  	private static final String PROTO_FORM = "searchFormProto";
36  
37  	private static final String PROTO_ACTION = "actionSearch";
38  
39  	public SearchConfigGenerator(ConverterResource resource, TgwEntity entity,
40  			ModuleConfigMetadata metadata) {
41  		super(metadata, resource, null);
42  		this.entity = entity;
43  	}
44  
45  	public void generate(SearchExeFunction searchExeFunction) {
46  		String formName = getModuleConfigMetadata().generateSearchFormName(
47  				entity, searchExeFunction);
48  		String actionName = getModuleConfigMetadata().generateSearchActionName(
49  				entity, searchExeFunction);
50  		generate(PROTO_FORM, formName, PROTO_ACTION, actionName);
51  		addProperties(searchExeFunction);
52  	}
53  
54  	private void addProperties(SearchExeFunction searchExeFunction) {
55  		Iterator itr = searchExeFunction.getCriteriaListFunction()
56  				.getFunctionList().iterator();
57  		while (itr.hasNext()) {
58  
59  			CriteriaFunction function = (CriteriaFunction) itr.next();
60  			String name = function.getName();
61  			// String fieldName = function.getField();
62  			String fieldName = getNestedFieldName("", function);
63  			TgwAttribute field = entity.getField(fieldName);
64  
65  			addProperty(name, EntityUtils.toJavaType(field));
66  
67  			if (EntityUtils.needConverter(field)) {
68  				String src = TgwNameUtils.toConverterFieldName(name);
69  				addConverterResource(field, src, name);
70  			}
71  		}
72  		addProperty(Constants.PARAM_PAGENAME, "java.lang.String");
73  	}
74  
75  	private String getNestedFieldName(String prefix, CriteriaFunction function) {
76  		if (function instanceof SubcriteriaFunction) {
77  			return getNestedFieldName(prefix + function.getField() + ".",
78  					((SubcriteriaFunction) function).getSequentialFunction());
79  		}
80  		return prefix + function.getField();
81  	}
82  }