001    /*
002     * Copyright 2002,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.scaffold.sql;
018    
019    
020    import java.util.Collection;
021    
022    import org.apache.commons.scaffold.lang.Tokens;
023    
024    import org.apache.commons.scaffold.lang.ParameterException;
025    import org.apache.commons.scaffold.lang.ResourceException;
026    
027    import org.apache.commons.scaffold.util.ProcessResult;
028    import org.apache.commons.scaffold.util.ProcessResultBase;
029    import org.apache.commons.scaffold.util.ResultList;
030    import org.apache.commons.scaffold.util.ResultListBase;
031    import org.apache.commons.scaffold.util.Scroller;
032    import org.apache.commons.scaffold.util.ScrollerBean;
033    
034    
035    /**
036     * Base class for scrolling searches.
037     *
038     * @author Ted Husted
039     * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
040     */
041    public class ScrollerBeanBase extends StorageBeanBase implements ScrollerBean {
042    
043    
044        /**
045         * The entry (or page) from which to scroll [1].
046         */
047        public Integer scrollFrom = new Integer(1);
048    
049    
050         // See interface for Javadoc
051        public Integer getScrollFrom() {
052            return scrollFrom;
053        }
054    
055    
056         // See interface for Javadoc
057        public void setScrollFrom(Integer scrollFrom) {
058            this.scrollFrom = scrollFrom;
059        }
060    
061    
062         // See interface for Javadoc
063        public Integer scrollOffset() {
064            int offset = getScrollFrom().intValue();
065            return new Integer(--offset);
066        }
067    
068    
069         // See interface for Javadoc
070        public int scrollRows() {
071            return Scroller.SCROLL_ROWS;
072    
073        }
074    
075    
076         // See interface for Javadoc
077        public String countKey() {
078            return null;
079        }
080    
081    
082         // See interface for Javadoc
083        public boolean hasParameter() {
084            return false;
085        }
086    
087    
088         // See interface for Javadoc
089        public Object searchValue() {
090            return null;
091        }
092    
093    
094         // See interface for Javadoc
095        public boolean isLike() {
096            return false;
097        }
098    
099    
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