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.database;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.seasar.tuigwaa.model.common.TgwElementVisitor;
26  import org.seasar.tuigwaa.model.core.TgwAttribute;
27  import org.seasar.tuigwaa.model.core.TgwEntity;
28  import org.seasar.tuigwaa.system.Constants;
29  import org.seasar.tuigwaa.view.wiki.DefaultDataViewVisitor;
30  
31  import com.isenshi.util.functor.BinaryFunction;
32  
33  public class DataTableFactory {
34  
35  	public static final DataTable create(TgwEntity entity, Collection data) {
36  		// String name = entity.getName();
37  		String displayName = entity.getDisplayName();
38  		List list = new ArrayList();
39  		list.addAll(data);
40  		List headers = createSystemHeaders(entity);
41  		List displayHeaders = createDisplayHeaders(entity);
42  		Map filterMap = createFilterMap(entity, DefaultDataViewVisitor.INSTANCE);
43  		int strategy = (entity.isAggregation()) ? DataTable.PRIMITIVE_DATA
44  				: DataTable.BEAN_DATA;
45  		return new DataTableImpl(displayName, headers, displayHeaders, list,
46   				strategy, filterMap);
47  	}
48  
49  	private static final List createSystemHeaders(TgwEntity entity) {
50  		List list = new ArrayList();
51  		if (!entity.isAggregation()) {
52  			list.add(Constants.ENTITY_BUILTIN_ID);
53  		}
54  		Iterator itr = entity.getFieldIterator();
55  		while (itr.hasNext()) {
56  			TgwAttribute field = (TgwAttribute) itr.next();
57  			list.add(field.getName());
58  		}
59  		return list;
60  	}
61  
62  	/***
63  	 * テーブルの各カラムの表示名を設定する
64  	 */
65  	private static final List createDisplayHeaders(TgwEntity entity) {
66  		List list = new ArrayList();
67  		if (!entity.isAggregation()) {
68  			list.add(entity.getPrimaryKeyDisplayName());
69  		}
70  		Iterator itr = entity.getFieldIterator();
71  		while (itr.hasNext()) {
72  			TgwAttribute field = (TgwAttribute) itr.next();
73  			list.add(field.getDisplayName());
74  		}
75  		return list;
76  	}
77  
78  	public static Map createFilterMap(TgwEntity entity,
79  			TgwElementVisitor visitor) {
80  		Map filterMap = new HashMap();
81  		for (Iterator i = entity.getFieldIterator(); i.hasNext();) {
82  			TgwAttribute field = (TgwAttribute) i.next();
83  			String fieldName = field.getName();
84  			bindFilterFunction(filterMap, field, fieldName, visitor);
85  		}
86  		return filterMap;
87  	}
88  
89  	private static void bindFilterFunction(Map filterMap, TgwAttribute field,
90  			String fieldName, TgwElementVisitor visitor) {
91  		BinaryFunction viewFunction = (BinaryFunction) field.accept(visitor,
92  				null);
93  		if (viewFunction != null) {
94  			filterMap.put(fieldName, viewFunction);
95  		}
96  	}
97  }