1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.scaffold.sql; |
18 | |
|
19 | |
|
20 | |
import java.util.Collection; |
21 | |
|
22 | |
import org.apache.commons.scaffold.lang.Tokens; |
23 | |
|
24 | |
import org.apache.commons.scaffold.lang.ParameterException; |
25 | |
import org.apache.commons.scaffold.lang.ResourceException; |
26 | |
|
27 | |
import org.apache.commons.scaffold.util.ProcessResult; |
28 | |
import org.apache.commons.scaffold.util.ProcessResultBase; |
29 | |
import org.apache.commons.scaffold.util.ResultList; |
30 | |
import org.apache.commons.scaffold.util.ResultListBase; |
31 | |
import org.apache.commons.scaffold.util.Scroller; |
32 | |
import org.apache.commons.scaffold.util.ScrollerBean; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class ScrollerBeanBase extends StorageBeanBase implements ScrollerBean { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public Integer scrollFrom = new Integer(1); |
48 | |
|
49 | |
|
50 | |
|
51 | |
public Integer getScrollFrom() { |
52 | 0 | return scrollFrom; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
public void setScrollFrom(Integer scrollFrom) { |
58 | 0 | this.scrollFrom = scrollFrom; |
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
public Integer scrollOffset() { |
64 | 0 | int offset = getScrollFrom().intValue(); |
65 | 0 | return new Integer(--offset); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
public int scrollRows() { |
71 | 0 | return Scroller.SCROLL_ROWS; |
72 | |
|
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
public String countKey() { |
78 | 0 | return null; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
public boolean hasParameter() { |
84 | 0 | return false; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
public Object searchValue() { |
90 | 0 | return null; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
public boolean isLike() { |
96 | 0 | return false; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
public int entryCount() throws ResourceException { |
102 | |
|
103 | 0 | if (hasParameter()) { |
104 | |
|
105 | 0 | if (isLike()) |
106 | 0 | return count(countKey(),like(searchValue().toString())); |
107 | |
else |
108 | 0 | return count(countKey(),searchValue()); |
109 | |
|
110 | |
} |
111 | |
|
112 | 0 | return count(countKey()); |
113 | |
|
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
public String commandKey() { |
119 | 0 | return null; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public String searchProperty() { |
126 | 0 | return null; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
public String searchLabel() { |
132 | 0 | return searchProperty(); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
public boolean failsOnEmpty () { |
138 | 0 | return false; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
public String tokenEmptyMessage() { |
144 | 0 | return Tokens.DATA_ACCESS_EMPTY; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
public String tokenEmptyDispatch() { |
150 | 0 | return Tokens.FAILURE; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
public Object[] scrollerParams(String parameter) { |
156 | |
|
157 | 0 | Object[] parameters = null; |
158 | |
|
159 | 0 | if (hasParameter()) { |
160 | |
|
161 | 0 | parameters = new Object[3]; |
162 | 0 | parameters[0] = parameter; |
163 | 0 | parameters[1] = scrollOffset(); |
164 | 0 | parameters[2] = new Integer(scrollRows()); |
165 | |
} |
166 | |
|
167 | |
else { |
168 | |
|
169 | 0 | parameters = new Object[2]; |
170 | 0 | parameters[0] = scrollOffset(); |
171 | 0 | parameters[1] = new Integer(scrollRows()); |
172 | |
} |
173 | |
|
174 | 0 | return parameters; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
public Collection result(Object target, Object parameter) |
180 | |
throws ResourceException { |
181 | |
|
182 | 0 | String param = null; |
183 | 0 | if (null!=parameter) { |
184 | 0 | if (isLike()) param = like(parameter.toString()); |
185 | 0 | else param = parameter.toString(); |
186 | |
} |
187 | |
|
188 | 0 | return findCollection( |
189 | |
target, |
190 | |
commandKey(), |
191 | |
scrollerParams(param) |
192 | |
); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
public Scroller newScroller(int entries) throws ResourceException { |
198 | |
|
199 | 0 | Scroller scroller = new Scroller(); |
200 | 0 | scroller.calculate( |
201 | |
entries, |
202 | |
getScrollFrom().intValue(), |
203 | |
entryCount(), |
204 | |
scrollRows() |
205 | |
); |
206 | |
|
207 | 0 | if (hasParameter()) { |
208 | 0 | java.util.Map map = new java.util.HashMap(); |
209 | 0 | map.put(searchProperty(),searchValue()); |
210 | 0 | scroller.setParameters(map); |
211 | |
} |
212 | |
|
213 | 0 | return scroller; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
public void listLegend(ResultList list) { |
219 | |
|
220 | 0 | if (hasParameter()) |
221 | 0 | list.setLegend(searchLabel(),searchValue().toString()); |
222 | |
else |
223 | 0 | list.setLegend(searchLabel()); |
224 | |
|
225 | 0 | } |
226 | |
|
227 | |
|
228 | |
|
229 | |
public ProcessResult scrollerSearch() throws Exception { |
230 | |
|
231 | 0 | Object key = null; |
232 | 0 | if (hasParameter()) { |
233 | 0 | key = searchValue(); |
234 | 0 | if (null==key) { |
235 | 0 | throw new ParameterException(); |
236 | |
} |
237 | |
} |
238 | |
|
239 | 0 | ResultList list = new ResultListBase (result(this,key)); |
240 | 0 | Scroller scroller = newScroller(list.size()); |
241 | 0 | list.setScroller(scroller); |
242 | 0 | ProcessResult result = new ProcessResultBase(list); |
243 | |
|
244 | 0 | listLegend(list); |
245 | |
|
246 | 0 | if (failsOnEmpty() && list.isEmpty()) { |
247 | 0 | result.addMessage(tokenEmptyMessage()); |
248 | 0 | result.setDispatch(tokenEmptyDispatch()); |
249 | |
} |
250 | 0 | return result; |
251 | |
|
252 | |
} |
253 | |
|
254 | |
} |
255 | |
|