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.lang.reflect.InvocationTargetException;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import org.apache.commons.beanutils.PropertyUtils;
29  
30  import com.isenshi.util.TableMap;
31  import com.isenshi.util.functor.BinaryFunction;
32  
33  public class DataTableImpl extends AbstractDataTable {
34  
35  	public static final int BEAN_DATA = 0;
36  
37  	public static final int PRIMITIVE_DATA = 1;
38  
39  	private Map filterMap_;
40  
41  	private List data_;
42  
43  	private DataGetterStrategy strategy_;
44  
45  	public DataTableImpl(String name, List headers, List data, int strategy) {
46  		this(name, headers, headers, data, strategy, null);
47  	}
48  
49  	public void setFilterMap(Map filterMap) {
50  		this.filterMap_ = filterMap;
51  	}
52  
53  	public DataTableImpl(String name, List headers, List displayHeaders,
54  			List data, int strategy, Map filterMap) {
55  		super(name, headers, displayHeaders);
56  
57  		// this.displayHeaders_ = displayHeaders;
58  		this.data_ = data;
59  		this.filterMap_ = filterMap;
60  		
61  		this.setStrategy(strategy);
62  	}
63  
64  	
65  	public Collection getDataList() {
66  		return data_;
67  	}
68  
69  	public int getRowSize() {
70  		return strategy_.getRowSize();
71  	}
72  
73  	public Object getData(int row, int column) {
74  		Object data = strategy_.getData(row, column);
75  		String header = getHeader(column);
76  
77  		if (filterMap_ == null) {
78  			return data;
79  		}
80  
81  		BinaryFunction function = (BinaryFunction) filterMap_.get(header);
82  		if (function != null) {
83  			data = function.evaluate(data, data_.get(row));
84  		}
85  		return data;
86  	}
87  
88  	public Map getDataMap(int row) {
89  		Iterator itr = getHeaderIterator();
90  		Map map = new HashMap();
91  		while (itr.hasNext()) {
92  			String header = (String) itr.next();
93  			map.put(header, getData(row, header));
94  		}
95  		return map;
96  	}
97  
98  	public void remove(int row) {
99  		data_.remove(row);
100 	}
101 
102 	public void setStrategy(int strategy) {
103 		if (BEAN_DATA == strategy) {
104 			strategy_ = new BeanDataGetterStrategy();
105 		} else if (PRIMITIVE_DATA == strategy) {
106 			strategy_ = new PrimitiveDataGetterStrategy();
107 		}
108 	}
109 
110 	interface DataGetterStrategy {
111 		int getRowSize();
112 
113 		Object getData(int row, int column);
114 	}
115 
116 	public Object getRowObject(int row) {
117 		return data_.get(row);
118 	}
119 
120 	public SpreadSheet createSpreadSheet() {
121 		Set rowSet = new TreeSet();
122 		Set columnSet = new TreeSet();
123 		TableMap map = new TableMap();
124 		while (hasNext()) {
125 			DataRow dataRow = nextRow();
126 			Object rowLabel = dataRow.getCell(0);
127 			Object columnLabel = dataRow.getCell(1);
128 			if (rowLabel == null) {
129 				rowLabel = "";
130 			}
131 			if (columnLabel == null) {
132 				columnLabel = "";
133 			}
134 			rowSet.add(rowLabel);
135 			columnSet.add(columnLabel);
136 			map.put(dataRow.getCell(2), rowLabel, columnLabel);
137 		}
138 		List rowHeaders = new ArrayList();
139 		List columnHeaders = new ArrayList();
140 		rowHeaders.addAll(rowSet);
141 		columnHeaders.addAll(columnSet);
142 
143 		String rowLabel = (String) getDisplayHeaderMap().get(getHeader(0));
144 		String columnLabel = (String) getDisplayHeaderMap().get(getHeader(1));
145 		String dataLabel = (String) getDisplayHeaderMap().get(getHeader(2));
146 
147 		return new SpreadSheetImpl(getName(), dataLabel, rowLabel, rowHeaders,
148 				columnLabel, columnHeaders, map);
149 	}
150 
151 	class BeanDataGetterStrategy implements DataGetterStrategy {
152 
153 		public int getRowSize() {
154 			return data_.size();
155 		}
156 
157 		public Object getData(int row, int column) {
158 			if (row >= getRowSize()) {
159 				return null;
160 			}
161 			Object rowObject = getRowObject(row);
162 			return getProperty(rowObject, column);
163 		}
164 
165 		private Object getProperty(Object obj, int index) {
166 			String header = getHeader(index);
167 			Object ret = null;
168 			try {
169 				ret = PropertyUtils.getProperty(obj, header);
170 			} catch (IllegalAccessException e) {
171 				e.printStackTrace();
172 				throw new RuntimeException(e);
173 			} catch (InvocationTargetException e) {
174 				e.printStackTrace();
175 				throw new RuntimeException(e);
176 			} catch (NoSuchMethodException e) {
177 				e.printStackTrace();
178 				throw new RuntimeException(e);
179 			}
180 			return ret;
181 		}
182 
183 	}
184 
185 	class PrimitiveDataGetterStrategy implements DataGetterStrategy {
186 
187 		public int getRowSize() {
188 			if (data_ == null || data_.size() == 0) {
189 				return 0;
190 			}
191 
192 			Object row = data_.get(0);
193 			boolean isArray = false;
194 			if (row instanceof Object[]) {
195 				isArray = true;
196 			}
197 			if (isArray) {
198 				return data_.size();
199 			} else if (!isArray && getColumnSize() == 1) {
200 				return data_.size();
201 			} else {
202 				return 1;
203 			}
204 		}
205 
206 		public Object getData(int row, int column) {
207 			int columnSize = getColumnSize();
208 			int rowSize = getRowSize();
209 			if (column >= columnSize || row >= rowSize) {
210 				return null;
211 			}
212 
213 			Object ret = null;
214 
215 			// if (rowSize == 1 && columnSize ==1) {
216 			// ret = data_.get(column);
217 			// ret = ((Object[])data_.get(0))[column];
218 			// } else
219 
220 			if (columnSize == 1) {
221 				ret = data_.get(row);
222 			} else {
223 				Object[] dataArray = (Object[]) data_.get(row);
224 				ret = dataArray[column];
225 			}
226 			return ret;
227 		}
228 
229 	}
230 
231 	public String toString() {
232 		StringBuffer buf = new StringBuffer();
233 
234 		while (this.hasNext()) {
235 			DataRow dataRow = (DataRow) this.next();
236 			while (dataRow.hasNext()) {
237 				buf.append(dataRow.next());
238 				buf.append("\t");
239 			}
240 			buf.append("\n");
241 		}
242 
243 		return buf.toString();
244 	}
245 }