Coverage Report - org.kuali.student.common.ui.client.widgets.list.SearchResultListItems
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchResultListItems
0%
0/75
0%
0/26
1.696
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.widgets.list;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.core.assembly.data.LookupMetadata;
 22  
 import org.kuali.student.core.search.dto.ResultColumnInfo;
 23  
 import org.kuali.student.core.search.dto.SearchResultRow;
 24  
 
 25  
 /**
 26  
  * This is a ListItems adapter for search results returned by the search service.
 27  
  * Searches must return id as the first column to be compatible with the ListItems interface. 
 28  
  * 
 29  
  * @author Kuali Student Team
 30  
  *
 31  
  */
 32  
 public class SearchResultListItems implements ListItems{
 33  
 
 34  
     private ArrayList<String> attrKeys;
 35  0
     private List<SearchResultRow> resultDataMap = new ArrayList<SearchResultRow>();
 36  0
     private int attrOffset = 0;
 37  
     //default values for attr indexes
 38  
     //these are necessary for use in search dispatcher assumed 2 columns min
 39  
     //we may want to switch these all to 0 to allow for 1 column result lists
 40  0
     private int sortAttrNdx = 1;        //sort key
 41  0
     private int itemTextAttrNdx = 1;
 42  0
     private int keyAttrNdx = 0;         //unique key
 43  
         
 44  
     public int getSortAttrNdx() {
 45  0
                 return sortAttrNdx;
 46  
         }
 47  
 
 48  
         public void setSortAttrNdx(int sortAttrNdx) {
 49  0
                 this.sortAttrNdx = sortAttrNdx;
 50  0
         }
 51  
 
 52  
         public void setSortAttrNdxFromAttrKey(List<SearchResultRow> results, String sortAttrKey) {
 53  0
                 this.sortAttrNdx = getAttrKeyNdx(results, sortAttrKey);
 54  0
         }
 55  
 
 56  
         public int getKeyAttrNdx() {
 57  0
                 return keyAttrNdx;
 58  
         }
 59  
 
 60  
         public void setKeyAttrNdx(int keyAttrNdx) {
 61  0
                 this.keyAttrNdx = keyAttrNdx;
 62  0
         }
 63  
         
 64  
         public void setKeyAttrNdxFromAttrKey(List<SearchResultRow> results, String keyAttrKey) {
 65  0
                 this.keyAttrNdx = getAttrKeyNdx(results, keyAttrKey);
 66  0
         }
 67  
 
 68  
         public int getItemTextAttrNdx() {
 69  0
                 return itemTextAttrNdx;
 70  
         }
 71  
 
 72  
         public void setItemTextAttrNdx(int itemTextAttrNdx) {
 73  0
                 this.itemTextAttrNdx = itemTextAttrNdx;
 74  0
         }
 75  
 
 76  
         public void setItemTextAttrNdxFromAttrKey(List<SearchResultRow> results, String itemTextAttrKey) {
 77  0
                 this.itemTextAttrNdx = getAttrKeyNdx(results, itemTextAttrKey);
 78  0
         }
 79  
         
 80  0
         public SearchResultListItems(){ 
 81  0
     }
 82  
     
 83  
     private void setAttrNdxs(List<SearchResultRow> results, LookupMetadata lookupMetadata) {
 84  
             
 85  0
             setItemTextAttrNdxFromAttrKey(results, lookupMetadata.getResultDisplayKey());
 86  0
             setKeyAttrNdxFromAttrKey(results, lookupMetadata.getResultReturnKey());
 87  0
             setSortAttrNdxFromAttrKey(results, lookupMetadata.getResultSortKey());        
 88  0
     }
 89  
     
 90  0
     public SearchResultListItems(List<ResultColumnInfo> resultColumns, List<SearchResultRow> results, LookupMetadata lookupMetadata){
 91  
             
 92  0
             setAttrNdxs(results, lookupMetadata);
 93  0
             setResultColumns(resultColumns);
 94  0
         setResults(results);
 95  0
     }   
 96  
     
 97  0
     public SearchResultListItems(List<SearchResultRow> results, LookupMetadata lookupMetadata){
 98  
     
 99  0
             setAttrNdxs(results, lookupMetadata);
 100  0
         setResults(results);
 101  0
     }
 102  
     
 103  0
     public SearchResultListItems(List<SearchResultRow> results){
 104  
         
 105  0
             setResults(results);
 106  0
     }
 107  
             
 108  
     public void setResultColumns(List<ResultColumnInfo> resultColumns){
 109  0
         attrKeys = new ArrayList<String>();
 110  
         
 111  0
         for (ResultColumnInfo r:resultColumns){
 112  0
             attrKeys.add(r.getName());
 113  
         }
 114  
         
 115  
         //Exclude identifier column from list items (don't want this displayed)
 116  0
         attrKeys.remove(0);
 117  0
         attrOffset = 1;
 118  0
     }
 119  
            
 120  
     public void setResults(List<SearchResultRow> results) {          
 121  0
         resultDataMap.clear();
 122  
 
 123  0
         if (results != null){            
 124  0
             resultDataMap = new ArrayList<SearchResultRow>(results);           
 125  
             
 126  
             //Default keys for column attributes
 127  0
             if (results.size() > 0){
 128  0
                 SearchResultRow r = results.get(0);
 129  0
                 if (attrKeys == null){
 130  0
                     attrKeys = new ArrayList<String>();
 131  0
                     for (int i=0; i < r.getCells().size(); i ++){
 132  0
                         attrKeys.add("attr" + String.valueOf(i));
 133  
                     }
 134  
                 }
 135  
             }
 136  
         }
 137  0
     }
 138  
     
 139  
     private int getAttrKeyNdx(List<SearchResultRow> results, String keyAttrKey) {
 140  
 
 141  0
             if (results.size() > 0){
 142  0
                 for (int i=0; i < results.get(0).getCells().size(); i++){
 143  0
                         if (results.get(0).getCells().get(i).getKey().equals(keyAttrKey)) {
 144  0
                                 return i;
 145  
                         }
 146  
                 }
 147  
                 }
 148  
                 
 149  0
                 return 0;
 150  
         }
 151  
     
 152  
     @Override
 153  
     public List<String> getAttrKeys() {                       
 154  0
         return attrKeys;
 155  
     }
 156  
 
 157  
     @Override
 158  
     public String getItemAttribute(String id, String attrKey) {
 159  0
         SearchResultRow r = getListItem(id);
 160  
         
 161  0
         int attrIndex = attrKeys.indexOf(attrKey);
 162  0
         if (attrIndex >= 0 && r != null){
 163  0
             return r.getCells().get(attrIndex + attrOffset).getValue(); 
 164  
         }
 165  
 
 166  0
         return null;
 167  
     }
 168  
 
 169  
     /**
 170  
      * @see org.kuali.student.common.ui.client.widgets.list.ListItems#getItemCount()
 171  
      */
 172  
     @Override
 173  
     public int getItemCount() {
 174  0
         return resultDataMap.size();
 175  
     }
 176  
 
 177  
     /**
 178  
      * @see org.kuali.student.common.ui.client.widgets.list.ListItems#getItemIds()
 179  
      */
 180  
     @Override
 181  
     public List<String> getItemIds() {
 182  0
         List<String> ids = new ArrayList<String>();
 183  
 
 184  0
         for (SearchResultRow s:resultDataMap){
 185  0
             ids.add(s.getCells().get(keyAttrNdx).getValue());
 186  
         }
 187  
         
 188  0
         return ids;
 189  
     }
 190  
 
 191  
     /**
 192  
      * @see org.kuali.student.common.ui.client.widgets.list.ListItems#getItemText(java.lang.String)
 193  
      */
 194  
     @Override
 195  
     public String getItemText(String id) {
 196  0
         return getListItem(id).getCells().get(itemTextAttrNdx).getValue();
 197  
     }
 198  
     
 199  
     private SearchResultRow getListItem(String id) {
 200  0
         for (SearchResultRow s : resultDataMap) {
 201  0
             if (s.getCells().get(keyAttrNdx).getValue().equals(id)) {
 202  0
                 return s;
 203  
             }
 204  
         }
 205  0
         return null;
 206  
     }
 207  
     
 208  
 }