Coverage Report - org.kuali.student.common.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.common.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.common.assembly.data.AssemblyException;
 23  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 24  
 import org.kuali.student.common.assembly.data.LookupParamMetadata;
 25  
 import org.kuali.student.common.assembly.data.Metadata.WriteAccess;
 26  
 import org.kuali.student.common.search.dto.SearchParam;
 27  
 import org.kuali.student.common.search.dto.SearchRequest;
 28  
 import org.kuali.student.common.search.dto.SearchResult;
 29  
 import org.kuali.student.common.search.dto.SearchResultCell;
 30  
 import org.kuali.student.common.search.dto.SearchResultRow;
 31  
 import org.kuali.student.common.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  
 
 59  0
             List<SearchParam> searchParams = new ArrayList<SearchParam>();
 60  0
             if (lookupMetadata.getSearchParamIdKey() != null) {
 61  
                     //FIXME: workaround until orch dict can handle multi part keys on initiallookup defs
 62  0
                     if (lookupMetadata.getSearchParamIdKey().contains(ENUMERATION)) {
 63  0
                             for (LookupParamMetadata p : lookupMetadata.getParams()) {
 64  0
                                     if (p.getWriteAccess() != null && p.getWriteAccess().equals(WriteAccess.NEVER) && p.getDefaultValueString() != null) {
 65  0
                                             SearchParam param = createParam(p.getKey(), p.getDefaultValueString());
 66  0
                                             searchParams.add(param);                                                               
 67  0
                                     }
 68  0
                                     else if (p.getWriteAccess() == null || !p.getWriteAccess().equals(WriteAccess.NEVER)){
 69  0
                                             SearchParam param = createParam(p.getKey(), searchId);
 70  0
                                             searchParams.add(param);                                                                   
 71  0
                                     }
 72  
                             }
 73  
                     }
 74  
                     else {
 75  0
                             SearchParam param = createParam(lookupMetadata.getSearchParamIdKey(), searchId);
 76  0
                             searchParams.add(param);        
 77  
                     }
 78  
 
 79  0
                     sr.setParams(searchParams);                 
 80  
             }        
 81  
             else {
 82  0
                     LOG.warn("Unable to build search request for " + lookupMetadata.getSearchTypeId() + " translation for id " + searchId);
 83  
                     
 84  
             }
 85  
 
 86  
             
 87  0
             sr.getParams().addAll(additionalParams);
 88  
 
 89  0
         return sr;
 90  
     }
 91  
     
 92  
     private SearchParam createParam(String key, String value) {
 93  0
             SearchParam param = new SearchParam();
 94  
             
 95  0
             if(key == null) {
 96  0
                         param.setKey("");
 97  
                 } else {
 98  0
                         param.setKey(key);
 99  
                 }
 100  
 
 101  0
             if(value == null) {
 102  0
                         param.setValue("");
 103  
                 } else {
 104  0
                         param.setValue(value);
 105  
                 }
 106  
             
 107  0
             return param;
 108  
     }
 109  
     
 110  
     public IdTranslation getTranslation(LookupMetadata lookupMetadata, String id) throws AssemblyException {
 111  0
         SearchRequest searchRequest = buildSearchRequestById(lookupMetadata, id);
 112  0
         SearchResult searchResults = null;
 113  
                 
 114  0
                 searchResults = searchDispatcher.dispatchSearch(searchRequest);
 115  
                         
 116  
                 
 117  0
         IdTranslation result = null; 
 118  0
         if(searchResults != null && searchResults.getRows().size() > 0){
 119  0
                 result = new IdTranslation();
 120  0
                 result.setId(id);
 121  
                 
 122  0
                 SearchResultRow r = searchResults.getRows().get(0);
 123  0
             for(SearchResultCell c: r.getCells()){
 124  0
                 if(c.getKey().equals(lookupMetadata.getResultDisplayKey())){
 125  0
                     result.addAttribute(c.getKey(), c.getValue());
 126  0
                     result.setDisplay(c.getValue());
 127  
                 } else {
 128  0
                     result.addAttribute(c.getKey(), c.getValue());
 129  
                 }
 130  
             }
 131  
         }
 132  0
         return result;
 133  
         
 134  
     }
 135  
 
 136  
     public String getTranslationForAtp(String value) {
 137  0
         SearchRequest searchRequest = new SearchRequest();
 138  0
         searchRequest.setSearchKey("atp.search.advancedAtpSearch");
 139  0
         ArrayList<SearchParam> searchParams = new ArrayList<SearchParam>();
 140  0
         SearchParam searchParam = new SearchParam();
 141  0
         searchParam.setKey("atp.advancedAtpSearchParam.atpId");
 142  0
         searchParam.setValue(value);
 143  0
         searchParams.add(searchParam);
 144  0
         searchRequest.setParams(searchParams);
 145  0
         SearchResult searchResult = searchDispatcher.dispatchSearch(searchRequest);
 146  0
         if(searchResult!= null){
 147  0
             for (SearchResultRow resultRow : searchResult.getRows()) {
 148  0
                 for (SearchResultCell searchResultCell : resultRow.getCells()) {
 149  0
                     if(searchResultCell.getKey().equals("atp.resultColumn.atpDescrPlain")){
 150  0
                         return searchResultCell.getValue();
 151  
                     }
 152  
                 }
 153  
             }
 154  
         }
 155  0
         return null;
 156  
     }
 157  
 }