Coverage Report - org.kuali.student.core.assembly.util.IdTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
IdTranslator
0%
0/64
0%
0/36
4.8
 
 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.core.assembly.util;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.student.core.assembly.data.AssemblyException;
 23  
 import org.kuali.student.core.assembly.data.LookupMetadata;
 24  
 import org.kuali.student.core.assembly.data.LookupParamMetadata;
 25  
 import org.kuali.student.core.assembly.data.Metadata.WriteAccess;
 26  
 import org.kuali.student.core.search.dto.SearchParam;
 27  
 import org.kuali.student.core.search.dto.SearchRequest;
 28  
 import org.kuali.student.core.search.dto.SearchResult;
 29  
 import org.kuali.student.core.search.dto.SearchResultCell;
 30  
 import org.kuali.student.core.search.dto.SearchResultRow;
 31  
 import org.kuali.student.core.search.service.SearchDispatcher;
 32  
 
 33  
 /**
 34  
  * Builds and issues SearchRequests parameterized with the information from LookupMetadata and the provided ID
 35  
  * 
 36  
  * @author Kuali Student Team
 37  
  *
 38  
  */
 39  
 public class IdTranslator {
 40  
     
 41  
     private static final String ENUMERATION = "enumeration";
 42  0
         private List<SearchParam> additionalParams = new ArrayList<SearchParam>();
 43  
     private SearchDispatcher searchDispatcher;
 44  
     
 45  0
     final Logger LOG = Logger.getLogger(IdTranslator.class);
 46  
 
 47  0
     public IdTranslator(SearchDispatcher searchDispatcher) throws AssemblyException {
 48  0
             this.searchDispatcher = searchDispatcher;
 49  0
     }
 50  
     
 51  
         private SearchRequest buildSearchRequestById(LookupMetadata lookupMetadata, String searchId) {
 52  0
             SearchRequest sr = new SearchRequest();
 53  0
             sr.setNeededTotalResults(false);
 54  
             
 55  0
             sr.setSearchKey(lookupMetadata.getSearchTypeId());
 56  0
             sr.setSortColumn(lookupMetadata.getResultSortKey());
 57  
 
 58  0
             List<SearchParam> searchParams = new ArrayList<SearchParam>();
 59  0
             if (lookupMetadata.getSearchParamIdKey() != null) {
 60  
                     //FIXME: workaround until orch dict can handle multi part keys on initiallookup defs
 61  0
                     if (lookupMetadata.getSearchParamIdKey().contains(ENUMERATION)) {
 62  0
                             for (LookupParamMetadata p : lookupMetadata.getParams()) {
 63  0
                                     if (p.getWriteAccess() != null && p.getWriteAccess().equals(WriteAccess.NEVER) && p.getDefaultValueString() != null) {
 64  0
                                             SearchParam param = createParam(p.getKey(), p.getDefaultValueString());
 65  0
                                             searchParams.add(param);                                                               
 66  0
                                     }
 67  0
                                     else if (p.getWriteAccess() == null || !p.getWriteAccess().equals(WriteAccess.NEVER)){
 68  0
                                             SearchParam param = createParam(p.getKey(), searchId);
 69  0
                                             searchParams.add(param);                                                                   
 70  0
                                     }
 71  
                             }
 72  
                     }
 73  
                     else {
 74  0
                             SearchParam param = createParam(lookupMetadata.getSearchParamIdKey(), searchId);
 75  0
                             searchParams.add(param);        
 76  
                     }
 77  
 
 78  0
                     sr.setParams(searchParams);                 
 79  
             }        
 80  
             else {
 81  0
                     LOG.warn("Unable to build search request for " + lookupMetadata.getSearchTypeId() + " translation for id " + searchId);
 82  
                     
 83  
             }
 84  
 
 85  
             
 86  0
             sr.getParams().addAll(additionalParams);
 87  
 
 88  0
         return sr;
 89  
     }
 90  
     
 91  
     private SearchParam createParam(String key, String value) {
 92  0
             SearchParam param = new SearchParam();
 93  
             
 94  0
             if(key == null) {
 95  0
                         param.setKey("");
 96  
                 } else {
 97  0
                         param.setKey(key);
 98  
                 }
 99  
 
 100  0
             if(value == null) {
 101  0
                         param.setValue("");
 102  
                 } else {
 103  0
                         param.setValue(value);
 104  
                 }
 105  
             
 106  0
             return param;
 107  
     }
 108  
     
 109  
     public IdTranslation getTranslation(LookupMetadata lookupMetadata, String id) throws AssemblyException {
 110  0
         SearchRequest searchRequest = buildSearchRequestById(lookupMetadata, id);
 111  0
         SearchResult searchResults = null;
 112  
                 
 113  0
                 searchResults = searchDispatcher.dispatchSearch(searchRequest);
 114  
                         
 115  
                 
 116  0
         IdTranslation result = null; 
 117  0
         if(searchResults != null && searchResults.getRows().size() > 0){
 118  0
                 result = new IdTranslation();
 119  0
                 result.setId(id);
 120  
                 
 121  0
                 SearchResultRow r = searchResults.getRows().get(0);
 122  0
             for(SearchResultCell c: r.getCells()){
 123  0
                 if(c.getKey().equals(lookupMetadata.getResultDisplayKey())){
 124  0
                     result.addAttribute(c.getKey(), c.getValue());
 125  0
                     result.setDisplay(c.getValue());
 126  
                 } else {
 127  0
                     result.addAttribute(c.getKey(), c.getValue());
 128  
                 }
 129  
             }
 130  
         }
 131  0
         return result;
 132  
         
 133  
     }
 134  
 
 135  
     public String getTranslationForAtp(String value) {
 136  0
         SearchRequest searchRequest = new SearchRequest();
 137  0
         searchRequest.setSearchKey("atp.search.advancedAtpSearch");
 138  0
         ArrayList<SearchParam> searchParams = new ArrayList<SearchParam>();
 139  0
         SearchParam searchParam = new SearchParam();
 140  0
         searchParam.setKey("atp.advancedAtpSearchParam.atpId");
 141  0
         searchParam.setValue(value);
 142  0
         searchParams.add(searchParam);
 143  0
         searchRequest.setParams(searchParams);
 144  0
         SearchResult searchResult = searchDispatcher.dispatchSearch(searchRequest);
 145  0
         if(searchResult!= null){
 146  0
             for (SearchResultRow resultRow : searchResult.getRows()) {
 147  0
                 for (SearchResultCell searchResultCell : resultRow.getCells()) {
 148  0
                     if(searchResultCell.getKey().equals("atp.resultColumn.atpDescrPlain")){
 149  0
                         return searchResultCell.getValue();
 150  
                     }
 151  
                 }
 152  
             }
 153  
         }
 154  0
         return null;
 155  
     }
 156  
 }