1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.extremecomponents.table.model;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.apache.commons.beanutils.MethodUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.extremecomponents.table.bean.Column;
29 import org.extremecomponents.table.bean.TableSection;
30 import org.extremecomponents.table.cell.Cell;
31 import org.extremecomponents.table.cell.HeaderCell;
32
33
34 /***
35 * Helpful utilities for the Model
36 *
37 * @author Jeff Johnston
38 */
39 public class TableModelUtils
40 {
41 private static Log logger = LogFactory.getLog(TableModelUtils.class);
42
43 private TableModelUtils()
44 {
45 }
46
47 public static List getHeader(TableModel model, String method)
48 {
49 List results = new ArrayList();
50
51 Iterator columns = model.getColumnMetaData().getColumns();
52
53 for (Iterator iter = columns; iter.hasNext();)
54 {
55 Column column = (Column) iter.next();
56 Cell cell = model.getCachedCell(HeaderCell.class);
57 cell.init(model, column, null);
58
59 try
60 {
61 results.add((String) MethodUtils.invokeMethod(cell, method, null));
62 }
63 catch (Exception e)
64 {
65 logger.debug("Could not invoke the cell method.", e);
66 }
67
68 cell.destroy();
69 }
70
71 return results;
72 }
73
74 /***
75 * Grab the relevant section of the table.
76 */
77 public static List splitIntoTableSectionRows(TableModel model, List rows, TableSection tableSection)
78 {
79 boolean invokeExport = model.getExportHandler().invokeExport();
80
81 if (invokeExport)
82 {
83 return rows;
84 }
85
86 List results = new ArrayList();
87
88 for (int i = tableSection.getRowStart(); i < tableSection.getRowEnd(); i++)
89 {
90 Object obj = (Object) rows.get(i);
91 results.add(obj);
92 }
93
94 return results;
95 }
96
97 /***
98 * Find out where we should start and end to get the relevant section of the table.
99 */
100 public static TableSection getTableSection(TableModel model, int totalRows)
101 {
102 boolean invokeExport = model.getExportHandler().invokeExport();
103
104 if (invokeExport)
105 {
106 return new TableSection(1, 1, totalRows, totalRows);
107 }
108
109 String pagination = model.getRegistry().getParameter(ParameterRegistry.PAGE);
110 int page = 1;
111
112 if (!StringUtils.isEmpty(pagination))
113 {
114 page = Integer.parseInt(pagination);
115 }
116
117 int maxRows = Integer.parseInt(model.getTable().getMaxRows());
118
119 if (maxRows == 0)
120 {
121 maxRows = totalRows;
122 }
123
124 int rowStart = (page - 1) * maxRows;
125
126 int rowEnd = rowStart + maxRows;
127
128 if (rowEnd > totalRows)
129 {
130 rowEnd = totalRows;
131 }
132
133 return new TableSection(page, rowStart, rowEnd, totalRows);
134 }
135
136 /***
137 * Get a list of bean property names
138 */
139 public static List beanProperties(Object bean)
140 throws Exception
141 {
142 List properties = new ArrayList();
143
144 if (bean instanceof Map)
145 {
146 properties.addAll(((Map) bean).keySet());
147 }
148 else
149 {
150 properties.addAll(BeanUtils.describe(bean).keySet());
151 }
152
153 return properties;
154 }
155
156 public static boolean canGroup(TableModel model)
157 {
158 for (Iterator iter = model.getColumnMetaData().getColumns(); iter.hasNext();)
159 {
160 Column column = (Column) iter.next();
161
162 if (column.canGroup())
163 {
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 /***
172 * TODO: This needs to move.
173 * Whichever View needs to worry about parsing should handle the parsing.
174 */
175 public static String parseCell(String value)
176 {
177 int aStart = value.indexOf("<a");
178
179 if (aStart != -1)
180 {
181 int aClose = value.indexOf(">");
182 int aEnd = value.lastIndexOf("</a>");
183 value = value.substring(aClose + 1, aEnd);
184 }
185
186
187
188 if (StringUtils.contains(value, "&"))
189 {
190 value = StringUtils.replace(value, "&", "&");
191 }
192
193 return value;
194 }
195 }