Coverage Report - org.apache.commons.scaffold.sql.ScrollerBeanBase
 
Classes in this File Line Coverage Branch Coverage Complexity
ScrollerBeanBase
0%
0/63
0%
0/22
1.65
 
 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  0
 public class ScrollerBeanBase extends StorageBeanBase implements ScrollerBean {
 42  
 
 43  
 
 44  
     /**
 45  
      * The entry (or page) from which to scroll [1].
 46  
      */
 47  0
     public Integer scrollFrom = new Integer(1);
 48  
 
 49  
 
 50  
      // See interface for Javadoc
 51  
     public Integer getScrollFrom() {
 52  0
         return scrollFrom;
 53  
     }
 54  
 
 55  
 
 56  
      // See interface for Javadoc
 57  
     public void setScrollFrom(Integer scrollFrom) {
 58  0
         this.scrollFrom = scrollFrom;
 59  0
     }
 60  
 
 61  
 
 62  
      // See interface for Javadoc
 63  
     public Integer scrollOffset() {
 64  0
         int offset = getScrollFrom().intValue();
 65  0
         return new Integer(--offset);
 66  
     }
 67  
 
 68  
 
 69  
      // See interface for Javadoc
 70  
     public int scrollRows() {
 71  0
         return Scroller.SCROLL_ROWS;
 72  
 
 73  
     }
 74  
 
 75  
 
 76  
      // See interface for Javadoc
 77  
     public String countKey() {
 78  0
         return null;
 79  
     }
 80  
 
 81  
 
 82  
      // See interface for Javadoc
 83  
     public boolean hasParameter() {
 84  0
         return false;
 85  
     }
 86  
 
 87  
 
 88  
      // See interface for Javadoc
 89  
     public Object searchValue() {
 90  0
         return null;
 91  
     }
 92  
 
 93  
 
 94  
      // See interface for Javadoc
 95  
     public boolean isLike() {
 96  0
         return false;
 97  
     }
 98  
 
 99  
 
 100  
      // See interface for Javadoc
 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  
      // See interface for Javadoc
 118  
     public String commandKey() {
 119  0
         return null;
 120  
     }
 121  
 
 122  
 
 123  
 
 124  
      // See interface for Javadoc
 125  
     public String searchProperty() {
 126  0
         return null;
 127  
     }
 128  
 
 129  
 
 130  
      // See interface for Javadoc
 131  
     public String searchLabel() {
 132  0
         return searchProperty();
 133  
     }
 134  
 
 135  
 
 136  
      // See interface for Javadoc
 137  
     public boolean failsOnEmpty () {
 138  0
         return false;
 139  
     }
 140  
 
 141  
 
 142  
      // See interface for Javadoc
 143  
     public String tokenEmptyMessage() {
 144  0
         return Tokens.DATA_ACCESS_EMPTY;
 145  
     }
 146  
 
 147  
 
 148  
      // See interface for Javadoc
 149  
     public String tokenEmptyDispatch() {
 150  0
         return Tokens.FAILURE;
 151  
     }
 152  
 
 153  
 
 154  
      // See interface for Javadoc
 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  
      // See interface for Javadoc
 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  
      // See interface for Javadoc
 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  
      // See interface for Javadoc
 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  
      // See interface for Javadoc
 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  
    } // end scrollerSearch
 253  
 
 254  
 } // end ScrollerBeanBase
 255