View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Base class for scrolling searches.
37   *
38   * @author Ted Husted
39   * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
40   */
41  public class ScrollerBeanBase extends StorageBeanBase implements ScrollerBean {
42  
43  
44      /**
45       * The entry (or page) from which to scroll [1].
46       */
47      public Integer scrollFrom = new Integer(1);
48  
49  
50       // See interface for Javadoc
51      public Integer getScrollFrom() {
52          return scrollFrom;
53      }
54  
55  
56       // See interface for Javadoc
57      public void setScrollFrom(Integer scrollFrom) {
58          this.scrollFrom = scrollFrom;
59      }
60  
61  
62       // See interface for Javadoc
63      public Integer scrollOffset() {
64          int offset = getScrollFrom().intValue();
65          return new Integer(--offset);
66      }
67  
68  
69       // See interface for Javadoc
70      public int scrollRows() {
71          return Scroller.SCROLL_ROWS;
72  
73      }
74  
75  
76       // See interface for Javadoc
77      public String countKey() {
78          return null;
79      }
80  
81  
82       // See interface for Javadoc
83      public boolean hasParameter() {
84          return false;
85      }
86  
87  
88       // See interface for Javadoc
89      public Object searchValue() {
90          return null;
91      }
92  
93  
94       // See interface for Javadoc
95      public boolean isLike() {
96          return false;
97      }
98  
99  
100      // See interface for Javadoc
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      // See interface for Javadoc
118     public String commandKey() {
119         return null;
120     }
121 
122 
123 
124      // See interface for Javadoc
125     public String searchProperty() {
126         return null;
127     }
128 
129 
130      // See interface for Javadoc
131     public String searchLabel() {
132         return searchProperty();
133     }
134 
135 
136      // See interface for Javadoc
137     public boolean failsOnEmpty () {
138         return false;
139     }
140 
141 
142      // See interface for Javadoc
143     public String tokenEmptyMessage() {
144         return Tokens.DATA_ACCESS_EMPTY;
145     }
146 
147 
148      // See interface for Javadoc
149     public String tokenEmptyDispatch() {
150         return Tokens.FAILURE;
151     }
152 
153 
154      // See interface for Javadoc
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      // See interface for Javadoc
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      // See interface for Javadoc
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      // See interface for Javadoc
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      // See interface for Javadoc
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    } // end scrollerSearch
253 
254 } // end ScrollerBeanBase
255