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