Coverage Report - org.kuali.student.common.ui.client.util.SearchUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchUtils
0%
0/69
0%
0/66
5.125
SearchUtils$SearchRequestWrapper
0%
0/12
N/A
5.125
 
 1  
 package org.kuali.student.common.ui.client.util;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashSet;
 5  
 import java.util.Iterator;
 6  
 import java.util.List;
 7  
 
 8  
 import org.kuali.student.common.assembly.data.Data;
 9  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 10  
 import org.kuali.student.common.assembly.data.LookupParamMetadata;
 11  
 import org.kuali.student.common.assembly.data.Data.Property;
 12  
 import org.kuali.student.common.assembly.data.Data.Value;
 13  
 import org.kuali.student.common.assembly.data.Metadata.WriteAccess;
 14  
 import org.kuali.student.common.search.dto.SearchParam;
 15  
 import org.kuali.student.common.search.dto.SearchRequest;
 16  
 import org.kuali.student.common.ui.client.application.Application;
 17  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 18  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 19  
 
 20  
 import com.google.gwt.core.client.GWT;
 21  
 
 22  
 /**
 23  
  *  
 24  
  *  This is a library of utility methods that can be used when working with the search rpc service. 
 25  
  *
 26  
  */
 27  0
 public class SearchUtils {
 28  
 
 29  
         /**
 30  
          * This class wraps the search request, with additional information needed to execute/process
 31  
          * a search, such as handling constraints or whether the search should be deferred.
 32  
          * 
 33  
          */
 34  0
         public static class SearchRequestWrapper{
 35  
                 SearchRequest searchRequest;
 36  0
                 HashSet<String> crossConstraints = new HashSet<String>();
 37  0
                 boolean deferSearch = false;
 38  
                 
 39  
                 public SearchRequest getSearchRequest() {
 40  0
                         return searchRequest;
 41  
                 }
 42  
                 
 43  
                 public void setSearchRequest(SearchRequest searchRequest) {
 44  0
                         this.searchRequest = searchRequest;
 45  0
                 }
 46  
                 
 47  
                 public HashSet<String> getCrossConstraints() {
 48  0
                         return crossConstraints;
 49  
                 }
 50  
                 
 51  
                 public void setCrossConstraints(HashSet<String> constraints) {
 52  0
                         this.crossConstraints = constraints;
 53  0
                 }
 54  
 
 55  
                 public boolean isDeferSearch() {
 56  0
                         return deferSearch;
 57  
                 }
 58  
 
 59  
                 public void setDeferSearch(boolean deferSearch) {
 60  0
                         this.deferSearch = deferSearch;
 61  0
                 }                                
 62  
         }
 63  
         
 64  
         /**
 65  
          * Use this to build a SearchRequest given a LookupMetadata definition. The search
 66  
          * request can then be passed into the SearchRpcService to retreive a list of search
 67  
          * results.
 68  
          * 
 69  
          * @param lookup
 70  
          * @return
 71  
          */
 72  
         public static SearchRequest initializeSearchRequest(LookupMetadata lookup) {
 73  
                 //Initialize the search using the SearchRequestWrapper, but return only the
 74  
                 //SearchRequest, since the consumer doesn't care about additional search data 
 75  0
                 SearchRequestWrapper searchRequestWrapper = new SearchRequestWrapper();
 76  0
                 initializeSearchRequest(lookup, searchRequestWrapper);
 77  0
                 return searchRequestWrapper.getSearchRequest();
 78  
         }
 79  
 
 80  
         /**
 81  
          * Use this to build a SearchRequest, update search constraints and deferred search options
 82  
          * contained within the SearchRequestWrapper. The wrapper is mostly to accommodate handling 
 83  
          * of search options required for constraining values that appear in the KSPicker.
 84  
          * 
 85  
          * Generally this method should not be called directly if only the SearchRequest is required.  
 86  
          * @see SearchUtils#initializeSearchRequest(LookupMetadata)
 87  
          * 
 88  
          * @param lookup
 89  
          * @return
 90  
          */
 91  
         public static void initializeSearchRequest(LookupMetadata lookup, SearchRequestWrapper searchRequestWrapper) {
 92  
 
 93  0
                 HashSet<String> crossConstraints = searchRequestWrapper.getCrossConstraints();
 94  
                 
 95  0
                 SearchRequest sr = new SearchRequest();
 96  0
         List<SearchParam> params = new ArrayList<SearchParam>();
 97  
 
 98  0
         sr.setSearchKey(lookup.getSearchTypeId());
 99  
 
 100  0
         if (lookup.getResultSortKey() != null){
 101  0
                 sr.setSortColumn(lookup.getResultSortKey());
 102  
         }
 103  
 
 104  
         //initialize search parameters that are hidden from the UI because they are set to default context specific values
 105  0
         for(final LookupParamMetadata metaParam: lookup.getParams()){
 106  0
             if(metaParam.getWriteAccess() == WriteAccess.NEVER){
 107  0
                 if ((metaParam.getDefaultValueString() == null || metaParam.getDefaultValueString().isEmpty())&&
 108  
                     (metaParam.getDefaultValueList() == null || metaParam.getDefaultValueList().isEmpty())&&
 109  
                     (metaParam.getFieldPath() == null || metaParam.getFieldPath().isEmpty())) {
 110  
                     //FIXME throw an exception?
 111  0
                     GWT.log("Key = " + metaParam.getKey() + " has write access NEVER but has no default value!", null);
 112  0
                     continue;
 113  
                 }
 114  0
                 final SearchParam param = new SearchParam();
 115  0
                 param.setKey(metaParam.getKey());
 116  0
                 if(metaParam.getFieldPath()!=null){
 117  0
                         FieldDescriptor fd = null;
 118  
                         String finalPath;
 119  0
                         if(metaParam.getFieldPath().startsWith("/")){
 120  0
                                 finalPath=metaParam.getFieldPath().substring(1);
 121  
                         }else{
 122  0
                                 finalPath=Application.getApplicationContext().getParentPath()+metaParam.getFieldPath();
 123  
                         }
 124  0
                             crossConstraints.add(finalPath);
 125  0
                             fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath);
 126  0
                         if(fd!=null){
 127  0
                                 if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){
 128  0
                                         Value value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue();
 129  0
                                         if(value!=null&&value.get()!=null){
 130  0
                                                 if(value.get() instanceof Data){
 131  0
                                                         ArrayList<String> listValue = new ArrayList<String>();
 132  0
                                                         for(Iterator<Property> iter =((Data)value.get()).realPropertyIterator();iter.hasNext();){
 133  0
                                                                 listValue.add(iter.next().getValue().toString());
 134  
                                                         }
 135  0
                                                         if(listValue.isEmpty()){
 136  0
                                                                 listValue.add("");
 137  
                                                         }
 138  0
                                                         param.setValue(listValue);
 139  0
                                                 }else{
 140  0
                                                         param.setValue(value.get().toString());        
 141  
                                                 }
 142  
                                         }else{
 143  0
                                                 param.setValue((String)null);
 144  
                                         }                                                
 145  
                                 }
 146  
                         }
 147  0
                         searchRequestWrapper.setDeferSearch(true);
 148  0
                 }else if(metaParam.getDefaultValueList()==null){
 149  0
                     param.setValue(metaParam.getDefaultValueString());
 150  
                 }else{
 151  0
                     param.setValue(metaParam.getDefaultValueList());
 152  
                 }
 153  0
                 params.add(param);
 154  0
             }
 155  0
             else if(metaParam.getWriteAccess() == WriteAccess.WHEN_NULL){
 156  0
                 if((metaParam.getDefaultValueString() != null && !metaParam.getDefaultValueString().isEmpty())||
 157  
                    (metaParam.getDefaultValueList() != null && !metaParam.getDefaultValueList().isEmpty())||
 158  
                    (metaParam.getFieldPath() != null && !metaParam.getFieldPath().isEmpty())){
 159  0
                     final SearchParam param = new SearchParam();
 160  0
                     param.setKey(metaParam.getKey());
 161  0
                     if(metaParam.getFieldPath()!=null){
 162  0
                             FieldDescriptor fd = null;
 163  
                             String finalPath;
 164  0
                             if(metaParam.getFieldPath().startsWith("/")){
 165  0
                                     finalPath=metaParam.getFieldPath().substring(1);
 166  
                             }else{
 167  0
                                     finalPath=Application.getApplicationContext().getParentPath()+metaParam.getFieldPath();
 168  
                             }
 169  0
                                 crossConstraints.add(finalPath);
 170  0
                                 fd = Application.getApplicationContext().getPathToFieldMapping(null, finalPath);
 171  0
                             if(fd!=null){
 172  0
                                     if(fd.getFieldElement().getFieldWidget() instanceof HasDataValue){
 173  0
                                             Value value = ((HasDataValue)fd.getFieldElement().getFieldWidget()).getValue();
 174  0
                                             param.setValue(value==null?null:value.get()==null?null:value.get().toString());
 175  
                                     }
 176  
                             }
 177  0
                             searchRequestWrapper.setDeferSearch(true);
 178  0
                     }else if(metaParam.getDefaultValueList()==null){
 179  0
                         param.setValue(metaParam.getDefaultValueString());
 180  
                     }else{
 181  0
                         param.setValue(metaParam.getDefaultValueList());
 182  
                     }
 183  0
                     params.add(param);
 184  0
                 }
 185  
             }
 186  
         }
 187  0
         sr.setParams(params);
 188  0
         searchRequestWrapper.setSearchRequest(sr);
 189  0
     }
 190  
 
 191  
 }